LCOV - code coverage report
Current view: top level - drivers/pinctrl - core.c (source / functions) Hit Total Coverage
Test: Real Lines: 325 700 46.4 %
Date: 2020-10-17 15:46:16 Functions: 0 82 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0-only
       2                 :            : /*
       3                 :            :  * Core driver for the pin control subsystem
       4                 :            :  *
       5                 :            :  * Copyright (C) 2011-2012 ST-Ericsson SA
       6                 :            :  * Written on behalf of Linaro for ST-Ericsson
       7                 :            :  * Based on bits of regulator core, gpio core and clk core
       8                 :            :  *
       9                 :            :  * Author: Linus Walleij <linus.walleij@linaro.org>
      10                 :            :  *
      11                 :            :  * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
      12                 :            :  */
      13                 :            : #define pr_fmt(fmt) "pinctrl core: " fmt
      14                 :            : 
      15                 :            : #include <linux/kernel.h>
      16                 :            : #include <linux/kref.h>
      17                 :            : #include <linux/export.h>
      18                 :            : #include <linux/init.h>
      19                 :            : #include <linux/device.h>
      20                 :            : #include <linux/slab.h>
      21                 :            : #include <linux/err.h>
      22                 :            : #include <linux/list.h>
      23                 :            : #include <linux/debugfs.h>
      24                 :            : #include <linux/seq_file.h>
      25                 :            : #include <linux/pinctrl/consumer.h>
      26                 :            : #include <linux/pinctrl/pinctrl.h>
      27                 :            : #include <linux/pinctrl/machine.h>
      28                 :            : 
      29                 :            : #ifdef CONFIG_GPIOLIB
      30                 :            : #include <asm-generic/gpio.h>
      31                 :            : #endif
      32                 :            : 
      33                 :            : #include "core.h"
      34                 :            : #include "devicetree.h"
      35                 :            : #include "pinmux.h"
      36                 :            : #include "pinconf.h"
      37                 :            : 
      38                 :            : 
      39                 :            : static bool pinctrl_dummy_state;
      40                 :            : 
      41                 :            : /* Mutex taken to protect pinctrl_list */
      42                 :            : static DEFINE_MUTEX(pinctrl_list_mutex);
      43                 :            : 
      44                 :            : /* Mutex taken to protect pinctrl_maps */
      45                 :            : DEFINE_MUTEX(pinctrl_maps_mutex);
      46                 :            : 
      47                 :            : /* Mutex taken to protect pinctrldev_list */
      48                 :            : static DEFINE_MUTEX(pinctrldev_list_mutex);
      49                 :            : 
      50                 :            : /* Global list of pin control devices (struct pinctrl_dev) */
      51                 :            : static LIST_HEAD(pinctrldev_list);
      52                 :            : 
      53                 :            : /* List of pin controller handles (struct pinctrl) */
      54                 :            : static LIST_HEAD(pinctrl_list);
      55                 :            : 
      56                 :            : /* List of pinctrl maps (struct pinctrl_maps) */
      57                 :            : LIST_HEAD(pinctrl_maps);
      58                 :            : 
      59                 :            : 
      60                 :            : /**
      61                 :            :  * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
      62                 :            :  *
      63                 :            :  * Usually this function is called by platforms without pinctrl driver support
      64                 :            :  * but run with some shared drivers using pinctrl APIs.
      65                 :            :  * After calling this function, the pinctrl core will return successfully
      66                 :            :  * with creating a dummy state for the driver to keep going smoothly.
      67                 :            :  */
      68                 :          0 : void pinctrl_provide_dummies(void)
      69                 :            : {
      70                 :          0 :         pinctrl_dummy_state = true;
      71                 :          0 : }
      72                 :            : 
      73                 :          0 : const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
      74                 :            : {
      75                 :            :         /* We're not allowed to register devices without name */
      76                 :          0 :         return pctldev->desc->name;
      77                 :            : }
      78                 :            : EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
      79                 :            : 
      80                 :          0 : const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
      81                 :            : {
      82                 :          0 :         return dev_name(pctldev->dev);
      83                 :            : }
      84                 :            : EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
      85                 :            : 
      86                 :          3 : void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
      87                 :            : {
      88                 :          3 :         return pctldev->driver_data;
      89                 :            : }
      90                 :            : EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
      91                 :            : 
      92                 :            : /**
      93                 :            :  * get_pinctrl_dev_from_devname() - look up pin controller device
      94                 :            :  * @devname: the name of a device instance, as returned by dev_name()
      95                 :            :  *
      96                 :            :  * Looks up a pin control device matching a certain device name or pure device
      97                 :            :  * pointer, the pure device pointer will take precedence.
      98                 :            :  */
      99                 :          3 : struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
     100                 :            : {
     101                 :            :         struct pinctrl_dev *pctldev;
     102                 :            : 
     103                 :          3 :         if (!devname)
     104                 :            :                 return NULL;
     105                 :            : 
     106                 :          3 :         mutex_lock(&pinctrldev_list_mutex);
     107                 :            : 
     108                 :          3 :         list_for_each_entry(pctldev, &pinctrldev_list, node) {
     109                 :          3 :                 if (!strcmp(dev_name(pctldev->dev), devname)) {
     110                 :            :                         /* Matched on device name */
     111                 :          3 :                         mutex_unlock(&pinctrldev_list_mutex);
     112                 :          3 :                         return pctldev;
     113                 :            :                 }
     114                 :            :         }
     115                 :            : 
     116                 :          0 :         mutex_unlock(&pinctrldev_list_mutex);
     117                 :            : 
     118                 :          0 :         return NULL;
     119                 :            : }
     120                 :            : 
     121                 :          3 : struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
     122                 :            : {
     123                 :            :         struct pinctrl_dev *pctldev;
     124                 :            : 
     125                 :          3 :         mutex_lock(&pinctrldev_list_mutex);
     126                 :            : 
     127                 :          3 :         list_for_each_entry(pctldev, &pinctrldev_list, node)
     128                 :          3 :                 if (pctldev->dev->of_node == np) {
     129                 :          3 :                         mutex_unlock(&pinctrldev_list_mutex);
     130                 :          3 :                         return pctldev;
     131                 :            :                 }
     132                 :            : 
     133                 :          0 :         mutex_unlock(&pinctrldev_list_mutex);
     134                 :            : 
     135                 :          0 :         return NULL;
     136                 :            : }
     137                 :            : 
     138                 :            : /**
     139                 :            :  * pin_get_from_name() - look up a pin number from a name
     140                 :            :  * @pctldev: the pin control device to lookup the pin on
     141                 :            :  * @name: the name of the pin to look up
     142                 :            :  */
     143                 :          0 : int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
     144                 :            : {
     145                 :            :         unsigned i, pin;
     146                 :            : 
     147                 :            :         /* The pin number can be retrived from the pin controller descriptor */
     148                 :          0 :         for (i = 0; i < pctldev->desc->npins; i++) {
     149                 :            :                 struct pin_desc *desc;
     150                 :            : 
     151                 :          0 :                 pin = pctldev->desc->pins[i].number;
     152                 :            :                 desc = pin_desc_get(pctldev, pin);
     153                 :            :                 /* Pin space may be sparse */
     154                 :          0 :                 if (desc && !strcmp(name, desc->name))
     155                 :          0 :                         return pin;
     156                 :            :         }
     157                 :            : 
     158                 :            :         return -EINVAL;
     159                 :            : }
     160                 :            : 
     161                 :            : /**
     162                 :            :  * pin_get_name_from_id() - look up a pin name from a pin id
     163                 :            :  * @pctldev: the pin control device to lookup the pin on
     164                 :            :  * @name: the name of the pin to look up
     165                 :            :  */
     166                 :          0 : const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
     167                 :            : {
     168                 :            :         const struct pin_desc *desc;
     169                 :            : 
     170                 :            :         desc = pin_desc_get(pctldev, pin);
     171                 :          0 :         if (!desc) {
     172                 :          0 :                 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
     173                 :            :                         pin);
     174                 :          0 :                 return NULL;
     175                 :            :         }
     176                 :            : 
     177                 :          0 :         return desc->name;
     178                 :            : }
     179                 :            : 
     180                 :            : /* Deletes a range of pin descriptors */
     181                 :          0 : static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
     182                 :            :                                   const struct pinctrl_pin_desc *pins,
     183                 :            :                                   unsigned num_pins)
     184                 :            : {
     185                 :            :         int i;
     186                 :            : 
     187                 :          0 :         for (i = 0; i < num_pins; i++) {
     188                 :            :                 struct pin_desc *pindesc;
     189                 :            : 
     190                 :          0 :                 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
     191                 :          0 :                                             pins[i].number);
     192                 :          0 :                 if (pindesc) {
     193                 :          0 :                         radix_tree_delete(&pctldev->pin_desc_tree,
     194                 :          0 :                                           pins[i].number);
     195                 :          0 :                         if (pindesc->dynamic_name)
     196                 :          0 :                                 kfree(pindesc->name);
     197                 :            :                 }
     198                 :          0 :                 kfree(pindesc);
     199                 :            :         }
     200                 :          0 : }
     201                 :            : 
     202                 :          3 : static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
     203                 :            :                                     const struct pinctrl_pin_desc *pin)
     204                 :            : {
     205                 :            :         struct pin_desc *pindesc;
     206                 :            : 
     207                 :          3 :         pindesc = pin_desc_get(pctldev, pin->number);
     208                 :          3 :         if (pindesc) {
     209                 :          0 :                 dev_err(pctldev->dev, "pin %d already registered\n",
     210                 :            :                         pin->number);
     211                 :          0 :                 return -EINVAL;
     212                 :            :         }
     213                 :            : 
     214                 :          3 :         pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
     215                 :          3 :         if (!pindesc)
     216                 :            :                 return -ENOMEM;
     217                 :            : 
     218                 :            :         /* Set owner */
     219                 :          3 :         pindesc->pctldev = pctldev;
     220                 :            : 
     221                 :            :         /* Copy basic pin info */
     222                 :          3 :         if (pin->name) {
     223                 :          3 :                 pindesc->name = pin->name;
     224                 :            :         } else {
     225                 :          0 :                 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number);
     226                 :          0 :                 if (!pindesc->name) {
     227                 :          0 :                         kfree(pindesc);
     228                 :          0 :                         return -ENOMEM;
     229                 :            :                 }
     230                 :          0 :                 pindesc->dynamic_name = true;
     231                 :            :         }
     232                 :            : 
     233                 :          3 :         pindesc->drv_data = pin->drv_data;
     234                 :            : 
     235                 :          3 :         radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);
     236                 :            :         pr_debug("registered pin %d (%s) on %s\n",
     237                 :            :                  pin->number, pindesc->name, pctldev->desc->name);
     238                 :          3 :         return 0;
     239                 :            : }
     240                 :            : 
     241                 :          3 : static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
     242                 :            :                                  const struct pinctrl_pin_desc *pins,
     243                 :            :                                  unsigned num_descs)
     244                 :            : {
     245                 :            :         unsigned i;
     246                 :            :         int ret = 0;
     247                 :            : 
     248                 :          3 :         for (i = 0; i < num_descs; i++) {
     249                 :          3 :                 ret = pinctrl_register_one_pin(pctldev, &pins[i]);
     250                 :          3 :                 if (ret)
     251                 :          0 :                         return ret;
     252                 :            :         }
     253                 :            : 
     254                 :            :         return 0;
     255                 :            : }
     256                 :            : 
     257                 :            : /**
     258                 :            :  * gpio_to_pin() - GPIO range GPIO number to pin number translation
     259                 :            :  * @range: GPIO range used for the translation
     260                 :            :  * @gpio: gpio pin to translate to a pin number
     261                 :            :  *
     262                 :            :  * Finds the pin number for a given GPIO using the specified GPIO range
     263                 :            :  * as a base for translation. The distinction between linear GPIO ranges
     264                 :            :  * and pin list based GPIO ranges is managed correctly by this function.
     265                 :            :  *
     266                 :            :  * This function assumes the gpio is part of the specified GPIO range, use
     267                 :            :  * only after making sure this is the case (e.g. by calling it on the
     268                 :            :  * result of successful pinctrl_get_device_gpio_range calls)!
     269                 :            :  */
     270                 :            : static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
     271                 :            :                                 unsigned int gpio)
     272                 :            : {
     273                 :          3 :         unsigned int offset = gpio - range->base;
     274                 :          3 :         if (range->pins)
     275                 :          0 :                 return range->pins[offset];
     276                 :            :         else
     277                 :          3 :                 return range->pin_base + offset;
     278                 :            : }
     279                 :            : 
     280                 :            : /**
     281                 :            :  * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
     282                 :            :  * @pctldev: pin controller device to check
     283                 :            :  * @gpio: gpio pin to check taken from the global GPIO pin space
     284                 :            :  *
     285                 :            :  * Tries to match a GPIO pin number to the ranges handled by a certain pin
     286                 :            :  * controller, return the range or NULL
     287                 :            :  */
     288                 :            : static struct pinctrl_gpio_range *
     289                 :          3 : pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
     290                 :            : {
     291                 :            :         struct pinctrl_gpio_range *range;
     292                 :            : 
     293                 :          3 :         mutex_lock(&pctldev->mutex);
     294                 :            :         /* Loop over the ranges */
     295                 :          3 :         list_for_each_entry(range, &pctldev->gpio_ranges, node) {
     296                 :            :                 /* Check if we're in the valid range */
     297                 :          3 :                 if (gpio >= range->base &&
     298                 :          3 :                     gpio < range->base + range->npins) {
     299                 :          3 :                         mutex_unlock(&pctldev->mutex);
     300                 :          3 :                         return range;
     301                 :            :                 }
     302                 :            :         }
     303                 :          0 :         mutex_unlock(&pctldev->mutex);
     304                 :          0 :         return NULL;
     305                 :            : }
     306                 :            : 
     307                 :            : /**
     308                 :            :  * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
     309                 :            :  * the same GPIO chip are in range
     310                 :            :  * @gpio: gpio pin to check taken from the global GPIO pin space
     311                 :            :  *
     312                 :            :  * This function is complement of pinctrl_match_gpio_range(). If the return
     313                 :            :  * value of pinctrl_match_gpio_range() is NULL, this function could be used
     314                 :            :  * to check whether pinctrl device is ready or not. Maybe some GPIO pins
     315                 :            :  * of the same GPIO chip don't have back-end pinctrl interface.
     316                 :            :  * If the return value is true, it means that pinctrl device is ready & the
     317                 :            :  * certain GPIO pin doesn't have back-end pinctrl device. If the return value
     318                 :            :  * is false, it means that pinctrl device may not be ready.
     319                 :            :  */
     320                 :            : #ifdef CONFIG_GPIOLIB
     321                 :          0 : static bool pinctrl_ready_for_gpio_range(unsigned gpio)
     322                 :            : {
     323                 :            :         struct pinctrl_dev *pctldev;
     324                 :            :         struct pinctrl_gpio_range *range = NULL;
     325                 :            :         struct gpio_chip *chip = gpio_to_chip(gpio);
     326                 :            : 
     327                 :          0 :         if (WARN(!chip, "no gpio_chip for gpio%i?", gpio))
     328                 :            :                 return false;
     329                 :            : 
     330                 :          0 :         mutex_lock(&pinctrldev_list_mutex);
     331                 :            : 
     332                 :            :         /* Loop over the pin controllers */
     333                 :          0 :         list_for_each_entry(pctldev, &pinctrldev_list, node) {
     334                 :            :                 /* Loop over the ranges */
     335                 :          0 :                 mutex_lock(&pctldev->mutex);
     336                 :          0 :                 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
     337                 :            :                         /* Check if any gpio range overlapped with gpio chip */
     338                 :          0 :                         if (range->base + range->npins - 1 < chip->base ||
     339                 :          0 :                             range->base > chip->base + chip->ngpio - 1)
     340                 :          0 :                                 continue;
     341                 :          0 :                         mutex_unlock(&pctldev->mutex);
     342                 :          0 :                         mutex_unlock(&pinctrldev_list_mutex);
     343                 :          0 :                         return true;
     344                 :            :                 }
     345                 :          0 :                 mutex_unlock(&pctldev->mutex);
     346                 :            :         }
     347                 :            : 
     348                 :          0 :         mutex_unlock(&pinctrldev_list_mutex);
     349                 :            : 
     350                 :          0 :         return false;
     351                 :            : }
     352                 :            : #else
     353                 :            : static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
     354                 :            : #endif
     355                 :            : 
     356                 :            : /**
     357                 :            :  * pinctrl_get_device_gpio_range() - find device for GPIO range
     358                 :            :  * @gpio: the pin to locate the pin controller for
     359                 :            :  * @outdev: the pin control device if found
     360                 :            :  * @outrange: the GPIO range if found
     361                 :            :  *
     362                 :            :  * Find the pin controller handling a certain GPIO pin from the pinspace of
     363                 :            :  * the GPIO subsystem, return the device and the matching GPIO range. Returns
     364                 :            :  * -EPROBE_DEFER if the GPIO range could not be found in any device since it
     365                 :            :  * may still have not been registered.
     366                 :            :  */
     367                 :          3 : static int pinctrl_get_device_gpio_range(unsigned gpio,
     368                 :            :                                          struct pinctrl_dev **outdev,
     369                 :            :                                          struct pinctrl_gpio_range **outrange)
     370                 :            : {
     371                 :            :         struct pinctrl_dev *pctldev;
     372                 :            : 
     373                 :          3 :         mutex_lock(&pinctrldev_list_mutex);
     374                 :            : 
     375                 :            :         /* Loop over the pin controllers */
     376                 :          3 :         list_for_each_entry(pctldev, &pinctrldev_list, node) {
     377                 :            :                 struct pinctrl_gpio_range *range;
     378                 :            : 
     379                 :          3 :                 range = pinctrl_match_gpio_range(pctldev, gpio);
     380                 :          3 :                 if (range) {
     381                 :          3 :                         *outdev = pctldev;
     382                 :          3 :                         *outrange = range;
     383                 :          3 :                         mutex_unlock(&pinctrldev_list_mutex);
     384                 :          3 :                         return 0;
     385                 :            :                 }
     386                 :            :         }
     387                 :            : 
     388                 :          0 :         mutex_unlock(&pinctrldev_list_mutex);
     389                 :            : 
     390                 :          0 :         return -EPROBE_DEFER;
     391                 :            : }
     392                 :            : 
     393                 :            : /**
     394                 :            :  * pinctrl_add_gpio_range() - register a GPIO range for a controller
     395                 :            :  * @pctldev: pin controller device to add the range to
     396                 :            :  * @range: the GPIO range to add
     397                 :            :  *
     398                 :            :  * This adds a range of GPIOs to be handled by a certain pin controller. Call
     399                 :            :  * this to register handled ranges after registering your pin controller.
     400                 :            :  */
     401                 :          3 : void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
     402                 :            :                             struct pinctrl_gpio_range *range)
     403                 :            : {
     404                 :          3 :         mutex_lock(&pctldev->mutex);
     405                 :          3 :         list_add_tail(&range->node, &pctldev->gpio_ranges);
     406                 :          3 :         mutex_unlock(&pctldev->mutex);
     407                 :          3 : }
     408                 :            : EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
     409                 :            : 
     410                 :          0 : void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
     411                 :            :                              struct pinctrl_gpio_range *ranges,
     412                 :            :                              unsigned nranges)
     413                 :            : {
     414                 :            :         int i;
     415                 :            : 
     416                 :          0 :         for (i = 0; i < nranges; i++)
     417                 :          0 :                 pinctrl_add_gpio_range(pctldev, &ranges[i]);
     418                 :          0 : }
     419                 :            : EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
     420                 :            : 
     421                 :          0 : struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
     422                 :            :                 struct pinctrl_gpio_range *range)
     423                 :            : {
     424                 :            :         struct pinctrl_dev *pctldev;
     425                 :            : 
     426                 :          0 :         pctldev = get_pinctrl_dev_from_devname(devname);
     427                 :            : 
     428                 :            :         /*
     429                 :            :          * If we can't find this device, let's assume that is because
     430                 :            :          * it has not probed yet, so the driver trying to register this
     431                 :            :          * range need to defer probing.
     432                 :            :          */
     433                 :          0 :         if (!pctldev) {
     434                 :            :                 return ERR_PTR(-EPROBE_DEFER);
     435                 :            :         }
     436                 :          0 :         pinctrl_add_gpio_range(pctldev, range);
     437                 :            : 
     438                 :          0 :         return pctldev;
     439                 :            : }
     440                 :            : EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
     441                 :            : 
     442                 :          0 : int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
     443                 :            :                                 const unsigned **pins, unsigned *num_pins)
     444                 :            : {
     445                 :          0 :         const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
     446                 :            :         int gs;
     447                 :            : 
     448                 :          0 :         if (!pctlops->get_group_pins)
     449                 :            :                 return -EINVAL;
     450                 :            : 
     451                 :          0 :         gs = pinctrl_get_group_selector(pctldev, pin_group);
     452                 :          0 :         if (gs < 0)
     453                 :            :                 return gs;
     454                 :            : 
     455                 :          0 :         return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
     456                 :            : }
     457                 :            : EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
     458                 :            : 
     459                 :            : struct pinctrl_gpio_range *
     460                 :          0 : pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
     461                 :            :                                         unsigned int pin)
     462                 :            : {
     463                 :            :         struct pinctrl_gpio_range *range;
     464                 :            : 
     465                 :            :         /* Loop over the ranges */
     466                 :          0 :         list_for_each_entry(range, &pctldev->gpio_ranges, node) {
     467                 :            :                 /* Check if we're in the valid range */
     468                 :          0 :                 if (range->pins) {
     469                 :            :                         int a;
     470                 :          0 :                         for (a = 0; a < range->npins; a++) {
     471                 :          0 :                                 if (range->pins[a] == pin)
     472                 :          0 :                                         return range;
     473                 :            :                         }
     474                 :          0 :                 } else if (pin >= range->pin_base &&
     475                 :          0 :                            pin < range->pin_base + range->npins)
     476                 :          0 :                         return range;
     477                 :            :         }
     478                 :            : 
     479                 :            :         return NULL;
     480                 :            : }
     481                 :            : EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock);
     482                 :            : 
     483                 :            : /**
     484                 :            :  * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
     485                 :            :  * @pctldev: the pin controller device to look in
     486                 :            :  * @pin: a controller-local number to find the range for
     487                 :            :  */
     488                 :            : struct pinctrl_gpio_range *
     489                 :          0 : pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
     490                 :            :                                  unsigned int pin)
     491                 :            : {
     492                 :            :         struct pinctrl_gpio_range *range;
     493                 :            : 
     494                 :          0 :         mutex_lock(&pctldev->mutex);
     495                 :          0 :         range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
     496                 :          0 :         mutex_unlock(&pctldev->mutex);
     497                 :            : 
     498                 :          0 :         return range;
     499                 :            : }
     500                 :            : EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
     501                 :            : 
     502                 :            : /**
     503                 :            :  * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller
     504                 :            :  * @pctldev: pin controller device to remove the range from
     505                 :            :  * @range: the GPIO range to remove
     506                 :            :  */
     507                 :          0 : void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
     508                 :            :                                struct pinctrl_gpio_range *range)
     509                 :            : {
     510                 :          0 :         mutex_lock(&pctldev->mutex);
     511                 :            :         list_del(&range->node);
     512                 :          0 :         mutex_unlock(&pctldev->mutex);
     513                 :          0 : }
     514                 :            : EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
     515                 :            : 
     516                 :            : #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
     517                 :            : 
     518                 :            : /**
     519                 :            :  * pinctrl_generic_get_group_count() - returns the number of pin groups
     520                 :            :  * @pctldev: pin controller device
     521                 :            :  */
     522                 :            : int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev)
     523                 :            : {
     524                 :            :         return pctldev->num_groups;
     525                 :            : }
     526                 :            : EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count);
     527                 :            : 
     528                 :            : /**
     529                 :            :  * pinctrl_generic_get_group_name() - returns the name of a pin group
     530                 :            :  * @pctldev: pin controller device
     531                 :            :  * @selector: group number
     532                 :            :  */
     533                 :            : const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
     534                 :            :                                            unsigned int selector)
     535                 :            : {
     536                 :            :         struct group_desc *group;
     537                 :            : 
     538                 :            :         group = radix_tree_lookup(&pctldev->pin_group_tree,
     539                 :            :                                   selector);
     540                 :            :         if (!group)
     541                 :            :                 return NULL;
     542                 :            : 
     543                 :            :         return group->name;
     544                 :            : }
     545                 :            : EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name);
     546                 :            : 
     547                 :            : /**
     548                 :            :  * pinctrl_generic_get_group_pins() - gets the pin group pins
     549                 :            :  * @pctldev: pin controller device
     550                 :            :  * @selector: group number
     551                 :            :  * @pins: pins in the group
     552                 :            :  * @num_pins: number of pins in the group
     553                 :            :  */
     554                 :            : int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
     555                 :            :                                    unsigned int selector,
     556                 :            :                                    const unsigned int **pins,
     557                 :            :                                    unsigned int *num_pins)
     558                 :            : {
     559                 :            :         struct group_desc *group;
     560                 :            : 
     561                 :            :         group = radix_tree_lookup(&pctldev->pin_group_tree,
     562                 :            :                                   selector);
     563                 :            :         if (!group) {
     564                 :            :                 dev_err(pctldev->dev, "%s could not find pingroup%i\n",
     565                 :            :                         __func__, selector);
     566                 :            :                 return -EINVAL;
     567                 :            :         }
     568                 :            : 
     569                 :            :         *pins = group->pins;
     570                 :            :         *num_pins = group->num_pins;
     571                 :            : 
     572                 :            :         return 0;
     573                 :            : }
     574                 :            : EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins);
     575                 :            : 
     576                 :            : /**
     577                 :            :  * pinctrl_generic_get_group() - returns a pin group based on the number
     578                 :            :  * @pctldev: pin controller device
     579                 :            :  * @gselector: group number
     580                 :            :  */
     581                 :            : struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
     582                 :            :                                              unsigned int selector)
     583                 :            : {
     584                 :            :         struct group_desc *group;
     585                 :            : 
     586                 :            :         group = radix_tree_lookup(&pctldev->pin_group_tree,
     587                 :            :                                   selector);
     588                 :            :         if (!group)
     589                 :            :                 return NULL;
     590                 :            : 
     591                 :            :         return group;
     592                 :            : }
     593                 :            : EXPORT_SYMBOL_GPL(pinctrl_generic_get_group);
     594                 :            : 
     595                 :            : static int pinctrl_generic_group_name_to_selector(struct pinctrl_dev *pctldev,
     596                 :            :                                                   const char *function)
     597                 :            : {
     598                 :            :         const struct pinctrl_ops *ops = pctldev->desc->pctlops;
     599                 :            :         int ngroups = ops->get_groups_count(pctldev);
     600                 :            :         int selector = 0;
     601                 :            : 
     602                 :            :         /* See if this pctldev has this group */
     603                 :            :         while (selector < ngroups) {
     604                 :            :                 const char *gname = ops->get_group_name(pctldev, selector);
     605                 :            : 
     606                 :            :                 if (gname && !strcmp(function, gname))
     607                 :            :                         return selector;
     608                 :            : 
     609                 :            :                 selector++;
     610                 :            :         }
     611                 :            : 
     612                 :            :         return -EINVAL;
     613                 :            : }
     614                 :            : 
     615                 :            : /**
     616                 :            :  * pinctrl_generic_add_group() - adds a new pin group
     617                 :            :  * @pctldev: pin controller device
     618                 :            :  * @name: name of the pin group
     619                 :            :  * @pins: pins in the pin group
     620                 :            :  * @num_pins: number of pins in the pin group
     621                 :            :  * @data: pin controller driver specific data
     622                 :            :  *
     623                 :            :  * Note that the caller must take care of locking.
     624                 :            :  */
     625                 :            : int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
     626                 :            :                               int *pins, int num_pins, void *data)
     627                 :            : {
     628                 :            :         struct group_desc *group;
     629                 :            :         int selector;
     630                 :            : 
     631                 :            :         if (!name)
     632                 :            :                 return -EINVAL;
     633                 :            : 
     634                 :            :         selector = pinctrl_generic_group_name_to_selector(pctldev, name);
     635                 :            :         if (selector >= 0)
     636                 :            :                 return selector;
     637                 :            : 
     638                 :            :         selector = pctldev->num_groups;
     639                 :            : 
     640                 :            :         group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL);
     641                 :            :         if (!group)
     642                 :            :                 return -ENOMEM;
     643                 :            : 
     644                 :            :         group->name = name;
     645                 :            :         group->pins = pins;
     646                 :            :         group->num_pins = num_pins;
     647                 :            :         group->data = data;
     648                 :            : 
     649                 :            :         radix_tree_insert(&pctldev->pin_group_tree, selector, group);
     650                 :            : 
     651                 :            :         pctldev->num_groups++;
     652                 :            : 
     653                 :            :         return selector;
     654                 :            : }
     655                 :            : EXPORT_SYMBOL_GPL(pinctrl_generic_add_group);
     656                 :            : 
     657                 :            : /**
     658                 :            :  * pinctrl_generic_remove_group() - removes a numbered pin group
     659                 :            :  * @pctldev: pin controller device
     660                 :            :  * @selector: group number
     661                 :            :  *
     662                 :            :  * Note that the caller must take care of locking.
     663                 :            :  */
     664                 :            : int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
     665                 :            :                                  unsigned int selector)
     666                 :            : {
     667                 :            :         struct group_desc *group;
     668                 :            : 
     669                 :            :         group = radix_tree_lookup(&pctldev->pin_group_tree,
     670                 :            :                                   selector);
     671                 :            :         if (!group)
     672                 :            :                 return -ENOENT;
     673                 :            : 
     674                 :            :         radix_tree_delete(&pctldev->pin_group_tree, selector);
     675                 :            :         devm_kfree(pctldev->dev, group);
     676                 :            : 
     677                 :            :         pctldev->num_groups--;
     678                 :            : 
     679                 :            :         return 0;
     680                 :            : }
     681                 :            : EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group);
     682                 :            : 
     683                 :            : /**
     684                 :            :  * pinctrl_generic_free_groups() - removes all pin groups
     685                 :            :  * @pctldev: pin controller device
     686                 :            :  *
     687                 :            :  * Note that the caller must take care of locking. The pinctrl groups
     688                 :            :  * are allocated with devm_kzalloc() so no need to free them here.
     689                 :            :  */
     690                 :            : static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
     691                 :            : {
     692                 :            :         struct radix_tree_iter iter;
     693                 :            :         void __rcu **slot;
     694                 :            : 
     695                 :            :         radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0)
     696                 :            :                 radix_tree_delete(&pctldev->pin_group_tree, iter.index);
     697                 :            : 
     698                 :            :         pctldev->num_groups = 0;
     699                 :            : }
     700                 :            : 
     701                 :            : #else
     702                 :            : static inline void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
     703                 :            : {
     704                 :            : }
     705                 :            : #endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
     706                 :            : 
     707                 :            : /**
     708                 :            :  * pinctrl_get_group_selector() - returns the group selector for a group
     709                 :            :  * @pctldev: the pin controller handling the group
     710                 :            :  * @pin_group: the pin group to look up
     711                 :            :  */
     712                 :          3 : int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
     713                 :            :                                const char *pin_group)
     714                 :            : {
     715                 :          3 :         const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
     716                 :          3 :         unsigned ngroups = pctlops->get_groups_count(pctldev);
     717                 :            :         unsigned group_selector = 0;
     718                 :            : 
     719                 :          3 :         while (group_selector < ngroups) {
     720                 :          3 :                 const char *gname = pctlops->get_group_name(pctldev,
     721                 :            :                                                             group_selector);
     722                 :          3 :                 if (gname && !strcmp(gname, pin_group)) {
     723                 :            :                         dev_dbg(pctldev->dev,
     724                 :            :                                 "found group selector %u for %s\n",
     725                 :            :                                 group_selector,
     726                 :            :                                 pin_group);
     727                 :          3 :                         return group_selector;
     728                 :            :                 }
     729                 :            : 
     730                 :          3 :                 group_selector++;
     731                 :            :         }
     732                 :            : 
     733                 :          0 :         dev_err(pctldev->dev, "does not have pin group %s\n",
     734                 :            :                 pin_group);
     735                 :            : 
     736                 :          0 :         return -EINVAL;
     737                 :            : }
     738                 :            : 
     739                 :          0 : bool pinctrl_gpio_can_use_line(unsigned gpio)
     740                 :            : {
     741                 :            :         struct pinctrl_dev *pctldev;
     742                 :            :         struct pinctrl_gpio_range *range;
     743                 :            :         bool result;
     744                 :            :         int pin;
     745                 :            : 
     746                 :            :         /*
     747                 :            :          * Try to obtain GPIO range, if it fails
     748                 :            :          * we're probably dealing with GPIO driver
     749                 :            :          * without a backing pin controller - bail out.
     750                 :            :          */
     751                 :          0 :         if (pinctrl_get_device_gpio_range(gpio, &pctldev, &range))
     752                 :            :                 return true;
     753                 :            : 
     754                 :          0 :         mutex_lock(&pctldev->mutex);
     755                 :            : 
     756                 :            :         /* Convert to the pin controllers number space */
     757                 :          0 :         pin = gpio_to_pin(range, gpio);
     758                 :            : 
     759                 :          0 :         result = pinmux_can_be_used_for_gpio(pctldev, pin);
     760                 :            : 
     761                 :          0 :         mutex_unlock(&pctldev->mutex);
     762                 :            : 
     763                 :          0 :         return result;
     764                 :            : }
     765                 :            : EXPORT_SYMBOL_GPL(pinctrl_gpio_can_use_line);
     766                 :            : 
     767                 :            : /**
     768                 :            :  * pinctrl_gpio_request() - request a single pin to be used as GPIO
     769                 :            :  * @gpio: the GPIO pin number from the GPIO subsystem number space
     770                 :            :  *
     771                 :            :  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
     772                 :            :  * as part of their gpio_request() semantics, platforms and individual drivers
     773                 :            :  * shall *NOT* request GPIO pins to be muxed in.
     774                 :            :  */
     775                 :          3 : int pinctrl_gpio_request(unsigned gpio)
     776                 :            : {
     777                 :            :         struct pinctrl_dev *pctldev;
     778                 :            :         struct pinctrl_gpio_range *range;
     779                 :            :         int ret;
     780                 :            :         int pin;
     781                 :            : 
     782                 :          3 :         ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
     783                 :          3 :         if (ret) {
     784                 :          0 :                 if (pinctrl_ready_for_gpio_range(gpio))
     785                 :            :                         ret = 0;
     786                 :          0 :                 return ret;
     787                 :            :         }
     788                 :            : 
     789                 :          3 :         mutex_lock(&pctldev->mutex);
     790                 :            : 
     791                 :            :         /* Convert to the pin controllers number space */
     792                 :          3 :         pin = gpio_to_pin(range, gpio);
     793                 :            : 
     794                 :          3 :         ret = pinmux_request_gpio(pctldev, range, pin, gpio);
     795                 :            : 
     796                 :          3 :         mutex_unlock(&pctldev->mutex);
     797                 :            : 
     798                 :          3 :         return ret;
     799                 :            : }
     800                 :            : EXPORT_SYMBOL_GPL(pinctrl_gpio_request);
     801                 :            : 
     802                 :            : /**
     803                 :            :  * pinctrl_gpio_free() - free control on a single pin, currently used as GPIO
     804                 :            :  * @gpio: the GPIO pin number from the GPIO subsystem number space
     805                 :            :  *
     806                 :            :  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
     807                 :            :  * as part of their gpio_free() semantics, platforms and individual drivers
     808                 :            :  * shall *NOT* request GPIO pins to be muxed out.
     809                 :            :  */
     810                 :          0 : void pinctrl_gpio_free(unsigned gpio)
     811                 :            : {
     812                 :            :         struct pinctrl_dev *pctldev;
     813                 :            :         struct pinctrl_gpio_range *range;
     814                 :            :         int ret;
     815                 :            :         int pin;
     816                 :            : 
     817                 :          0 :         ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
     818                 :          0 :         if (ret) {
     819                 :          0 :                 return;
     820                 :            :         }
     821                 :          0 :         mutex_lock(&pctldev->mutex);
     822                 :            : 
     823                 :            :         /* Convert to the pin controllers number space */
     824                 :          0 :         pin = gpio_to_pin(range, gpio);
     825                 :            : 
     826                 :          0 :         pinmux_free_gpio(pctldev, pin, range);
     827                 :            : 
     828                 :          0 :         mutex_unlock(&pctldev->mutex);
     829                 :            : }
     830                 :            : EXPORT_SYMBOL_GPL(pinctrl_gpio_free);
     831                 :            : 
     832                 :          3 : static int pinctrl_gpio_direction(unsigned gpio, bool input)
     833                 :            : {
     834                 :            :         struct pinctrl_dev *pctldev;
     835                 :            :         struct pinctrl_gpio_range *range;
     836                 :            :         int ret;
     837                 :            :         int pin;
     838                 :            : 
     839                 :          3 :         ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
     840                 :          3 :         if (ret) {
     841                 :            :                 return ret;
     842                 :            :         }
     843                 :            : 
     844                 :          3 :         mutex_lock(&pctldev->mutex);
     845                 :            : 
     846                 :            :         /* Convert to the pin controllers number space */
     847                 :          3 :         pin = gpio_to_pin(range, gpio);
     848                 :          3 :         ret = pinmux_gpio_direction(pctldev, range, pin, input);
     849                 :            : 
     850                 :          3 :         mutex_unlock(&pctldev->mutex);
     851                 :            : 
     852                 :          3 :         return ret;
     853                 :            : }
     854                 :            : 
     855                 :            : /**
     856                 :            :  * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
     857                 :            :  * @gpio: the GPIO pin number from the GPIO subsystem number space
     858                 :            :  *
     859                 :            :  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
     860                 :            :  * as part of their gpio_direction_input() semantics, platforms and individual
     861                 :            :  * drivers shall *NOT* touch pin control GPIO calls.
     862                 :            :  */
     863                 :          3 : int pinctrl_gpio_direction_input(unsigned gpio)
     864                 :            : {
     865                 :          3 :         return pinctrl_gpio_direction(gpio, true);
     866                 :            : }
     867                 :            : EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
     868                 :            : 
     869                 :            : /**
     870                 :            :  * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
     871                 :            :  * @gpio: the GPIO pin number from the GPIO subsystem number space
     872                 :            :  *
     873                 :            :  * This function should *ONLY* be used from gpiolib-based GPIO drivers,
     874                 :            :  * as part of their gpio_direction_output() semantics, platforms and individual
     875                 :            :  * drivers shall *NOT* touch pin control GPIO calls.
     876                 :            :  */
     877                 :          3 : int pinctrl_gpio_direction_output(unsigned gpio)
     878                 :            : {
     879                 :          3 :         return pinctrl_gpio_direction(gpio, false);
     880                 :            : }
     881                 :            : EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
     882                 :            : 
     883                 :            : /**
     884                 :            :  * pinctrl_gpio_set_config() - Apply config to given GPIO pin
     885                 :            :  * @gpio: the GPIO pin number from the GPIO subsystem number space
     886                 :            :  * @config: the configuration to apply to the GPIO
     887                 :            :  *
     888                 :            :  * This function should *ONLY* be used from gpiolib-based GPIO drivers, if
     889                 :            :  * they need to call the underlying pin controller to change GPIO config
     890                 :            :  * (for example set debounce time).
     891                 :            :  */
     892                 :          3 : int pinctrl_gpio_set_config(unsigned gpio, unsigned long config)
     893                 :            : {
     894                 :          3 :         unsigned long configs[] = { config };
     895                 :            :         struct pinctrl_gpio_range *range;
     896                 :            :         struct pinctrl_dev *pctldev;
     897                 :            :         int ret, pin;
     898                 :            : 
     899                 :          3 :         ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
     900                 :          3 :         if (ret)
     901                 :            :                 return ret;
     902                 :            : 
     903                 :          3 :         mutex_lock(&pctldev->mutex);
     904                 :          3 :         pin = gpio_to_pin(range, gpio);
     905                 :          3 :         ret = pinconf_set_config(pctldev, pin, configs, ARRAY_SIZE(configs));
     906                 :          3 :         mutex_unlock(&pctldev->mutex);
     907                 :            : 
     908                 :          3 :         return ret;
     909                 :            : }
     910                 :            : EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config);
     911                 :            : 
     912                 :          3 : static struct pinctrl_state *find_state(struct pinctrl *p,
     913                 :            :                                         const char *name)
     914                 :            : {
     915                 :            :         struct pinctrl_state *state;
     916                 :            : 
     917                 :          3 :         list_for_each_entry(state, &p->states, node)
     918                 :          3 :                 if (!strcmp(state->name, name))
     919                 :          3 :                         return state;
     920                 :            : 
     921                 :            :         return NULL;
     922                 :            : }
     923                 :            : 
     924                 :          3 : static struct pinctrl_state *create_state(struct pinctrl *p,
     925                 :            :                                           const char *name)
     926                 :            : {
     927                 :            :         struct pinctrl_state *state;
     928                 :            : 
     929                 :          3 :         state = kzalloc(sizeof(*state), GFP_KERNEL);
     930                 :          3 :         if (!state)
     931                 :            :                 return ERR_PTR(-ENOMEM);
     932                 :            : 
     933                 :          3 :         state->name = name;
     934                 :          3 :         INIT_LIST_HEAD(&state->settings);
     935                 :            : 
     936                 :          3 :         list_add_tail(&state->node, &p->states);
     937                 :            : 
     938                 :          3 :         return state;
     939                 :            : }
     940                 :            : 
     941                 :          3 : static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev,
     942                 :            :                        const struct pinctrl_map *map)
     943                 :            : {
     944                 :            :         struct pinctrl_state *state;
     945                 :            :         struct pinctrl_setting *setting;
     946                 :            :         int ret;
     947                 :            : 
     948                 :          3 :         state = find_state(p, map->name);
     949                 :          3 :         if (!state)
     950                 :          3 :                 state = create_state(p, map->name);
     951                 :          3 :         if (IS_ERR(state))
     952                 :          0 :                 return PTR_ERR(state);
     953                 :            : 
     954                 :          3 :         if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
     955                 :            :                 return 0;
     956                 :            : 
     957                 :          3 :         setting = kzalloc(sizeof(*setting), GFP_KERNEL);
     958                 :          3 :         if (!setting)
     959                 :            :                 return -ENOMEM;
     960                 :            : 
     961                 :          3 :         setting->type = map->type;
     962                 :            : 
     963                 :          3 :         if (pctldev)
     964                 :          0 :                 setting->pctldev = pctldev;
     965                 :            :         else
     966                 :          3 :                 setting->pctldev =
     967                 :          3 :                         get_pinctrl_dev_from_devname(map->ctrl_dev_name);
     968                 :          3 :         if (!setting->pctldev) {
     969                 :          0 :                 kfree(setting);
     970                 :            :                 /* Do not defer probing of hogs (circular loop) */
     971                 :          0 :                 if (!strcmp(map->ctrl_dev_name, map->dev_name))
     972                 :            :                         return -ENODEV;
     973                 :            :                 /*
     974                 :            :                  * OK let us guess that the driver is not there yet, and
     975                 :            :                  * let's defer obtaining this pinctrl handle to later...
     976                 :            :                  */
     977                 :          0 :                 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
     978                 :            :                         map->ctrl_dev_name);
     979                 :          0 :                 return -EPROBE_DEFER;
     980                 :            :         }
     981                 :            : 
     982                 :          3 :         setting->dev_name = map->dev_name;
     983                 :            : 
     984                 :          3 :         switch (map->type) {
     985                 :            :         case PIN_MAP_TYPE_MUX_GROUP:
     986                 :          3 :                 ret = pinmux_map_to_setting(map, setting);
     987                 :          3 :                 break;
     988                 :            :         case PIN_MAP_TYPE_CONFIGS_PIN:
     989                 :            :         case PIN_MAP_TYPE_CONFIGS_GROUP:
     990                 :          0 :                 ret = pinconf_map_to_setting(map, setting);
     991                 :          0 :                 break;
     992                 :            :         default:
     993                 :            :                 ret = -EINVAL;
     994                 :            :                 break;
     995                 :            :         }
     996                 :          3 :         if (ret < 0) {
     997                 :          0 :                 kfree(setting);
     998                 :          0 :                 return ret;
     999                 :            :         }
    1000                 :            : 
    1001                 :          3 :         list_add_tail(&setting->node, &state->settings);
    1002                 :            : 
    1003                 :          3 :         return 0;
    1004                 :            : }
    1005                 :            : 
    1006                 :          3 : static struct pinctrl *find_pinctrl(struct device *dev)
    1007                 :            : {
    1008                 :            :         struct pinctrl *p;
    1009                 :            : 
    1010                 :          3 :         mutex_lock(&pinctrl_list_mutex);
    1011                 :          3 :         list_for_each_entry(p, &pinctrl_list, node)
    1012                 :          3 :                 if (p->dev == dev) {
    1013                 :          0 :                         mutex_unlock(&pinctrl_list_mutex);
    1014                 :          0 :                         return p;
    1015                 :            :                 }
    1016                 :            : 
    1017                 :          3 :         mutex_unlock(&pinctrl_list_mutex);
    1018                 :          3 :         return NULL;
    1019                 :            : }
    1020                 :            : 
    1021                 :            : static void pinctrl_free(struct pinctrl *p, bool inlist);
    1022                 :            : 
    1023                 :          3 : static struct pinctrl *create_pinctrl(struct device *dev,
    1024                 :            :                                       struct pinctrl_dev *pctldev)
    1025                 :            : {
    1026                 :            :         struct pinctrl *p;
    1027                 :            :         const char *devname;
    1028                 :            :         struct pinctrl_maps *maps_node;
    1029                 :            :         int i;
    1030                 :            :         const struct pinctrl_map *map;
    1031                 :            :         int ret;
    1032                 :            : 
    1033                 :            :         /*
    1034                 :            :          * create the state cookie holder struct pinctrl for each
    1035                 :            :          * mapping, this is what consumers will get when requesting
    1036                 :            :          * a pin control handle with pinctrl_get()
    1037                 :            :          */
    1038                 :          3 :         p = kzalloc(sizeof(*p), GFP_KERNEL);
    1039                 :          3 :         if (!p)
    1040                 :            :                 return ERR_PTR(-ENOMEM);
    1041                 :          3 :         p->dev = dev;
    1042                 :          3 :         INIT_LIST_HEAD(&p->states);
    1043                 :          3 :         INIT_LIST_HEAD(&p->dt_maps);
    1044                 :            : 
    1045                 :          3 :         ret = pinctrl_dt_to_map(p, pctldev);
    1046                 :          3 :         if (ret < 0) {
    1047                 :          3 :                 kfree(p);
    1048                 :          3 :                 return ERR_PTR(ret);
    1049                 :            :         }
    1050                 :            : 
    1051                 :            :         devname = dev_name(dev);
    1052                 :            : 
    1053                 :          3 :         mutex_lock(&pinctrl_maps_mutex);
    1054                 :            :         /* Iterate over the pin control maps to locate the right ones */
    1055                 :          3 :         for_each_maps(maps_node, i, map) {
    1056                 :            :                 /* Map must be for this device */
    1057                 :          3 :                 if (strcmp(map->dev_name, devname))
    1058                 :          3 :                         continue;
    1059                 :            :                 /*
    1060                 :            :                  * If pctldev is not null, we are claiming hog for it,
    1061                 :            :                  * that means, setting that is served by pctldev by itself.
    1062                 :            :                  *
    1063                 :            :                  * Thus we must skip map that is for this device but is served
    1064                 :            :                  * by other device.
    1065                 :            :                  */
    1066                 :          3 :                 if (pctldev &&
    1067                 :          0 :                     strcmp(dev_name(pctldev->dev), map->ctrl_dev_name))
    1068                 :          0 :                         continue;
    1069                 :            : 
    1070                 :          3 :                 ret = add_setting(p, pctldev, map);
    1071                 :            :                 /*
    1072                 :            :                  * At this point the adding of a setting may:
    1073                 :            :                  *
    1074                 :            :                  * - Defer, if the pinctrl device is not yet available
    1075                 :            :                  * - Fail, if the pinctrl device is not yet available,
    1076                 :            :                  *   AND the setting is a hog. We cannot defer that, since
    1077                 :            :                  *   the hog will kick in immediately after the device
    1078                 :            :                  *   is registered.
    1079                 :            :                  *
    1080                 :            :                  * If the error returned was not -EPROBE_DEFER then we
    1081                 :            :                  * accumulate the errors to see if we end up with
    1082                 :            :                  * an -EPROBE_DEFER later, as that is the worst case.
    1083                 :            :                  */
    1084                 :          3 :                 if (ret == -EPROBE_DEFER) {
    1085                 :          0 :                         pinctrl_free(p, false);
    1086                 :          0 :                         mutex_unlock(&pinctrl_maps_mutex);
    1087                 :          0 :                         return ERR_PTR(ret);
    1088                 :            :                 }
    1089                 :            :         }
    1090                 :          3 :         mutex_unlock(&pinctrl_maps_mutex);
    1091                 :            : 
    1092                 :          3 :         if (ret < 0) {
    1093                 :            :                 /* If some other error than deferral occurred, return here */
    1094                 :          0 :                 pinctrl_free(p, false);
    1095                 :          0 :                 return ERR_PTR(ret);
    1096                 :            :         }
    1097                 :            : 
    1098                 :            :         kref_init(&p->users);
    1099                 :            : 
    1100                 :            :         /* Add the pinctrl handle to the global list */
    1101                 :          3 :         mutex_lock(&pinctrl_list_mutex);
    1102                 :          3 :         list_add_tail(&p->node, &pinctrl_list);
    1103                 :          3 :         mutex_unlock(&pinctrl_list_mutex);
    1104                 :            : 
    1105                 :          3 :         return p;
    1106                 :            : }
    1107                 :            : 
    1108                 :            : /**
    1109                 :            :  * pinctrl_get() - retrieves the pinctrl handle for a device
    1110                 :            :  * @dev: the device to obtain the handle for
    1111                 :            :  */
    1112                 :          3 : struct pinctrl *pinctrl_get(struct device *dev)
    1113                 :            : {
    1114                 :            :         struct pinctrl *p;
    1115                 :            : 
    1116                 :          3 :         if (WARN_ON(!dev))
    1117                 :            :                 return ERR_PTR(-EINVAL);
    1118                 :            : 
    1119                 :            :         /*
    1120                 :            :          * See if somebody else (such as the device core) has already
    1121                 :            :          * obtained a handle to the pinctrl for this device. In that case,
    1122                 :            :          * return another pointer to it.
    1123                 :            :          */
    1124                 :          3 :         p = find_pinctrl(dev);
    1125                 :          3 :         if (p) {
    1126                 :            :                 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
    1127                 :            :                 kref_get(&p->users);
    1128                 :          0 :                 return p;
    1129                 :            :         }
    1130                 :            : 
    1131                 :          3 :         return create_pinctrl(dev, NULL);
    1132                 :            : }
    1133                 :            : EXPORT_SYMBOL_GPL(pinctrl_get);
    1134                 :            : 
    1135                 :          3 : static void pinctrl_free_setting(bool disable_setting,
    1136                 :            :                                  struct pinctrl_setting *setting)
    1137                 :            : {
    1138                 :          3 :         switch (setting->type) {
    1139                 :            :         case PIN_MAP_TYPE_MUX_GROUP:
    1140                 :          3 :                 if (disable_setting)
    1141                 :          3 :                         pinmux_disable_setting(setting);
    1142                 :          3 :                 pinmux_free_setting(setting);
    1143                 :          3 :                 break;
    1144                 :            :         case PIN_MAP_TYPE_CONFIGS_PIN:
    1145                 :            :         case PIN_MAP_TYPE_CONFIGS_GROUP:
    1146                 :          0 :                 pinconf_free_setting(setting);
    1147                 :          0 :                 break;
    1148                 :            :         default:
    1149                 :            :                 break;
    1150                 :            :         }
    1151                 :          3 : }
    1152                 :            : 
    1153                 :          3 : static void pinctrl_free(struct pinctrl *p, bool inlist)
    1154                 :            : {
    1155                 :            :         struct pinctrl_state *state, *n1;
    1156                 :            :         struct pinctrl_setting *setting, *n2;
    1157                 :            : 
    1158                 :          3 :         mutex_lock(&pinctrl_list_mutex);
    1159                 :          3 :         list_for_each_entry_safe(state, n1, &p->states, node) {
    1160                 :          3 :                 list_for_each_entry_safe(setting, n2, &state->settings, node) {
    1161                 :          3 :                         pinctrl_free_setting(state == p->state, setting);
    1162                 :            :                         list_del(&setting->node);
    1163                 :          3 :                         kfree(setting);
    1164                 :            :                 }
    1165                 :            :                 list_del(&state->node);
    1166                 :          3 :                 kfree(state);
    1167                 :            :         }
    1168                 :            : 
    1169                 :          3 :         pinctrl_dt_free_maps(p);
    1170                 :            : 
    1171                 :          3 :         if (inlist)
    1172                 :            :                 list_del(&p->node);
    1173                 :          3 :         kfree(p);
    1174                 :          3 :         mutex_unlock(&pinctrl_list_mutex);
    1175                 :          3 : }
    1176                 :            : 
    1177                 :            : /**
    1178                 :            :  * pinctrl_release() - release the pinctrl handle
    1179                 :            :  * @kref: the kref in the pinctrl being released
    1180                 :            :  */
    1181                 :          3 : static void pinctrl_release(struct kref *kref)
    1182                 :            : {
    1183                 :          3 :         struct pinctrl *p = container_of(kref, struct pinctrl, users);
    1184                 :            : 
    1185                 :          3 :         pinctrl_free(p, true);
    1186                 :          3 : }
    1187                 :            : 
    1188                 :            : /**
    1189                 :            :  * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
    1190                 :            :  * @p: the pinctrl handle to release
    1191                 :            :  */
    1192                 :          0 : void pinctrl_put(struct pinctrl *p)
    1193                 :            : {
    1194                 :          3 :         kref_put(&p->users, pinctrl_release);
    1195                 :          0 : }
    1196                 :            : EXPORT_SYMBOL_GPL(pinctrl_put);
    1197                 :            : 
    1198                 :            : /**
    1199                 :            :  * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
    1200                 :            :  * @p: the pinctrl handle to retrieve the state from
    1201                 :            :  * @name: the state name to retrieve
    1202                 :            :  */
    1203                 :          3 : struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
    1204                 :            :                                                  const char *name)
    1205                 :            : {
    1206                 :            :         struct pinctrl_state *state;
    1207                 :            : 
    1208                 :          3 :         state = find_state(p, name);
    1209                 :          3 :         if (!state) {
    1210                 :          3 :                 if (pinctrl_dummy_state) {
    1211                 :            :                         /* create dummy state */
    1212                 :            :                         dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
    1213                 :            :                                 name);
    1214                 :          0 :                         state = create_state(p, name);
    1215                 :            :                 } else
    1216                 :            :                         state = ERR_PTR(-ENODEV);
    1217                 :            :         }
    1218                 :            : 
    1219                 :          3 :         return state;
    1220                 :            : }
    1221                 :            : EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
    1222                 :            : 
    1223                 :            : static void pinctrl_link_add(struct pinctrl_dev *pctldev,
    1224                 :            :                              struct device *consumer)
    1225                 :            : {
    1226                 :          3 :         if (pctldev->desc->link_consumers)
    1227                 :          0 :                 device_link_add(consumer, pctldev->dev,
    1228                 :            :                                 DL_FLAG_PM_RUNTIME |
    1229                 :            :                                 DL_FLAG_AUTOREMOVE_CONSUMER);
    1230                 :            : }
    1231                 :            : 
    1232                 :            : /**
    1233                 :            :  * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
    1234                 :            :  * @p: the pinctrl handle for the device that requests configuration
    1235                 :            :  * @state: the state handle to select/activate/program
    1236                 :            :  */
    1237                 :          3 : static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
    1238                 :            : {
    1239                 :            :         struct pinctrl_setting *setting, *setting2;
    1240                 :          3 :         struct pinctrl_state *old_state = p->state;
    1241                 :            :         int ret;
    1242                 :            : 
    1243                 :          3 :         if (p->state) {
    1244                 :            :                 /*
    1245                 :            :                  * For each pinmux setting in the old state, forget SW's record
    1246                 :            :                  * of mux owner for that pingroup. Any pingroups which are
    1247                 :            :                  * still owned by the new state will be re-acquired by the call
    1248                 :            :                  * to pinmux_enable_setting() in the loop below.
    1249                 :            :                  */
    1250                 :          0 :                 list_for_each_entry(setting, &p->state->settings, node) {
    1251                 :          0 :                         if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
    1252                 :          0 :                                 continue;
    1253                 :          0 :                         pinmux_disable_setting(setting);
    1254                 :            :                 }
    1255                 :            :         }
    1256                 :            : 
    1257                 :          3 :         p->state = NULL;
    1258                 :            : 
    1259                 :            :         /* Apply all the settings for the new state */
    1260                 :          3 :         list_for_each_entry(setting, &state->settings, node) {
    1261                 :          3 :                 switch (setting->type) {
    1262                 :            :                 case PIN_MAP_TYPE_MUX_GROUP:
    1263                 :          3 :                         ret = pinmux_enable_setting(setting);
    1264                 :          3 :                         break;
    1265                 :            :                 case PIN_MAP_TYPE_CONFIGS_PIN:
    1266                 :            :                 case PIN_MAP_TYPE_CONFIGS_GROUP:
    1267                 :          0 :                         ret = pinconf_apply_setting(setting);
    1268                 :          0 :                         break;
    1269                 :            :                 default:
    1270                 :            :                         ret = -EINVAL;
    1271                 :            :                         break;
    1272                 :            :                 }
    1273                 :            : 
    1274                 :          3 :                 if (ret < 0) {
    1275                 :            :                         goto unapply_new_state;
    1276                 :            :                 }
    1277                 :            : 
    1278                 :            :                 /* Do not link hogs (circular dependency) */
    1279                 :          3 :                 if (p != setting->pctldev->p)
    1280                 :          3 :                         pinctrl_link_add(setting->pctldev, p->dev);
    1281                 :            :         }
    1282                 :            : 
    1283                 :          3 :         p->state = state;
    1284                 :            : 
    1285                 :          3 :         return 0;
    1286                 :            : 
    1287                 :            : unapply_new_state:
    1288                 :          0 :         dev_err(p->dev, "Error applying setting, reverse things back\n");
    1289                 :            : 
    1290                 :          0 :         list_for_each_entry(setting2, &state->settings, node) {
    1291                 :          0 :                 if (&setting2->node == &setting->node)
    1292                 :            :                         break;
    1293                 :            :                 /*
    1294                 :            :                  * All we can do here is pinmux_disable_setting.
    1295                 :            :                  * That means that some pins are muxed differently now
    1296                 :            :                  * than they were before applying the setting (We can't
    1297                 :            :                  * "unmux a pin"!), but it's not a big deal since the pins
    1298                 :            :                  * are free to be muxed by another apply_setting.
    1299                 :            :                  */
    1300                 :          0 :                 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
    1301                 :          0 :                         pinmux_disable_setting(setting2);
    1302                 :            :         }
    1303                 :            : 
    1304                 :            :         /* There's no infinite recursive loop here because p->state is NULL */
    1305                 :          0 :         if (old_state)
    1306                 :          0 :                 pinctrl_select_state(p, old_state);
    1307                 :            : 
    1308                 :          0 :         return ret;
    1309                 :            : }
    1310                 :            : 
    1311                 :            : /**
    1312                 :            :  * pinctrl_select_state() - select/activate/program a pinctrl state to HW
    1313                 :            :  * @p: the pinctrl handle for the device that requests configuration
    1314                 :            :  * @state: the state handle to select/activate/program
    1315                 :            :  */
    1316                 :          3 : int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
    1317                 :            : {
    1318                 :          3 :         if (p->state == state)
    1319                 :            :                 return 0;
    1320                 :            : 
    1321                 :          3 :         return pinctrl_commit_state(p, state);
    1322                 :            : }
    1323                 :            : EXPORT_SYMBOL_GPL(pinctrl_select_state);
    1324                 :            : 
    1325                 :          3 : static void devm_pinctrl_release(struct device *dev, void *res)
    1326                 :            : {
    1327                 :          3 :         pinctrl_put(*(struct pinctrl **)res);
    1328                 :          3 : }
    1329                 :            : 
    1330                 :            : /**
    1331                 :            :  * struct devm_pinctrl_get() - Resource managed pinctrl_get()
    1332                 :            :  * @dev: the device to obtain the handle for
    1333                 :            :  *
    1334                 :            :  * If there is a need to explicitly destroy the returned struct pinctrl,
    1335                 :            :  * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
    1336                 :            :  */
    1337                 :          3 : struct pinctrl *devm_pinctrl_get(struct device *dev)
    1338                 :            : {
    1339                 :            :         struct pinctrl **ptr, *p;
    1340                 :            : 
    1341                 :            :         ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
    1342                 :          3 :         if (!ptr)
    1343                 :            :                 return ERR_PTR(-ENOMEM);
    1344                 :            : 
    1345                 :          3 :         p = pinctrl_get(dev);
    1346                 :          3 :         if (!IS_ERR(p)) {
    1347                 :          3 :                 *ptr = p;
    1348                 :          3 :                 devres_add(dev, ptr);
    1349                 :            :         } else {
    1350                 :          3 :                 devres_free(ptr);
    1351                 :            :         }
    1352                 :            : 
    1353                 :          3 :         return p;
    1354                 :            : }
    1355                 :            : EXPORT_SYMBOL_GPL(devm_pinctrl_get);
    1356                 :            : 
    1357                 :          3 : static int devm_pinctrl_match(struct device *dev, void *res, void *data)
    1358                 :            : {
    1359                 :            :         struct pinctrl **p = res;
    1360                 :            : 
    1361                 :          3 :         return *p == data;
    1362                 :            : }
    1363                 :            : 
    1364                 :            : /**
    1365                 :            :  * devm_pinctrl_put() - Resource managed pinctrl_put()
    1366                 :            :  * @p: the pinctrl handle to release
    1367                 :            :  *
    1368                 :            :  * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
    1369                 :            :  * this function will not need to be called and the resource management
    1370                 :            :  * code will ensure that the resource is freed.
    1371                 :            :  */
    1372                 :          3 : void devm_pinctrl_put(struct pinctrl *p)
    1373                 :            : {
    1374                 :          3 :         WARN_ON(devres_release(p->dev, devm_pinctrl_release,
    1375                 :            :                                devm_pinctrl_match, p));
    1376                 :          3 : }
    1377                 :            : EXPORT_SYMBOL_GPL(devm_pinctrl_put);
    1378                 :            : 
    1379                 :          3 : int pinctrl_register_map(const struct pinctrl_map *maps, unsigned num_maps,
    1380                 :            :                          bool dup)
    1381                 :            : {
    1382                 :            :         int i, ret;
    1383                 :            :         struct pinctrl_maps *maps_node;
    1384                 :            : 
    1385                 :            :         pr_debug("add %u pinctrl maps\n", num_maps);
    1386                 :            : 
    1387                 :            :         /* First sanity check the new mapping */
    1388                 :          3 :         for (i = 0; i < num_maps; i++) {
    1389                 :          3 :                 if (!maps[i].dev_name) {
    1390                 :          0 :                         pr_err("failed to register map %s (%d): no device given\n",
    1391                 :            :                                maps[i].name, i);
    1392                 :          0 :                         return -EINVAL;
    1393                 :            :                 }
    1394                 :            : 
    1395                 :          3 :                 if (!maps[i].name) {
    1396                 :          0 :                         pr_err("failed to register map %d: no map name given\n",
    1397                 :            :                                i);
    1398                 :          0 :                         return -EINVAL;
    1399                 :            :                 }
    1400                 :            : 
    1401                 :          3 :                 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
    1402                 :          3 :                                 !maps[i].ctrl_dev_name) {
    1403                 :          0 :                         pr_err("failed to register map %s (%d): no pin control device given\n",
    1404                 :            :                                maps[i].name, i);
    1405                 :          0 :                         return -EINVAL;
    1406                 :            :                 }
    1407                 :            : 
    1408                 :          3 :                 switch (maps[i].type) {
    1409                 :            :                 case PIN_MAP_TYPE_DUMMY_STATE:
    1410                 :            :                         break;
    1411                 :            :                 case PIN_MAP_TYPE_MUX_GROUP:
    1412                 :          3 :                         ret = pinmux_validate_map(&maps[i], i);
    1413                 :          3 :                         if (ret < 0)
    1414                 :          0 :                                 return ret;
    1415                 :            :                         break;
    1416                 :            :                 case PIN_MAP_TYPE_CONFIGS_PIN:
    1417                 :            :                 case PIN_MAP_TYPE_CONFIGS_GROUP:
    1418                 :          0 :                         ret = pinconf_validate_map(&maps[i], i);
    1419                 :          0 :                         if (ret < 0)
    1420                 :          0 :                                 return ret;
    1421                 :            :                         break;
    1422                 :            :                 default:
    1423                 :          0 :                         pr_err("failed to register map %s (%d): invalid type given\n",
    1424                 :            :                                maps[i].name, i);
    1425                 :          0 :                         return -EINVAL;
    1426                 :            :                 }
    1427                 :            :         }
    1428                 :            : 
    1429                 :          3 :         maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
    1430                 :          3 :         if (!maps_node)
    1431                 :            :                 return -ENOMEM;
    1432                 :            : 
    1433                 :          3 :         maps_node->num_maps = num_maps;
    1434                 :          3 :         if (dup) {
    1435                 :          0 :                 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
    1436                 :            :                                           GFP_KERNEL);
    1437                 :          0 :                 if (!maps_node->maps) {
    1438                 :          0 :                         kfree(maps_node);
    1439                 :          0 :                         return -ENOMEM;
    1440                 :            :                 }
    1441                 :            :         } else {
    1442                 :          3 :                 maps_node->maps = maps;
    1443                 :            :         }
    1444                 :            : 
    1445                 :          3 :         mutex_lock(&pinctrl_maps_mutex);
    1446                 :          3 :         list_add_tail(&maps_node->node, &pinctrl_maps);
    1447                 :          3 :         mutex_unlock(&pinctrl_maps_mutex);
    1448                 :            : 
    1449                 :          3 :         return 0;
    1450                 :            : }
    1451                 :            : 
    1452                 :            : /**
    1453                 :            :  * pinctrl_register_mappings() - register a set of pin controller mappings
    1454                 :            :  * @maps: the pincontrol mappings table to register. This should probably be
    1455                 :            :  *      marked with __initdata so it can be discarded after boot. This
    1456                 :            :  *      function will perform a shallow copy for the mapping entries.
    1457                 :            :  * @num_maps: the number of maps in the mapping table
    1458                 :            :  */
    1459                 :          0 : int pinctrl_register_mappings(const struct pinctrl_map *maps,
    1460                 :            :                               unsigned num_maps)
    1461                 :            : {
    1462                 :          0 :         return pinctrl_register_map(maps, num_maps, true);
    1463                 :            : }
    1464                 :            : EXPORT_SYMBOL_GPL(pinctrl_register_mappings);
    1465                 :            : 
    1466                 :          3 : void pinctrl_unregister_map(const struct pinctrl_map *map)
    1467                 :            : {
    1468                 :            :         struct pinctrl_maps *maps_node;
    1469                 :            : 
    1470                 :          3 :         mutex_lock(&pinctrl_maps_mutex);
    1471                 :          3 :         list_for_each_entry(maps_node, &pinctrl_maps, node) {
    1472                 :          3 :                 if (maps_node->maps == map) {
    1473                 :            :                         list_del(&maps_node->node);
    1474                 :          3 :                         kfree(maps_node);
    1475                 :          3 :                         mutex_unlock(&pinctrl_maps_mutex);
    1476                 :          3 :                         return;
    1477                 :            :                 }
    1478                 :            :         }
    1479                 :          0 :         mutex_unlock(&pinctrl_maps_mutex);
    1480                 :            : }
    1481                 :            : 
    1482                 :            : /**
    1483                 :            :  * pinctrl_force_sleep() - turn a given controller device into sleep state
    1484                 :            :  * @pctldev: pin controller device
    1485                 :            :  */
    1486                 :          0 : int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
    1487                 :            : {
    1488                 :          0 :         if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
    1489                 :          0 :                 return pinctrl_commit_state(pctldev->p, pctldev->hog_sleep);
    1490                 :            :         return 0;
    1491                 :            : }
    1492                 :            : EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
    1493                 :            : 
    1494                 :            : /**
    1495                 :            :  * pinctrl_force_default() - turn a given controller device into default state
    1496                 :            :  * @pctldev: pin controller device
    1497                 :            :  */
    1498                 :          0 : int pinctrl_force_default(struct pinctrl_dev *pctldev)
    1499                 :            : {
    1500                 :          0 :         if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
    1501                 :          0 :                 return pinctrl_commit_state(pctldev->p, pctldev->hog_default);
    1502                 :            :         return 0;
    1503                 :            : }
    1504                 :            : EXPORT_SYMBOL_GPL(pinctrl_force_default);
    1505                 :            : 
    1506                 :            : /**
    1507                 :            :  * pinctrl_init_done() - tell pinctrl probe is done
    1508                 :            :  *
    1509                 :            :  * We'll use this time to switch the pins from "init" to "default" unless the
    1510                 :            :  * driver selected some other state.
    1511                 :            :  *
    1512                 :            :  * @dev: device to that's done probing
    1513                 :            :  */
    1514                 :          3 : int pinctrl_init_done(struct device *dev)
    1515                 :            : {
    1516                 :          3 :         struct dev_pin_info *pins = dev->pins;
    1517                 :            :         int ret;
    1518                 :            : 
    1519                 :          3 :         if (!pins)
    1520                 :            :                 return 0;
    1521                 :            : 
    1522                 :          3 :         if (IS_ERR(pins->init_state))
    1523                 :            :                 return 0; /* No such state */
    1524                 :            : 
    1525                 :          0 :         if (pins->p->state != pins->init_state)
    1526                 :            :                 return 0; /* Not at init anyway */
    1527                 :            : 
    1528                 :          0 :         if (IS_ERR(pins->default_state))
    1529                 :            :                 return 0; /* No default state */
    1530                 :            : 
    1531                 :            :         ret = pinctrl_select_state(pins->p, pins->default_state);
    1532                 :          0 :         if (ret)
    1533                 :          0 :                 dev_err(dev, "failed to activate default pinctrl state\n");
    1534                 :            : 
    1535                 :          0 :         return ret;
    1536                 :            : }
    1537                 :            : 
    1538                 :            : #ifdef CONFIG_PM
    1539                 :            : 
    1540                 :            : /**
    1541                 :            :  * pinctrl_pm_select_state() - select pinctrl state for PM
    1542                 :            :  * @dev: device to select default state for
    1543                 :            :  * @state: state to set
    1544                 :            :  */
    1545                 :          0 : static int pinctrl_pm_select_state(struct device *dev,
    1546                 :            :                                    struct pinctrl_state *state)
    1547                 :            : {
    1548                 :          0 :         struct dev_pin_info *pins = dev->pins;
    1549                 :            :         int ret;
    1550                 :            : 
    1551                 :          0 :         if (IS_ERR(state))
    1552                 :            :                 return 0; /* No such state */
    1553                 :          0 :         ret = pinctrl_select_state(pins->p, state);
    1554                 :          0 :         if (ret)
    1555                 :          0 :                 dev_err(dev, "failed to activate pinctrl state %s\n",
    1556                 :            :                         state->name);
    1557                 :          0 :         return ret;
    1558                 :            : }
    1559                 :            : 
    1560                 :            : /**
    1561                 :            :  * pinctrl_pm_select_default_state() - select default pinctrl state for PM
    1562                 :            :  * @dev: device to select default state for
    1563                 :            :  */
    1564                 :          3 : int pinctrl_pm_select_default_state(struct device *dev)
    1565                 :            : {
    1566                 :          3 :         if (!dev->pins)
    1567                 :            :                 return 0;
    1568                 :            : 
    1569                 :          0 :         return pinctrl_pm_select_state(dev, dev->pins->default_state);
    1570                 :            : }
    1571                 :            : EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
    1572                 :            : 
    1573                 :            : /**
    1574                 :            :  * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
    1575                 :            :  * @dev: device to select sleep state for
    1576                 :            :  */
    1577                 :          3 : int pinctrl_pm_select_sleep_state(struct device *dev)
    1578                 :            : {
    1579                 :          3 :         if (!dev->pins)
    1580                 :            :                 return 0;
    1581                 :            : 
    1582                 :          0 :         return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
    1583                 :            : }
    1584                 :            : EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
    1585                 :            : 
    1586                 :            : /**
    1587                 :            :  * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
    1588                 :            :  * @dev: device to select idle state for
    1589                 :            :  */
    1590                 :          0 : int pinctrl_pm_select_idle_state(struct device *dev)
    1591                 :            : {
    1592                 :          0 :         if (!dev->pins)
    1593                 :            :                 return 0;
    1594                 :            : 
    1595                 :          0 :         return pinctrl_pm_select_state(dev, dev->pins->idle_state);
    1596                 :            : }
    1597                 :            : EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
    1598                 :            : #endif
    1599                 :            : 
    1600                 :            : #ifdef CONFIG_DEBUG_FS
    1601                 :            : 
    1602                 :          0 : static int pinctrl_pins_show(struct seq_file *s, void *what)
    1603                 :            : {
    1604                 :          0 :         struct pinctrl_dev *pctldev = s->private;
    1605                 :          0 :         const struct pinctrl_ops *ops = pctldev->desc->pctlops;
    1606                 :            :         unsigned i, pin;
    1607                 :            : 
    1608                 :          0 :         seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
    1609                 :            : 
    1610                 :          0 :         mutex_lock(&pctldev->mutex);
    1611                 :            : 
    1612                 :            :         /* The pin number can be retrived from the pin controller descriptor */
    1613                 :          0 :         for (i = 0; i < pctldev->desc->npins; i++) {
    1614                 :            :                 struct pin_desc *desc;
    1615                 :            : 
    1616                 :          0 :                 pin = pctldev->desc->pins[i].number;
    1617                 :            :                 desc = pin_desc_get(pctldev, pin);
    1618                 :            :                 /* Pin space may be sparse */
    1619                 :          0 :                 if (!desc)
    1620                 :          0 :                         continue;
    1621                 :            : 
    1622                 :          0 :                 seq_printf(s, "pin %d (%s) ", pin, desc->name);
    1623                 :            : 
    1624                 :            :                 /* Driver-specific info per pin */
    1625                 :          0 :                 if (ops->pin_dbg_show)
    1626                 :          0 :                         ops->pin_dbg_show(pctldev, s, pin);
    1627                 :            : 
    1628                 :          0 :                 seq_puts(s, "\n");
    1629                 :            :         }
    1630                 :            : 
    1631                 :          0 :         mutex_unlock(&pctldev->mutex);
    1632                 :            : 
    1633                 :          0 :         return 0;
    1634                 :            : }
    1635                 :          0 : DEFINE_SHOW_ATTRIBUTE(pinctrl_pins);
    1636                 :            : 
    1637                 :          0 : static int pinctrl_groups_show(struct seq_file *s, void *what)
    1638                 :            : {
    1639                 :          0 :         struct pinctrl_dev *pctldev = s->private;
    1640                 :          0 :         const struct pinctrl_ops *ops = pctldev->desc->pctlops;
    1641                 :            :         unsigned ngroups, selector = 0;
    1642                 :            : 
    1643                 :          0 :         mutex_lock(&pctldev->mutex);
    1644                 :            : 
    1645                 :          0 :         ngroups = ops->get_groups_count(pctldev);
    1646                 :            : 
    1647                 :          0 :         seq_puts(s, "registered pin groups:\n");
    1648                 :          0 :         while (selector < ngroups) {
    1649                 :          0 :                 const unsigned *pins = NULL;
    1650                 :          0 :                 unsigned num_pins = 0;
    1651                 :          0 :                 const char *gname = ops->get_group_name(pctldev, selector);
    1652                 :            :                 const char *pname;
    1653                 :            :                 int ret = 0;
    1654                 :            :                 int i;
    1655                 :            : 
    1656                 :          0 :                 if (ops->get_group_pins)
    1657                 :          0 :                         ret = ops->get_group_pins(pctldev, selector,
    1658                 :            :                                                   &pins, &num_pins);
    1659                 :          0 :                 if (ret)
    1660                 :          0 :                         seq_printf(s, "%s [ERROR GETTING PINS]\n",
    1661                 :            :                                    gname);
    1662                 :            :                 else {
    1663                 :          0 :                         seq_printf(s, "group: %s\n", gname);
    1664                 :          0 :                         for (i = 0; i < num_pins; i++) {
    1665                 :          0 :                                 pname = pin_get_name(pctldev, pins[i]);
    1666                 :          0 :                                 if (WARN_ON(!pname)) {
    1667                 :          0 :                                         mutex_unlock(&pctldev->mutex);
    1668                 :          0 :                                         return -EINVAL;
    1669                 :            :                                 }
    1670                 :          0 :                                 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
    1671                 :            :                         }
    1672                 :          0 :                         seq_puts(s, "\n");
    1673                 :            :                 }
    1674                 :          0 :                 selector++;
    1675                 :            :         }
    1676                 :            : 
    1677                 :          0 :         mutex_unlock(&pctldev->mutex);
    1678                 :            : 
    1679                 :          0 :         return 0;
    1680                 :            : }
    1681                 :          0 : DEFINE_SHOW_ATTRIBUTE(pinctrl_groups);
    1682                 :            : 
    1683                 :          0 : static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
    1684                 :            : {
    1685                 :          0 :         struct pinctrl_dev *pctldev = s->private;
    1686                 :            :         struct pinctrl_gpio_range *range;
    1687                 :            : 
    1688                 :          0 :         seq_puts(s, "GPIO ranges handled:\n");
    1689                 :            : 
    1690                 :          0 :         mutex_lock(&pctldev->mutex);
    1691                 :            : 
    1692                 :            :         /* Loop over the ranges */
    1693                 :          0 :         list_for_each_entry(range, &pctldev->gpio_ranges, node) {
    1694                 :          0 :                 if (range->pins) {
    1695                 :            :                         int a;
    1696                 :          0 :                         seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
    1697                 :            :                                 range->id, range->name,
    1698                 :          0 :                                 range->base, (range->base + range->npins - 1));
    1699                 :          0 :                         for (a = 0; a < range->npins - 1; a++)
    1700                 :          0 :                                 seq_printf(s, "%u, ", range->pins[a]);
    1701                 :          0 :                         seq_printf(s, "%u}\n", range->pins[a]);
    1702                 :            :                 }
    1703                 :            :                 else
    1704                 :          0 :                         seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
    1705                 :            :                                 range->id, range->name,
    1706                 :          0 :                                 range->base, (range->base + range->npins - 1),
    1707                 :            :                                 range->pin_base,
    1708                 :          0 :                                 (range->pin_base + range->npins - 1));
    1709                 :            :         }
    1710                 :            : 
    1711                 :          0 :         mutex_unlock(&pctldev->mutex);
    1712                 :            : 
    1713                 :          0 :         return 0;
    1714                 :            : }
    1715                 :          0 : DEFINE_SHOW_ATTRIBUTE(pinctrl_gpioranges);
    1716                 :            : 
    1717                 :          0 : static int pinctrl_devices_show(struct seq_file *s, void *what)
    1718                 :            : {
    1719                 :            :         struct pinctrl_dev *pctldev;
    1720                 :            : 
    1721                 :          0 :         seq_puts(s, "name [pinmux] [pinconf]\n");
    1722                 :            : 
    1723                 :          0 :         mutex_lock(&pinctrldev_list_mutex);
    1724                 :            : 
    1725                 :          0 :         list_for_each_entry(pctldev, &pinctrldev_list, node) {
    1726                 :          0 :                 seq_printf(s, "%s ", pctldev->desc->name);
    1727                 :          0 :                 if (pctldev->desc->pmxops)
    1728                 :          0 :                         seq_puts(s, "yes ");
    1729                 :            :                 else
    1730                 :          0 :                         seq_puts(s, "no ");
    1731                 :          0 :                 if (pctldev->desc->confops)
    1732                 :          0 :                         seq_puts(s, "yes");
    1733                 :            :                 else
    1734                 :          0 :                         seq_puts(s, "no");
    1735                 :          0 :                 seq_puts(s, "\n");
    1736                 :            :         }
    1737                 :            : 
    1738                 :          0 :         mutex_unlock(&pinctrldev_list_mutex);
    1739                 :            : 
    1740                 :          0 :         return 0;
    1741                 :            : }
    1742                 :          0 : DEFINE_SHOW_ATTRIBUTE(pinctrl_devices);
    1743                 :            : 
    1744                 :            : static inline const char *map_type(enum pinctrl_map_type type)
    1745                 :            : {
    1746                 :            :         static const char * const names[] = {
    1747                 :            :                 "INVALID",
    1748                 :            :                 "DUMMY_STATE",
    1749                 :            :                 "MUX_GROUP",
    1750                 :            :                 "CONFIGS_PIN",
    1751                 :            :                 "CONFIGS_GROUP",
    1752                 :            :         };
    1753                 :            : 
    1754                 :          0 :         if (type >= ARRAY_SIZE(names))
    1755                 :            :                 return "UNKNOWN";
    1756                 :            : 
    1757                 :          0 :         return names[type];
    1758                 :            : }
    1759                 :            : 
    1760                 :          0 : static int pinctrl_maps_show(struct seq_file *s, void *what)
    1761                 :            : {
    1762                 :            :         struct pinctrl_maps *maps_node;
    1763                 :            :         int i;
    1764                 :            :         const struct pinctrl_map *map;
    1765                 :            : 
    1766                 :          0 :         seq_puts(s, "Pinctrl maps:\n");
    1767                 :            : 
    1768                 :          0 :         mutex_lock(&pinctrl_maps_mutex);
    1769                 :          0 :         for_each_maps(maps_node, i, map) {
    1770                 :          0 :                 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
    1771                 :            :                            map->dev_name, map->name, map_type(map->type),
    1772                 :            :                            map->type);
    1773                 :            : 
    1774                 :          0 :                 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
    1775                 :          0 :                         seq_printf(s, "controlling device %s\n",
    1776                 :            :                                    map->ctrl_dev_name);
    1777                 :            : 
    1778                 :          0 :                 switch (map->type) {
    1779                 :            :                 case PIN_MAP_TYPE_MUX_GROUP:
    1780                 :          0 :                         pinmux_show_map(s, map);
    1781                 :          0 :                         break;
    1782                 :            :                 case PIN_MAP_TYPE_CONFIGS_PIN:
    1783                 :            :                 case PIN_MAP_TYPE_CONFIGS_GROUP:
    1784                 :          0 :                         pinconf_show_map(s, map);
    1785                 :          0 :                         break;
    1786                 :            :                 default:
    1787                 :            :                         break;
    1788                 :            :                 }
    1789                 :            : 
    1790                 :          0 :                 seq_putc(s, '\n');
    1791                 :            :         }
    1792                 :          0 :         mutex_unlock(&pinctrl_maps_mutex);
    1793                 :            : 
    1794                 :          0 :         return 0;
    1795                 :            : }
    1796                 :          0 : DEFINE_SHOW_ATTRIBUTE(pinctrl_maps);
    1797                 :            : 
    1798                 :          0 : static int pinctrl_show(struct seq_file *s, void *what)
    1799                 :            : {
    1800                 :            :         struct pinctrl *p;
    1801                 :            :         struct pinctrl_state *state;
    1802                 :            :         struct pinctrl_setting *setting;
    1803                 :            : 
    1804                 :          0 :         seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
    1805                 :            : 
    1806                 :          0 :         mutex_lock(&pinctrl_list_mutex);
    1807                 :            : 
    1808                 :          0 :         list_for_each_entry(p, &pinctrl_list, node) {
    1809                 :          0 :                 seq_printf(s, "device: %s current state: %s\n",
    1810                 :          0 :                            dev_name(p->dev),
    1811                 :          0 :                            p->state ? p->state->name : "none");
    1812                 :            : 
    1813                 :          0 :                 list_for_each_entry(state, &p->states, node) {
    1814                 :          0 :                         seq_printf(s, "  state: %s\n", state->name);
    1815                 :            : 
    1816                 :          0 :                         list_for_each_entry(setting, &state->settings, node) {
    1817                 :          0 :                                 struct pinctrl_dev *pctldev = setting->pctldev;
    1818                 :            : 
    1819                 :          0 :                                 seq_printf(s, "    type: %s controller %s ",
    1820                 :            :                                            map_type(setting->type),
    1821                 :            :                                            pinctrl_dev_get_name(pctldev));
    1822                 :            : 
    1823                 :          0 :                                 switch (setting->type) {
    1824                 :            :                                 case PIN_MAP_TYPE_MUX_GROUP:
    1825                 :          0 :                                         pinmux_show_setting(s, setting);
    1826                 :          0 :                                         break;
    1827                 :            :                                 case PIN_MAP_TYPE_CONFIGS_PIN:
    1828                 :            :                                 case PIN_MAP_TYPE_CONFIGS_GROUP:
    1829                 :          0 :                                         pinconf_show_setting(s, setting);
    1830                 :          0 :                                         break;
    1831                 :            :                                 default:
    1832                 :            :                                         break;
    1833                 :            :                                 }
    1834                 :            :                         }
    1835                 :            :                 }
    1836                 :            :         }
    1837                 :            : 
    1838                 :          0 :         mutex_unlock(&pinctrl_list_mutex);
    1839                 :            : 
    1840                 :          0 :         return 0;
    1841                 :            : }
    1842                 :          0 : DEFINE_SHOW_ATTRIBUTE(pinctrl);
    1843                 :            : 
    1844                 :            : static struct dentry *debugfs_root;
    1845                 :            : 
    1846                 :          3 : static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
    1847                 :            : {
    1848                 :            :         struct dentry *device_root;
    1849                 :            :         const char *debugfs_name;
    1850                 :            : 
    1851                 :          3 :         if (pctldev->desc->name &&
    1852                 :          3 :                         strcmp(dev_name(pctldev->dev), pctldev->desc->name)) {
    1853                 :          3 :                 debugfs_name = devm_kasprintf(pctldev->dev, GFP_KERNEL,
    1854                 :            :                                 "%s-%s", dev_name(pctldev->dev),
    1855                 :            :                                 pctldev->desc->name);
    1856                 :          3 :                 if (!debugfs_name) {
    1857                 :          0 :                         pr_warn("failed to determine debugfs dir name for %s\n",
    1858                 :            :                                 dev_name(pctldev->dev));
    1859                 :          0 :                         return;
    1860                 :            :                 }
    1861                 :            :         } else {
    1862                 :          0 :                 debugfs_name = dev_name(pctldev->dev);
    1863                 :            :         }
    1864                 :            : 
    1865                 :          3 :         device_root = debugfs_create_dir(debugfs_name, debugfs_root);
    1866                 :          3 :         pctldev->device_root = device_root;
    1867                 :            : 
    1868                 :          3 :         if (IS_ERR(device_root) || !device_root) {
    1869                 :          0 :                 pr_warn("failed to create debugfs directory for %s\n",
    1870                 :            :                         dev_name(pctldev->dev));
    1871                 :          0 :                 return;
    1872                 :            :         }
    1873                 :          3 :         debugfs_create_file("pins", S_IFREG | S_IRUGO,
    1874                 :            :                             device_root, pctldev, &pinctrl_pins_fops);
    1875                 :          3 :         debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
    1876                 :            :                             device_root, pctldev, &pinctrl_groups_fops);
    1877                 :          3 :         debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
    1878                 :            :                             device_root, pctldev, &pinctrl_gpioranges_fops);
    1879                 :          3 :         if (pctldev->desc->pmxops)
    1880                 :          3 :                 pinmux_init_device_debugfs(device_root, pctldev);
    1881                 :          3 :         if (pctldev->desc->confops)
    1882                 :          3 :                 pinconf_init_device_debugfs(device_root, pctldev);
    1883                 :            : }
    1884                 :            : 
    1885                 :            : static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
    1886                 :            : {
    1887                 :          0 :         debugfs_remove_recursive(pctldev->device_root);
    1888                 :            : }
    1889                 :            : 
    1890                 :          3 : static void pinctrl_init_debugfs(void)
    1891                 :            : {
    1892                 :          3 :         debugfs_root = debugfs_create_dir("pinctrl", NULL);
    1893                 :          3 :         if (IS_ERR(debugfs_root) || !debugfs_root) {
    1894                 :          0 :                 pr_warn("failed to create debugfs directory\n");
    1895                 :          0 :                 debugfs_root = NULL;
    1896                 :          3 :                 return;
    1897                 :            :         }
    1898                 :            : 
    1899                 :          3 :         debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
    1900                 :            :                             debugfs_root, NULL, &pinctrl_devices_fops);
    1901                 :          3 :         debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
    1902                 :            :                             debugfs_root, NULL, &pinctrl_maps_fops);
    1903                 :          3 :         debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
    1904                 :            :                             debugfs_root, NULL, &pinctrl_fops);
    1905                 :            : }
    1906                 :            : 
    1907                 :            : #else /* CONFIG_DEBUG_FS */
    1908                 :            : 
    1909                 :            : static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
    1910                 :            : {
    1911                 :            : }
    1912                 :            : 
    1913                 :            : static void pinctrl_init_debugfs(void)
    1914                 :            : {
    1915                 :            : }
    1916                 :            : 
    1917                 :            : static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
    1918                 :            : {
    1919                 :            : }
    1920                 :            : 
    1921                 :            : #endif
    1922                 :            : 
    1923                 :            : static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
    1924                 :            : {
    1925                 :          3 :         const struct pinctrl_ops *ops = pctldev->desc->pctlops;
    1926                 :            : 
    1927                 :          3 :         if (!ops ||
    1928                 :          3 :             !ops->get_groups_count ||
    1929                 :          3 :             !ops->get_group_name)
    1930                 :            :                 return -EINVAL;
    1931                 :            : 
    1932                 :            :         return 0;
    1933                 :            : }
    1934                 :            : 
    1935                 :            : /**
    1936                 :            :  * pinctrl_init_controller() - init a pin controller device
    1937                 :            :  * @pctldesc: descriptor for this pin controller
    1938                 :            :  * @dev: parent device for this pin controller
    1939                 :            :  * @driver_data: private pin controller data for this pin controller
    1940                 :            :  */
    1941                 :            : static struct pinctrl_dev *
    1942                 :          3 : pinctrl_init_controller(struct pinctrl_desc *pctldesc, struct device *dev,
    1943                 :            :                         void *driver_data)
    1944                 :            : {
    1945                 :            :         struct pinctrl_dev *pctldev;
    1946                 :            :         int ret;
    1947                 :            : 
    1948                 :          3 :         if (!pctldesc)
    1949                 :            :                 return ERR_PTR(-EINVAL);
    1950                 :          3 :         if (!pctldesc->name)
    1951                 :            :                 return ERR_PTR(-EINVAL);
    1952                 :            : 
    1953                 :          3 :         pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
    1954                 :          3 :         if (!pctldev)
    1955                 :            :                 return ERR_PTR(-ENOMEM);
    1956                 :            : 
    1957                 :            :         /* Initialize pin control device struct */
    1958                 :          3 :         pctldev->owner = pctldesc->owner;
    1959                 :          3 :         pctldev->desc = pctldesc;
    1960                 :          3 :         pctldev->driver_data = driver_data;
    1961                 :            :         INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
    1962                 :            : #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
    1963                 :            :         INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL);
    1964                 :            : #endif
    1965                 :            : #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
    1966                 :            :         INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL);
    1967                 :            : #endif
    1968                 :          3 :         INIT_LIST_HEAD(&pctldev->gpio_ranges);
    1969                 :          3 :         INIT_LIST_HEAD(&pctldev->node);
    1970                 :          3 :         pctldev->dev = dev;
    1971                 :          3 :         mutex_init(&pctldev->mutex);
    1972                 :            : 
    1973                 :            :         /* check core ops for sanity */
    1974                 :            :         ret = pinctrl_check_ops(pctldev);
    1975                 :          3 :         if (ret) {
    1976                 :          0 :                 dev_err(dev, "pinctrl ops lacks necessary functions\n");
    1977                 :          0 :                 goto out_err;
    1978                 :            :         }
    1979                 :            : 
    1980                 :            :         /* If we're implementing pinmuxing, check the ops for sanity */
    1981                 :          3 :         if (pctldesc->pmxops) {
    1982                 :          3 :                 ret = pinmux_check_ops(pctldev);
    1983                 :          3 :                 if (ret)
    1984                 :            :                         goto out_err;
    1985                 :            :         }
    1986                 :            : 
    1987                 :            :         /* If we're implementing pinconfig, check the ops for sanity */
    1988                 :          3 :         if (pctldesc->confops) {
    1989                 :          3 :                 ret = pinconf_check_ops(pctldev);
    1990                 :          3 :                 if (ret)
    1991                 :            :                         goto out_err;
    1992                 :            :         }
    1993                 :            : 
    1994                 :            :         /* Register all the pins */
    1995                 :            :         dev_dbg(dev, "try to register %d pins ...\n",  pctldesc->npins);
    1996                 :          3 :         ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
    1997                 :          3 :         if (ret) {
    1998                 :          0 :                 dev_err(dev, "error during pin registration\n");
    1999                 :          0 :                 pinctrl_free_pindescs(pctldev, pctldesc->pins,
    2000                 :            :                                       pctldesc->npins);
    2001                 :          0 :                 goto out_err;
    2002                 :            :         }
    2003                 :            : 
    2004                 :            :         return pctldev;
    2005                 :            : 
    2006                 :            : out_err:
    2007                 :            :         mutex_destroy(&pctldev->mutex);
    2008                 :          0 :         kfree(pctldev);
    2009                 :          0 :         return ERR_PTR(ret);
    2010                 :            : }
    2011                 :            : 
    2012                 :          3 : static int pinctrl_claim_hogs(struct pinctrl_dev *pctldev)
    2013                 :            : {
    2014                 :          3 :         pctldev->p = create_pinctrl(pctldev->dev, pctldev);
    2015                 :          3 :         if (PTR_ERR(pctldev->p) == -ENODEV) {
    2016                 :            :                 dev_dbg(pctldev->dev, "no hogs found\n");
    2017                 :            : 
    2018                 :            :                 return 0;
    2019                 :            :         }
    2020                 :            : 
    2021                 :          0 :         if (IS_ERR(pctldev->p)) {
    2022                 :          0 :                 dev_err(pctldev->dev, "error claiming hogs: %li\n",
    2023                 :            :                         PTR_ERR(pctldev->p));
    2024                 :            : 
    2025                 :          0 :                 return PTR_ERR(pctldev->p);
    2026                 :            :         }
    2027                 :            : 
    2028                 :          0 :         pctldev->hog_default =
    2029                 :          0 :                 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
    2030                 :          0 :         if (IS_ERR(pctldev->hog_default)) {
    2031                 :            :                 dev_dbg(pctldev->dev,
    2032                 :            :                         "failed to lookup the default state\n");
    2033                 :            :         } else {
    2034                 :          0 :                 if (pinctrl_select_state(pctldev->p,
    2035                 :            :                                          pctldev->hog_default))
    2036                 :          0 :                         dev_err(pctldev->dev,
    2037                 :            :                                 "failed to select default state\n");
    2038                 :            :         }
    2039                 :            : 
    2040                 :          0 :         pctldev->hog_sleep =
    2041                 :          0 :                 pinctrl_lookup_state(pctldev->p,
    2042                 :            :                                      PINCTRL_STATE_SLEEP);
    2043                 :            :         if (IS_ERR(pctldev->hog_sleep))
    2044                 :            :                 dev_dbg(pctldev->dev,
    2045                 :            :                         "failed to lookup the sleep state\n");
    2046                 :            : 
    2047                 :          0 :         return 0;
    2048                 :            : }
    2049                 :            : 
    2050                 :          3 : int pinctrl_enable(struct pinctrl_dev *pctldev)
    2051                 :            : {
    2052                 :            :         int error;
    2053                 :            : 
    2054                 :          3 :         error = pinctrl_claim_hogs(pctldev);
    2055                 :          3 :         if (error) {
    2056                 :          0 :                 dev_err(pctldev->dev, "could not claim hogs: %i\n",
    2057                 :            :                         error);
    2058                 :            :                 mutex_destroy(&pctldev->mutex);
    2059                 :          0 :                 kfree(pctldev);
    2060                 :            : 
    2061                 :          0 :                 return error;
    2062                 :            :         }
    2063                 :            : 
    2064                 :          3 :         mutex_lock(&pinctrldev_list_mutex);
    2065                 :          3 :         list_add_tail(&pctldev->node, &pinctrldev_list);
    2066                 :          3 :         mutex_unlock(&pinctrldev_list_mutex);
    2067                 :            : 
    2068                 :          3 :         pinctrl_init_device_debugfs(pctldev);
    2069                 :            : 
    2070                 :          3 :         return 0;
    2071                 :            : }
    2072                 :            : EXPORT_SYMBOL_GPL(pinctrl_enable);
    2073                 :            : 
    2074                 :            : /**
    2075                 :            :  * pinctrl_register() - register a pin controller device
    2076                 :            :  * @pctldesc: descriptor for this pin controller
    2077                 :            :  * @dev: parent device for this pin controller
    2078                 :            :  * @driver_data: private pin controller data for this pin controller
    2079                 :            :  *
    2080                 :            :  * Note that pinctrl_register() is known to have problems as the pin
    2081                 :            :  * controller driver functions are called before the driver has a
    2082                 :            :  * struct pinctrl_dev handle. To avoid issues later on, please use the
    2083                 :            :  * new pinctrl_register_and_init() below instead.
    2084                 :            :  */
    2085                 :          3 : struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
    2086                 :            :                                     struct device *dev, void *driver_data)
    2087                 :            : {
    2088                 :            :         struct pinctrl_dev *pctldev;
    2089                 :            :         int error;
    2090                 :            : 
    2091                 :          3 :         pctldev = pinctrl_init_controller(pctldesc, dev, driver_data);
    2092                 :          3 :         if (IS_ERR(pctldev))
    2093                 :            :                 return pctldev;
    2094                 :            : 
    2095                 :          3 :         error = pinctrl_enable(pctldev);
    2096                 :          3 :         if (error)
    2097                 :          0 :                 return ERR_PTR(error);
    2098                 :            : 
    2099                 :            :         return pctldev;
    2100                 :            : 
    2101                 :            : }
    2102                 :            : EXPORT_SYMBOL_GPL(pinctrl_register);
    2103                 :            : 
    2104                 :            : /**
    2105                 :            :  * pinctrl_register_and_init() - register and init pin controller device
    2106                 :            :  * @pctldesc: descriptor for this pin controller
    2107                 :            :  * @dev: parent device for this pin controller
    2108                 :            :  * @driver_data: private pin controller data for this pin controller
    2109                 :            :  * @pctldev: pin controller device
    2110                 :            :  *
    2111                 :            :  * Note that pinctrl_enable() still needs to be manually called after
    2112                 :            :  * this once the driver is ready.
    2113                 :            :  */
    2114                 :          0 : int pinctrl_register_and_init(struct pinctrl_desc *pctldesc,
    2115                 :            :                               struct device *dev, void *driver_data,
    2116                 :            :                               struct pinctrl_dev **pctldev)
    2117                 :            : {
    2118                 :            :         struct pinctrl_dev *p;
    2119                 :            : 
    2120                 :          0 :         p = pinctrl_init_controller(pctldesc, dev, driver_data);
    2121                 :          0 :         if (IS_ERR(p))
    2122                 :          0 :                 return PTR_ERR(p);
    2123                 :            : 
    2124                 :            :         /*
    2125                 :            :          * We have pinctrl_start() call functions in the pin controller
    2126                 :            :          * driver with create_pinctrl() for at least dt_node_to_map(). So
    2127                 :            :          * let's make sure pctldev is properly initialized for the
    2128                 :            :          * pin controller driver before we do anything.
    2129                 :            :          */
    2130                 :          0 :         *pctldev = p;
    2131                 :            : 
    2132                 :          0 :         return 0;
    2133                 :            : }
    2134                 :            : EXPORT_SYMBOL_GPL(pinctrl_register_and_init);
    2135                 :            : 
    2136                 :            : /**
    2137                 :            :  * pinctrl_unregister() - unregister pinmux
    2138                 :            :  * @pctldev: pin controller to unregister
    2139                 :            :  *
    2140                 :            :  * Called by pinmux drivers to unregister a pinmux.
    2141                 :            :  */
    2142                 :          0 : void pinctrl_unregister(struct pinctrl_dev *pctldev)
    2143                 :            : {
    2144                 :            :         struct pinctrl_gpio_range *range, *n;
    2145                 :            : 
    2146                 :          0 :         if (!pctldev)
    2147                 :          0 :                 return;
    2148                 :            : 
    2149                 :          0 :         mutex_lock(&pctldev->mutex);
    2150                 :            :         pinctrl_remove_device_debugfs(pctldev);
    2151                 :          0 :         mutex_unlock(&pctldev->mutex);
    2152                 :            : 
    2153                 :          0 :         if (!IS_ERR_OR_NULL(pctldev->p))
    2154                 :            :                 pinctrl_put(pctldev->p);
    2155                 :            : 
    2156                 :          0 :         mutex_lock(&pinctrldev_list_mutex);
    2157                 :          0 :         mutex_lock(&pctldev->mutex);
    2158                 :            :         /* TODO: check that no pinmuxes are still active? */
    2159                 :            :         list_del(&pctldev->node);
    2160                 :            :         pinmux_generic_free_functions(pctldev);
    2161                 :            :         pinctrl_generic_free_groups(pctldev);
    2162                 :            :         /* Destroy descriptor tree */
    2163                 :          0 :         pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
    2164                 :            :                               pctldev->desc->npins);
    2165                 :            :         /* remove gpio ranges map */
    2166                 :          0 :         list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
    2167                 :            :                 list_del(&range->node);
    2168                 :            : 
    2169                 :          0 :         mutex_unlock(&pctldev->mutex);
    2170                 :            :         mutex_destroy(&pctldev->mutex);
    2171                 :          0 :         kfree(pctldev);
    2172                 :          0 :         mutex_unlock(&pinctrldev_list_mutex);
    2173                 :            : }
    2174                 :            : EXPORT_SYMBOL_GPL(pinctrl_unregister);
    2175                 :            : 
    2176                 :          0 : static void devm_pinctrl_dev_release(struct device *dev, void *res)
    2177                 :            : {
    2178                 :          0 :         struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
    2179                 :            : 
    2180                 :          0 :         pinctrl_unregister(pctldev);
    2181                 :          0 : }
    2182                 :            : 
    2183                 :          0 : static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
    2184                 :            : {
    2185                 :            :         struct pctldev **r = res;
    2186                 :            : 
    2187                 :          0 :         if (WARN_ON(!r || !*r))
    2188                 :            :                 return 0;
    2189                 :            : 
    2190                 :          0 :         return *r == data;
    2191                 :            : }
    2192                 :            : 
    2193                 :            : /**
    2194                 :            :  * devm_pinctrl_register() - Resource managed version of pinctrl_register().
    2195                 :            :  * @dev: parent device for this pin controller
    2196                 :            :  * @pctldesc: descriptor for this pin controller
    2197                 :            :  * @driver_data: private pin controller data for this pin controller
    2198                 :            :  *
    2199                 :            :  * Returns an error pointer if pincontrol register failed. Otherwise
    2200                 :            :  * it returns valid pinctrl handle.
    2201                 :            :  *
    2202                 :            :  * The pinctrl device will be automatically released when the device is unbound.
    2203                 :            :  */
    2204                 :          3 : struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
    2205                 :            :                                           struct pinctrl_desc *pctldesc,
    2206                 :            :                                           void *driver_data)
    2207                 :            : {
    2208                 :            :         struct pinctrl_dev **ptr, *pctldev;
    2209                 :            : 
    2210                 :            :         ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
    2211                 :          3 :         if (!ptr)
    2212                 :            :                 return ERR_PTR(-ENOMEM);
    2213                 :            : 
    2214                 :          3 :         pctldev = pinctrl_register(pctldesc, dev, driver_data);
    2215                 :          3 :         if (IS_ERR(pctldev)) {
    2216                 :          0 :                 devres_free(ptr);
    2217                 :          0 :                 return pctldev;
    2218                 :            :         }
    2219                 :            : 
    2220                 :          3 :         *ptr = pctldev;
    2221                 :          3 :         devres_add(dev, ptr);
    2222                 :            : 
    2223                 :          3 :         return pctldev;
    2224                 :            : }
    2225                 :            : EXPORT_SYMBOL_GPL(devm_pinctrl_register);
    2226                 :            : 
    2227                 :            : /**
    2228                 :            :  * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
    2229                 :            :  * @dev: parent device for this pin controller
    2230                 :            :  * @pctldesc: descriptor for this pin controller
    2231                 :            :  * @driver_data: private pin controller data for this pin controller
    2232                 :            :  *
    2233                 :            :  * Returns an error pointer if pincontrol register failed. Otherwise
    2234                 :            :  * it returns valid pinctrl handle.
    2235                 :            :  *
    2236                 :            :  * The pinctrl device will be automatically released when the device is unbound.
    2237                 :            :  */
    2238                 :          0 : int devm_pinctrl_register_and_init(struct device *dev,
    2239                 :            :                                    struct pinctrl_desc *pctldesc,
    2240                 :            :                                    void *driver_data,
    2241                 :            :                                    struct pinctrl_dev **pctldev)
    2242                 :            : {
    2243                 :            :         struct pinctrl_dev **ptr;
    2244                 :            :         int error;
    2245                 :            : 
    2246                 :            :         ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
    2247                 :          0 :         if (!ptr)
    2248                 :            :                 return -ENOMEM;
    2249                 :            : 
    2250                 :            :         error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev);
    2251                 :          0 :         if (error) {
    2252                 :          0 :                 devres_free(ptr);
    2253                 :          0 :                 return error;
    2254                 :            :         }
    2255                 :            : 
    2256                 :          0 :         *ptr = *pctldev;
    2257                 :          0 :         devres_add(dev, ptr);
    2258                 :            : 
    2259                 :          0 :         return 0;
    2260                 :            : }
    2261                 :            : EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init);
    2262                 :            : 
    2263                 :            : /**
    2264                 :            :  * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
    2265                 :            :  * @dev: device for which which resource was allocated
    2266                 :            :  * @pctldev: the pinctrl device to unregister.
    2267                 :            :  */
    2268                 :          0 : void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
    2269                 :            : {
    2270                 :          0 :         WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
    2271                 :            :                                devm_pinctrl_dev_match, pctldev));
    2272                 :          0 : }
    2273                 :            : EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
    2274                 :            : 
    2275                 :          3 : static int __init pinctrl_init(void)
    2276                 :            : {
    2277                 :          3 :         pr_info("initialized pinctrl subsystem\n");
    2278                 :          3 :         pinctrl_init_debugfs();
    2279                 :          3 :         return 0;
    2280                 :            : }
    2281                 :            : 
    2282                 :            : /* init early since many drivers really need to initialized pinmux early */
    2283                 :            : core_initcall(pinctrl_init);
    

Generated by: LCOV version 1.14