LCOV - code coverage report
Current view: top level - drivers/pinctrl - pinmux.c (source / functions) Hit Total Coverage
Test: Real Lines: 124 267 46.4 %
Date: 2020-10-17 15:46:16 Functions: 0 20 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 muxing portions of 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) "pinmux core: " fmt
      14                 :            : 
      15                 :            : #include <linux/kernel.h>
      16                 :            : #include <linux/module.h>
      17                 :            : #include <linux/init.h>
      18                 :            : #include <linux/device.h>
      19                 :            : #include <linux/slab.h>
      20                 :            : #include <linux/radix-tree.h>
      21                 :            : #include <linux/err.h>
      22                 :            : #include <linux/list.h>
      23                 :            : #include <linux/string.h>
      24                 :            : #include <linux/debugfs.h>
      25                 :            : #include <linux/seq_file.h>
      26                 :            : #include <linux/pinctrl/machine.h>
      27                 :            : #include <linux/pinctrl/pinmux.h>
      28                 :            : #include "core.h"
      29                 :            : #include "pinmux.h"
      30                 :            : 
      31                 :          3 : int pinmux_check_ops(struct pinctrl_dev *pctldev)
      32                 :            : {
      33                 :          3 :         const struct pinmux_ops *ops = pctldev->desc->pmxops;
      34                 :            :         unsigned nfuncs;
      35                 :            :         unsigned selector = 0;
      36                 :            : 
      37                 :            :         /* Check that we implement required operations */
      38                 :          3 :         if (!ops ||
      39                 :          3 :             !ops->get_functions_count ||
      40                 :          3 :             !ops->get_function_name ||
      41                 :          3 :             !ops->get_function_groups ||
      42                 :          3 :             !ops->set_mux) {
      43                 :          0 :                 dev_err(pctldev->dev, "pinmux ops lacks necessary functions\n");
      44                 :          0 :                 return -EINVAL;
      45                 :            :         }
      46                 :            :         /* Check that all functions registered have names */
      47                 :          3 :         nfuncs = ops->get_functions_count(pctldev);
      48                 :          3 :         while (selector < nfuncs) {
      49                 :          3 :                 const char *fname = ops->get_function_name(pctldev,
      50                 :            :                                                            selector);
      51                 :          3 :                 if (!fname) {
      52                 :          0 :                         dev_err(pctldev->dev, "pinmux ops has no name for function%u\n",
      53                 :            :                                 selector);
      54                 :          0 :                         return -EINVAL;
      55                 :            :                 }
      56                 :          3 :                 selector++;
      57                 :            :         }
      58                 :            : 
      59                 :            :         return 0;
      60                 :            : }
      61                 :            : 
      62                 :          3 : int pinmux_validate_map(const struct pinctrl_map *map, int i)
      63                 :            : {
      64                 :          3 :         if (!map->data.mux.function) {
      65                 :          0 :                 pr_err("failed to register map %s (%d): no function given\n",
      66                 :            :                        map->name, i);
      67                 :          0 :                 return -EINVAL;
      68                 :            :         }
      69                 :            : 
      70                 :            :         return 0;
      71                 :            : }
      72                 :            : 
      73                 :            : /**
      74                 :            :  * pinmux_can_be_used_for_gpio() - check if a specific pin
      75                 :            :  *      is either muxed to a different function or used as gpio.
      76                 :            :  *
      77                 :            :  * @pin: the pin number in the global pin space
      78                 :            :  *
      79                 :            :  * Controllers not defined as strict will always return true,
      80                 :            :  * menaning that the gpio can be used.
      81                 :            :  */
      82                 :          0 : bool pinmux_can_be_used_for_gpio(struct pinctrl_dev *pctldev, unsigned pin)
      83                 :            : {
      84                 :            :         struct pin_desc *desc = pin_desc_get(pctldev, pin);
      85                 :          0 :         const struct pinmux_ops *ops = pctldev->desc->pmxops;
      86                 :            : 
      87                 :            :         /* Can't inspect pin, assume it can be used */
      88                 :          0 :         if (!desc || !ops)
      89                 :            :                 return true;
      90                 :            : 
      91                 :          0 :         if (ops->strict && desc->mux_usecount)
      92                 :            :                 return false;
      93                 :            : 
      94                 :          0 :         return !(ops->strict && !!desc->gpio_owner);
      95                 :            : }
      96                 :            : 
      97                 :            : /**
      98                 :            :  * pin_request() - request a single pin to be muxed in, typically for GPIO
      99                 :            :  * @pin: the pin number in the global pin space
     100                 :            :  * @owner: a representation of the owner of this pin; typically the device
     101                 :            :  *      name that controls its mux function, or the requested GPIO name
     102                 :            :  * @gpio_range: the range matching the GPIO pin if this is a request for a
     103                 :            :  *      single GPIO pin
     104                 :            :  */
     105                 :          3 : static int pin_request(struct pinctrl_dev *pctldev,
     106                 :            :                        int pin, const char *owner,
     107                 :            :                        struct pinctrl_gpio_range *gpio_range)
     108                 :            : {
     109                 :            :         struct pin_desc *desc;
     110                 :          3 :         const struct pinmux_ops *ops = pctldev->desc->pmxops;
     111                 :            :         int status = -EINVAL;
     112                 :            : 
     113                 :          3 :         desc = pin_desc_get(pctldev, pin);
     114                 :          3 :         if (desc == NULL) {
     115                 :          0 :                 dev_err(pctldev->dev,
     116                 :            :                         "pin %d is not registered so it cannot be requested\n",
     117                 :            :                         pin);
     118                 :          0 :                 goto out;
     119                 :            :         }
     120                 :            : 
     121                 :            :         dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
     122                 :            :                 pin, desc->name, owner);
     123                 :            : 
     124                 :          3 :         if ((!gpio_range || ops->strict) &&
     125                 :          3 :             desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
     126                 :          0 :                 dev_err(pctldev->dev,
     127                 :            :                         "pin %s already requested by %s; cannot claim for %s\n",
     128                 :            :                         desc->name, desc->mux_owner, owner);
     129                 :          0 :                 goto out;
     130                 :            :         }
     131                 :            : 
     132                 :          3 :         if ((gpio_range || ops->strict) && desc->gpio_owner) {
     133                 :          0 :                 dev_err(pctldev->dev,
     134                 :            :                         "pin %s already requested by %s; cannot claim for %s\n",
     135                 :            :                         desc->name, desc->gpio_owner, owner);
     136                 :          0 :                 goto out;
     137                 :            :         }
     138                 :            : 
     139                 :          3 :         if (gpio_range) {
     140                 :          3 :                 desc->gpio_owner = owner;
     141                 :            :         } else {
     142                 :          3 :                 desc->mux_usecount++;
     143                 :          3 :                 if (desc->mux_usecount > 1)
     144                 :            :                         return 0;
     145                 :            : 
     146                 :          3 :                 desc->mux_owner = owner;
     147                 :            :         }
     148                 :            : 
     149                 :            :         /* Let each pin increase references to this module */
     150                 :          3 :         if (!try_module_get(pctldev->owner)) {
     151                 :          0 :                 dev_err(pctldev->dev,
     152                 :            :                         "could not increase module refcount for pin %d\n",
     153                 :            :                         pin);
     154                 :            :                 status = -EINVAL;
     155                 :          0 :                 goto out_free_pin;
     156                 :            :         }
     157                 :            : 
     158                 :            :         /*
     159                 :            :          * If there is no kind of request function for the pin we just assume
     160                 :            :          * we got it by default and proceed.
     161                 :            :          */
     162                 :          3 :         if (gpio_range && ops->gpio_request_enable)
     163                 :            :                 /* This requests and enables a single GPIO pin */
     164                 :          0 :                 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
     165                 :          3 :         else if (ops->request)
     166                 :          0 :                 status = ops->request(pctldev, pin);
     167                 :            :         else
     168                 :            :                 status = 0;
     169                 :            : 
     170                 :          3 :         if (status) {
     171                 :          0 :                 dev_err(pctldev->dev, "request() failed for pin %d\n", pin);
     172                 :          0 :                 module_put(pctldev->owner);
     173                 :            :         }
     174                 :            : 
     175                 :            : out_free_pin:
     176                 :          3 :         if (status) {
     177                 :          0 :                 if (gpio_range) {
     178                 :          0 :                         desc->gpio_owner = NULL;
     179                 :            :                 } else {
     180                 :          0 :                         desc->mux_usecount--;
     181                 :          0 :                         if (!desc->mux_usecount)
     182                 :          0 :                                 desc->mux_owner = NULL;
     183                 :            :                 }
     184                 :            :         }
     185                 :            : out:
     186                 :          3 :         if (status)
     187                 :          0 :                 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
     188                 :            :                         pin, owner, status);
     189                 :            : 
     190                 :          3 :         return status;
     191                 :            : }
     192                 :            : 
     193                 :            : /**
     194                 :            :  * pin_free() - release a single muxed in pin so something else can be muxed
     195                 :            :  * @pctldev: pin controller device handling this pin
     196                 :            :  * @pin: the pin to free
     197                 :            :  * @gpio_range: the range matching the GPIO pin if this is a request for a
     198                 :            :  *      single GPIO pin
     199                 :            :  *
     200                 :            :  * This function returns a pointer to the previous owner. This is used
     201                 :            :  * for callers that dynamically allocate an owner name so it can be freed
     202                 :            :  * once the pin is free. This is done for GPIO request functions.
     203                 :            :  */
     204                 :          3 : static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
     205                 :            :                             struct pinctrl_gpio_range *gpio_range)
     206                 :            : {
     207                 :          3 :         const struct pinmux_ops *ops = pctldev->desc->pmxops;
     208                 :            :         struct pin_desc *desc;
     209                 :            :         const char *owner;
     210                 :            : 
     211                 :          3 :         desc = pin_desc_get(pctldev, pin);
     212                 :          3 :         if (desc == NULL) {
     213                 :          0 :                 dev_err(pctldev->dev,
     214                 :            :                         "pin is not registered so it cannot be freed\n");
     215                 :          0 :                 return NULL;
     216                 :            :         }
     217                 :            : 
     218                 :          3 :         if (!gpio_range) {
     219                 :            :                 /*
     220                 :            :                  * A pin should not be freed more times than allocated.
     221                 :            :                  */
     222                 :          3 :                 if (WARN_ON(!desc->mux_usecount))
     223                 :            :                         return NULL;
     224                 :          3 :                 desc->mux_usecount--;
     225                 :          3 :                 if (desc->mux_usecount)
     226                 :            :                         return NULL;
     227                 :            :         }
     228                 :            : 
     229                 :            :         /*
     230                 :            :          * If there is no kind of request function for the pin we just assume
     231                 :            :          * we got it by default and proceed.
     232                 :            :          */
     233                 :          3 :         if (gpio_range && ops->gpio_disable_free)
     234                 :          0 :                 ops->gpio_disable_free(pctldev, gpio_range, pin);
     235                 :          3 :         else if (ops->free)
     236                 :          3 :                 ops->free(pctldev, pin);
     237                 :            : 
     238                 :          3 :         if (gpio_range) {
     239                 :          0 :                 owner = desc->gpio_owner;
     240                 :          0 :                 desc->gpio_owner = NULL;
     241                 :            :         } else {
     242                 :          3 :                 owner = desc->mux_owner;
     243                 :          3 :                 desc->mux_owner = NULL;
     244                 :          3 :                 desc->mux_setting = NULL;
     245                 :            :         }
     246                 :            : 
     247                 :          3 :         module_put(pctldev->owner);
     248                 :            : 
     249                 :          3 :         return owner;
     250                 :            : }
     251                 :            : 
     252                 :            : /**
     253                 :            :  * pinmux_request_gpio() - request pinmuxing for a GPIO pin
     254                 :            :  * @pctldev: pin controller device affected
     255                 :            :  * @pin: the pin to mux in for GPIO
     256                 :            :  * @range: the applicable GPIO range
     257                 :            :  */
     258                 :          3 : int pinmux_request_gpio(struct pinctrl_dev *pctldev,
     259                 :            :                         struct pinctrl_gpio_range *range,
     260                 :            :                         unsigned pin, unsigned gpio)
     261                 :            : {
     262                 :            :         const char *owner;
     263                 :            :         int ret;
     264                 :            : 
     265                 :            :         /* Conjure some name stating what chip and pin this is taken by */
     266                 :          3 :         owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
     267                 :          3 :         if (!owner)
     268                 :            :                 return -ENOMEM;
     269                 :            : 
     270                 :          3 :         ret = pin_request(pctldev, pin, owner, range);
     271                 :          3 :         if (ret < 0)
     272                 :          0 :                 kfree(owner);
     273                 :            : 
     274                 :          3 :         return ret;
     275                 :            : }
     276                 :            : 
     277                 :            : /**
     278                 :            :  * pinmux_free_gpio() - release a pin from GPIO muxing
     279                 :            :  * @pctldev: the pin controller device for the pin
     280                 :            :  * @pin: the affected currently GPIO-muxed in pin
     281                 :            :  * @range: applicable GPIO range
     282                 :            :  */
     283                 :          0 : void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
     284                 :            :                       struct pinctrl_gpio_range *range)
     285                 :            : {
     286                 :            :         const char *owner;
     287                 :            : 
     288                 :          0 :         owner = pin_free(pctldev, pin, range);
     289                 :          0 :         kfree(owner);
     290                 :          0 : }
     291                 :            : 
     292                 :            : /**
     293                 :            :  * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
     294                 :            :  * @pctldev: the pin controller handling this pin
     295                 :            :  * @range: applicable GPIO range
     296                 :            :  * @pin: the affected GPIO pin in this controller
     297                 :            :  * @input: true if we set the pin as input, false for output
     298                 :            :  */
     299                 :          3 : int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
     300                 :            :                           struct pinctrl_gpio_range *range,
     301                 :            :                           unsigned pin, bool input)
     302                 :            : {
     303                 :            :         const struct pinmux_ops *ops;
     304                 :            :         int ret;
     305                 :            : 
     306                 :          3 :         ops = pctldev->desc->pmxops;
     307                 :            : 
     308                 :          3 :         if (ops->gpio_set_direction)
     309                 :          3 :                 ret = ops->gpio_set_direction(pctldev, range, pin, input);
     310                 :            :         else
     311                 :            :                 ret = 0;
     312                 :            : 
     313                 :          3 :         return ret;
     314                 :            : }
     315                 :            : 
     316                 :          3 : static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
     317                 :            :                                         const char *function)
     318                 :            : {
     319                 :          3 :         const struct pinmux_ops *ops = pctldev->desc->pmxops;
     320                 :          3 :         unsigned nfuncs = ops->get_functions_count(pctldev);
     321                 :            :         unsigned selector = 0;
     322                 :            : 
     323                 :            :         /* See if this pctldev has this function */
     324                 :          3 :         while (selector < nfuncs) {
     325                 :          3 :                 const char *fname = ops->get_function_name(pctldev, selector);
     326                 :            : 
     327                 :          3 :                 if (!strcmp(function, fname))
     328                 :          3 :                         return selector;
     329                 :            : 
     330                 :          3 :                 selector++;
     331                 :            :         }
     332                 :            : 
     333                 :            :         return -EINVAL;
     334                 :            : }
     335                 :            : 
     336                 :          3 : int pinmux_map_to_setting(const struct pinctrl_map *map,
     337                 :            :                           struct pinctrl_setting *setting)
     338                 :            : {
     339                 :          3 :         struct pinctrl_dev *pctldev = setting->pctldev;
     340                 :          3 :         const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
     341                 :            :         char const * const *groups;
     342                 :            :         unsigned num_groups;
     343                 :            :         int ret;
     344                 :            :         const char *group;
     345                 :            : 
     346                 :          3 :         if (!pmxops) {
     347                 :          0 :                 dev_err(pctldev->dev, "does not support mux function\n");
     348                 :          0 :                 return -EINVAL;
     349                 :            :         }
     350                 :            : 
     351                 :          3 :         ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
     352                 :          3 :         if (ret < 0) {
     353                 :          0 :                 dev_err(pctldev->dev, "invalid function %s in map table\n",
     354                 :            :                         map->data.mux.function);
     355                 :          0 :                 return ret;
     356                 :            :         }
     357                 :          3 :         setting->data.mux.func = ret;
     358                 :            : 
     359                 :          3 :         ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
     360                 :            :                                           &groups, &num_groups);
     361                 :          3 :         if (ret < 0) {
     362                 :          0 :                 dev_err(pctldev->dev, "can't query groups for function %s\n",
     363                 :            :                         map->data.mux.function);
     364                 :          0 :                 return ret;
     365                 :            :         }
     366                 :          3 :         if (!num_groups) {
     367                 :          0 :                 dev_err(pctldev->dev,
     368                 :            :                         "function %s can't be selected on any group\n",
     369                 :            :                         map->data.mux.function);
     370                 :          0 :                 return -EINVAL;
     371                 :            :         }
     372                 :          3 :         if (map->data.mux.group) {
     373                 :            :                 group = map->data.mux.group;
     374                 :          3 :                 ret = match_string(groups, num_groups, group);
     375                 :          3 :                 if (ret < 0) {
     376                 :          0 :                         dev_err(pctldev->dev,
     377                 :            :                                 "invalid group \"%s\" for function \"%s\"\n",
     378                 :            :                                 group, map->data.mux.function);
     379                 :          0 :                         return ret;
     380                 :            :                 }
     381                 :            :         } else {
     382                 :          0 :                 group = groups[0];
     383                 :            :         }
     384                 :            : 
     385                 :          3 :         ret = pinctrl_get_group_selector(pctldev, group);
     386                 :          3 :         if (ret < 0) {
     387                 :          0 :                 dev_err(pctldev->dev, "invalid group %s in map table\n",
     388                 :            :                         map->data.mux.group);
     389                 :          0 :                 return ret;
     390                 :            :         }
     391                 :          3 :         setting->data.mux.group = ret;
     392                 :            : 
     393                 :          3 :         return 0;
     394                 :            : }
     395                 :            : 
     396                 :          3 : void pinmux_free_setting(const struct pinctrl_setting *setting)
     397                 :            : {
     398                 :            :         /* This function is currently unused */
     399                 :          3 : }
     400                 :            : 
     401                 :          3 : int pinmux_enable_setting(const struct pinctrl_setting *setting)
     402                 :            : {
     403                 :          3 :         struct pinctrl_dev *pctldev = setting->pctldev;
     404                 :          3 :         const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
     405                 :          3 :         const struct pinmux_ops *ops = pctldev->desc->pmxops;
     406                 :            :         int ret = 0;
     407                 :          3 :         const unsigned *pins = NULL;
     408                 :          3 :         unsigned num_pins = 0;
     409                 :            :         int i;
     410                 :            :         struct pin_desc *desc;
     411                 :            : 
     412                 :          3 :         if (pctlops->get_group_pins)
     413                 :          3 :                 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
     414                 :            :                                               &pins, &num_pins);
     415                 :            : 
     416                 :          3 :         if (ret) {
     417                 :            :                 const char *gname;
     418                 :            : 
     419                 :            :                 /* errors only affect debug data, so just warn */
     420                 :          0 :                 gname = pctlops->get_group_name(pctldev,
     421                 :            :                                                 setting->data.mux.group);
     422                 :          0 :                 dev_warn(pctldev->dev,
     423                 :            :                          "could not get pins for group %s\n",
     424                 :            :                          gname);
     425                 :          0 :                 num_pins = 0;
     426                 :            :         }
     427                 :            : 
     428                 :            :         /* Try to allocate all pins in this group, one by one */
     429                 :          3 :         for (i = 0; i < num_pins; i++) {
     430                 :          3 :                 ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
     431                 :          3 :                 if (ret) {
     432                 :            :                         const char *gname;
     433                 :            :                         const char *pname;
     434                 :            : 
     435                 :          0 :                         desc = pin_desc_get(pctldev, pins[i]);
     436                 :          0 :                         pname = desc ? desc->name : "non-existing";
     437                 :          0 :                         gname = pctlops->get_group_name(pctldev,
     438                 :            :                                                 setting->data.mux.group);
     439                 :          0 :                         dev_err(pctldev->dev,
     440                 :            :                                 "could not request pin %d (%s) from group %s "
     441                 :            :                                 " on device %s\n",
     442                 :            :                                 pins[i], pname, gname,
     443                 :            :                                 pinctrl_dev_get_name(pctldev));
     444                 :          0 :                         goto err_pin_request;
     445                 :            :                 }
     446                 :            :         }
     447                 :            : 
     448                 :            :         /* Now that we have acquired the pins, encode the mux setting */
     449                 :          3 :         for (i = 0; i < num_pins; i++) {
     450                 :          3 :                 desc = pin_desc_get(pctldev, pins[i]);
     451                 :          3 :                 if (desc == NULL) {
     452                 :          0 :                         dev_warn(pctldev->dev,
     453                 :            :                                  "could not get pin desc for pin %d\n",
     454                 :            :                                  pins[i]);
     455                 :          0 :                         continue;
     456                 :            :                 }
     457                 :          3 :                 desc->mux_setting = &(setting->data.mux);
     458                 :            :         }
     459                 :            : 
     460                 :          3 :         ret = ops->set_mux(pctldev, setting->data.mux.func,
     461                 :            :                            setting->data.mux.group);
     462                 :            : 
     463                 :          3 :         if (ret)
     464                 :            :                 goto err_set_mux;
     465                 :            : 
     466                 :            :         return 0;
     467                 :            : 
     468                 :            : err_set_mux:
     469                 :          0 :         for (i = 0; i < num_pins; i++) {
     470                 :          0 :                 desc = pin_desc_get(pctldev, pins[i]);
     471                 :          0 :                 if (desc)
     472                 :          0 :                         desc->mux_setting = NULL;
     473                 :            :         }
     474                 :            : err_pin_request:
     475                 :            :         /* On error release all taken pins */
     476                 :          0 :         while (--i >= 0)
     477                 :          0 :                 pin_free(pctldev, pins[i], NULL);
     478                 :            : 
     479                 :          0 :         return ret;
     480                 :            : }
     481                 :            : 
     482                 :          3 : void pinmux_disable_setting(const struct pinctrl_setting *setting)
     483                 :            : {
     484                 :          3 :         struct pinctrl_dev *pctldev = setting->pctldev;
     485                 :          3 :         const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
     486                 :            :         int ret = 0;
     487                 :          3 :         const unsigned *pins = NULL;
     488                 :          3 :         unsigned num_pins = 0;
     489                 :            :         int i;
     490                 :            :         struct pin_desc *desc;
     491                 :            : 
     492                 :          3 :         if (pctlops->get_group_pins)
     493                 :          3 :                 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
     494                 :            :                                               &pins, &num_pins);
     495                 :          3 :         if (ret) {
     496                 :            :                 const char *gname;
     497                 :            : 
     498                 :            :                 /* errors only affect debug data, so just warn */
     499                 :          0 :                 gname = pctlops->get_group_name(pctldev,
     500                 :            :                                                 setting->data.mux.group);
     501                 :          0 :                 dev_warn(pctldev->dev,
     502                 :            :                          "could not get pins for group %s\n",
     503                 :            :                          gname);
     504                 :          0 :                 num_pins = 0;
     505                 :            :         }
     506                 :            : 
     507                 :            :         /* Flag the descs that no setting is active */
     508                 :          3 :         for (i = 0; i < num_pins; i++) {
     509                 :          3 :                 desc = pin_desc_get(pctldev, pins[i]);
     510                 :          3 :                 if (desc == NULL) {
     511                 :          0 :                         dev_warn(pctldev->dev,
     512                 :            :                                  "could not get pin desc for pin %d\n",
     513                 :            :                                  pins[i]);
     514                 :          0 :                         continue;
     515                 :            :                 }
     516                 :          3 :                 if (desc->mux_setting == &(setting->data.mux)) {
     517                 :          3 :                         pin_free(pctldev, pins[i], NULL);
     518                 :            :                 } else {
     519                 :            :                         const char *gname;
     520                 :            : 
     521                 :          0 :                         gname = pctlops->get_group_name(pctldev,
     522                 :            :                                                 setting->data.mux.group);
     523                 :          0 :                         dev_warn(pctldev->dev,
     524                 :            :                                  "not freeing pin %d (%s) as part of "
     525                 :            :                                  "deactivating group %s - it is already "
     526                 :            :                                  "used for some other setting",
     527                 :            :                                  pins[i], desc->name, gname);
     528                 :            :                 }
     529                 :            :         }
     530                 :          3 : }
     531                 :            : 
     532                 :            : #ifdef CONFIG_DEBUG_FS
     533                 :            : 
     534                 :            : /* Called from pincontrol core */
     535                 :          0 : static int pinmux_functions_show(struct seq_file *s, void *what)
     536                 :            : {
     537                 :          0 :         struct pinctrl_dev *pctldev = s->private;
     538                 :          0 :         const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
     539                 :            :         unsigned nfuncs;
     540                 :            :         unsigned func_selector = 0;
     541                 :            : 
     542                 :          0 :         if (!pmxops)
     543                 :            :                 return 0;
     544                 :            : 
     545                 :          0 :         mutex_lock(&pctldev->mutex);
     546                 :          0 :         nfuncs = pmxops->get_functions_count(pctldev);
     547                 :          0 :         while (func_selector < nfuncs) {
     548                 :          0 :                 const char *func = pmxops->get_function_name(pctldev,
     549                 :            :                                                           func_selector);
     550                 :            :                 const char * const *groups;
     551                 :            :                 unsigned num_groups;
     552                 :            :                 int ret;
     553                 :            :                 int i;
     554                 :            : 
     555                 :          0 :                 ret = pmxops->get_function_groups(pctldev, func_selector,
     556                 :            :                                                   &groups, &num_groups);
     557                 :          0 :                 if (ret) {
     558                 :          0 :                         seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
     559                 :            :                                    func);
     560                 :          0 :                         func_selector++;
     561                 :          0 :                         continue;
     562                 :            :                 }
     563                 :            : 
     564                 :          0 :                 seq_printf(s, "function: %s, groups = [ ", func);
     565                 :          0 :                 for (i = 0; i < num_groups; i++)
     566                 :          0 :                         seq_printf(s, "%s ", groups[i]);
     567                 :          0 :                 seq_puts(s, "]\n");
     568                 :            : 
     569                 :          0 :                 func_selector++;
     570                 :            :         }
     571                 :            : 
     572                 :          0 :         mutex_unlock(&pctldev->mutex);
     573                 :            : 
     574                 :          0 :         return 0;
     575                 :            : }
     576                 :            : 
     577                 :          0 : static int pinmux_pins_show(struct seq_file *s, void *what)
     578                 :            : {
     579                 :          0 :         struct pinctrl_dev *pctldev = s->private;
     580                 :          0 :         const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
     581                 :          0 :         const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
     582                 :            :         unsigned i, pin;
     583                 :            : 
     584                 :          0 :         if (!pmxops)
     585                 :            :                 return 0;
     586                 :            : 
     587                 :          0 :         seq_puts(s, "Pinmux settings per pin\n");
     588                 :          0 :         if (pmxops->strict)
     589                 :          0 :                 seq_puts(s,
     590                 :            :                  "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
     591                 :            :         else
     592                 :          0 :                 seq_puts(s,
     593                 :            :                 "Format: pin (name): mux_owner gpio_owner hog?\n");
     594                 :            : 
     595                 :          0 :         mutex_lock(&pctldev->mutex);
     596                 :            : 
     597                 :            :         /* The pin number can be retrived from the pin controller descriptor */
     598                 :          0 :         for (i = 0; i < pctldev->desc->npins; i++) {
     599                 :            :                 struct pin_desc *desc;
     600                 :            :                 bool is_hog = false;
     601                 :            : 
     602                 :          0 :                 pin = pctldev->desc->pins[i].number;
     603                 :            :                 desc = pin_desc_get(pctldev, pin);
     604                 :            :                 /* Skip if we cannot search the pin */
     605                 :          0 :                 if (desc == NULL)
     606                 :          0 :                         continue;
     607                 :            : 
     608                 :          0 :                 if (desc->mux_owner &&
     609                 :          0 :                     !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
     610                 :            :                         is_hog = true;
     611                 :            : 
     612                 :          0 :                 if (pmxops->strict) {
     613                 :          0 :                         if (desc->mux_owner)
     614                 :          0 :                                 seq_printf(s, "pin %d (%s): device %s%s",
     615                 :            :                                            pin, desc->name, desc->mux_owner,
     616                 :            :                                            is_hog ? " (HOG)" : "");
     617                 :          0 :                         else if (desc->gpio_owner)
     618                 :          0 :                                 seq_printf(s, "pin %d (%s): GPIO %s",
     619                 :            :                                            pin, desc->name, desc->gpio_owner);
     620                 :            :                         else
     621                 :          0 :                                 seq_printf(s, "pin %d (%s): UNCLAIMED",
     622                 :            :                                            pin, desc->name);
     623                 :            :                 } else {
     624                 :            :                         /* For non-strict controllers */
     625                 :          0 :                         seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name,
     626                 :          0 :                                    desc->mux_owner ? desc->mux_owner
     627                 :            :                                    : "(MUX UNCLAIMED)",
     628                 :          0 :                                    desc->gpio_owner ? desc->gpio_owner
     629                 :            :                                    : "(GPIO UNCLAIMED)",
     630                 :            :                                    is_hog ? " (HOG)" : "");
     631                 :            :                 }
     632                 :            : 
     633                 :            :                 /* If mux: print function+group claiming the pin */
     634                 :          0 :                 if (desc->mux_setting)
     635                 :          0 :                         seq_printf(s, " function %s group %s\n",
     636                 :          0 :                                    pmxops->get_function_name(pctldev,
     637                 :            :                                         desc->mux_setting->func),
     638                 :          0 :                                    pctlops->get_group_name(pctldev,
     639                 :          0 :                                         desc->mux_setting->group));
     640                 :            :                 else
     641                 :          0 :                         seq_putc(s, '\n');
     642                 :            :         }
     643                 :            : 
     644                 :          0 :         mutex_unlock(&pctldev->mutex);
     645                 :            : 
     646                 :          0 :         return 0;
     647                 :            : }
     648                 :            : 
     649                 :          0 : void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map)
     650                 :            : {
     651                 :          0 :         seq_printf(s, "group %s\nfunction %s\n",
     652                 :          0 :                 map->data.mux.group ? map->data.mux.group : "(default)",
     653                 :            :                 map->data.mux.function);
     654                 :          0 : }
     655                 :            : 
     656                 :          0 : void pinmux_show_setting(struct seq_file *s,
     657                 :            :                          const struct pinctrl_setting *setting)
     658                 :            : {
     659                 :          0 :         struct pinctrl_dev *pctldev = setting->pctldev;
     660                 :          0 :         const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
     661                 :          0 :         const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
     662                 :            : 
     663                 :          0 :         seq_printf(s, "group: %s (%u) function: %s (%u)\n",
     664                 :          0 :                    pctlops->get_group_name(pctldev, setting->data.mux.group),
     665                 :            :                    setting->data.mux.group,
     666                 :          0 :                    pmxops->get_function_name(pctldev, setting->data.mux.func),
     667                 :            :                    setting->data.mux.func);
     668                 :          0 : }
     669                 :            : 
     670                 :          0 : DEFINE_SHOW_ATTRIBUTE(pinmux_functions);
     671                 :          0 : DEFINE_SHOW_ATTRIBUTE(pinmux_pins);
     672                 :            : 
     673                 :          3 : void pinmux_init_device_debugfs(struct dentry *devroot,
     674                 :            :                          struct pinctrl_dev *pctldev)
     675                 :            : {
     676                 :          3 :         debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
     677                 :            :                             devroot, pctldev, &pinmux_functions_fops);
     678                 :          3 :         debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
     679                 :            :                             devroot, pctldev, &pinmux_pins_fops);
     680                 :          3 : }
     681                 :            : 
     682                 :            : #endif /* CONFIG_DEBUG_FS */
     683                 :            : 
     684                 :            : #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
     685                 :            : 
     686                 :            : /**
     687                 :            :  * pinmux_generic_get_function_count() - returns number of functions
     688                 :            :  * @pctldev: pin controller device
     689                 :            :  */
     690                 :            : int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)
     691                 :            : {
     692                 :            :         return pctldev->num_functions;
     693                 :            : }
     694                 :            : EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);
     695                 :            : 
     696                 :            : /**
     697                 :            :  * pinmux_generic_get_function_name() - returns the function name
     698                 :            :  * @pctldev: pin controller device
     699                 :            :  * @selector: function number
     700                 :            :  */
     701                 :            : const char *
     702                 :            : pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,
     703                 :            :                                  unsigned int selector)
     704                 :            : {
     705                 :            :         struct function_desc *function;
     706                 :            : 
     707                 :            :         function = radix_tree_lookup(&pctldev->pin_function_tree,
     708                 :            :                                      selector);
     709                 :            :         if (!function)
     710                 :            :                 return NULL;
     711                 :            : 
     712                 :            :         return function->name;
     713                 :            : }
     714                 :            : EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);
     715                 :            : 
     716                 :            : /**
     717                 :            :  * pinmux_generic_get_function_groups() - gets the function groups
     718                 :            :  * @pctldev: pin controller device
     719                 :            :  * @selector: function number
     720                 :            :  * @groups: array of pin groups
     721                 :            :  * @num_groups: number of pin groups
     722                 :            :  */
     723                 :            : int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,
     724                 :            :                                        unsigned int selector,
     725                 :            :                                        const char * const **groups,
     726                 :            :                                        unsigned * const num_groups)
     727                 :            : {
     728                 :            :         struct function_desc *function;
     729                 :            : 
     730                 :            :         function = radix_tree_lookup(&pctldev->pin_function_tree,
     731                 :            :                                      selector);
     732                 :            :         if (!function) {
     733                 :            :                 dev_err(pctldev->dev, "%s could not find function%i\n",
     734                 :            :                         __func__, selector);
     735                 :            :                 return -EINVAL;
     736                 :            :         }
     737                 :            :         *groups = function->group_names;
     738                 :            :         *num_groups = function->num_group_names;
     739                 :            : 
     740                 :            :         return 0;
     741                 :            : }
     742                 :            : EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);
     743                 :            : 
     744                 :            : /**
     745                 :            :  * pinmux_generic_get_function() - returns a function based on the number
     746                 :            :  * @pctldev: pin controller device
     747                 :            :  * @group_selector: function number
     748                 :            :  */
     749                 :            : struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev,
     750                 :            :                                                   unsigned int selector)
     751                 :            : {
     752                 :            :         struct function_desc *function;
     753                 :            : 
     754                 :            :         function = radix_tree_lookup(&pctldev->pin_function_tree,
     755                 :            :                                      selector);
     756                 :            :         if (!function)
     757                 :            :                 return NULL;
     758                 :            : 
     759                 :            :         return function;
     760                 :            : }
     761                 :            : EXPORT_SYMBOL_GPL(pinmux_generic_get_function);
     762                 :            : 
     763                 :            : /**
     764                 :            :  * pinmux_generic_add_function() - adds a function group
     765                 :            :  * @pctldev: pin controller device
     766                 :            :  * @name: name of the function
     767                 :            :  * @groups: array of pin groups
     768                 :            :  * @num_groups: number of pin groups
     769                 :            :  * @data: pin controller driver specific data
     770                 :            :  */
     771                 :            : int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
     772                 :            :                                 const char *name,
     773                 :            :                                 const char **groups,
     774                 :            :                                 const unsigned int num_groups,
     775                 :            :                                 void *data)
     776                 :            : {
     777                 :            :         struct function_desc *function;
     778                 :            :         int selector;
     779                 :            : 
     780                 :            :         if (!name)
     781                 :            :                 return -EINVAL;
     782                 :            : 
     783                 :            :         selector = pinmux_func_name_to_selector(pctldev, name);
     784                 :            :         if (selector >= 0)
     785                 :            :                 return selector;
     786                 :            : 
     787                 :            :         selector = pctldev->num_functions;
     788                 :            : 
     789                 :            :         function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
     790                 :            :         if (!function)
     791                 :            :                 return -ENOMEM;
     792                 :            : 
     793                 :            :         function->name = name;
     794                 :            :         function->group_names = groups;
     795                 :            :         function->num_group_names = num_groups;
     796                 :            :         function->data = data;
     797                 :            : 
     798                 :            :         radix_tree_insert(&pctldev->pin_function_tree, selector, function);
     799                 :            : 
     800                 :            :         pctldev->num_functions++;
     801                 :            : 
     802                 :            :         return selector;
     803                 :            : }
     804                 :            : EXPORT_SYMBOL_GPL(pinmux_generic_add_function);
     805                 :            : 
     806                 :            : /**
     807                 :            :  * pinmux_generic_remove_function() - removes a numbered function
     808                 :            :  * @pctldev: pin controller device
     809                 :            :  * @selector: function number
     810                 :            :  *
     811                 :            :  * Note that the caller must take care of locking.
     812                 :            :  */
     813                 :            : int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
     814                 :            :                                    unsigned int selector)
     815                 :            : {
     816                 :            :         struct function_desc *function;
     817                 :            : 
     818                 :            :         function = radix_tree_lookup(&pctldev->pin_function_tree,
     819                 :            :                                      selector);
     820                 :            :         if (!function)
     821                 :            :                 return -ENOENT;
     822                 :            : 
     823                 :            :         radix_tree_delete(&pctldev->pin_function_tree, selector);
     824                 :            :         devm_kfree(pctldev->dev, function);
     825                 :            : 
     826                 :            :         pctldev->num_functions--;
     827                 :            : 
     828                 :            :         return 0;
     829                 :            : }
     830                 :            : EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
     831                 :            : 
     832                 :            : /**
     833                 :            :  * pinmux_generic_free_functions() - removes all functions
     834                 :            :  * @pctldev: pin controller device
     835                 :            :  *
     836                 :            :  * Note that the caller must take care of locking. The pinctrl
     837                 :            :  * functions are allocated with devm_kzalloc() so no need to free
     838                 :            :  * them here.
     839                 :            :  */
     840                 :            : void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
     841                 :            : {
     842                 :            :         struct radix_tree_iter iter;
     843                 :            :         void __rcu **slot;
     844                 :            : 
     845                 :            :         radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
     846                 :            :                 radix_tree_delete(&pctldev->pin_function_tree, iter.index);
     847                 :            : 
     848                 :            :         pctldev->num_functions = 0;
     849                 :            : }
     850                 :            : 
     851                 :            : #endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */
    

Generated by: LCOV version 1.14