Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Core driver for the pin muxing portions of the pin control subsystem
0004  *
0005  * Copyright (C) 2011-2012 ST-Ericsson SA
0006  * Written on behalf of Linaro for ST-Ericsson
0007  * Based on bits of regulator core, gpio core and clk core
0008  *
0009  * Author: Linus Walleij <linus.walleij@linaro.org>
0010  *
0011  * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
0012  */
0013 #define pr_fmt(fmt) "pinmux core: " fmt
0014 
0015 #include <linux/ctype.h>
0016 #include <linux/kernel.h>
0017 #include <linux/module.h>
0018 #include <linux/init.h>
0019 #include <linux/device.h>
0020 #include <linux/slab.h>
0021 #include <linux/radix-tree.h>
0022 #include <linux/err.h>
0023 #include <linux/list.h>
0024 #include <linux/string.h>
0025 #include <linux/debugfs.h>
0026 #include <linux/seq_file.h>
0027 #include <linux/pinctrl/machine.h>
0028 #include <linux/pinctrl/pinmux.h>
0029 #include "core.h"
0030 #include "pinmux.h"
0031 
0032 int pinmux_check_ops(struct pinctrl_dev *pctldev)
0033 {
0034     const struct pinmux_ops *ops = pctldev->desc->pmxops;
0035     unsigned nfuncs;
0036     unsigned selector = 0;
0037 
0038     /* Check that we implement required operations */
0039     if (!ops ||
0040         !ops->get_functions_count ||
0041         !ops->get_function_name ||
0042         !ops->get_function_groups ||
0043         !ops->set_mux) {
0044         dev_err(pctldev->dev, "pinmux ops lacks necessary functions\n");
0045         return -EINVAL;
0046     }
0047     /* Check that all functions registered have names */
0048     nfuncs = ops->get_functions_count(pctldev);
0049     while (selector < nfuncs) {
0050         const char *fname = ops->get_function_name(pctldev,
0051                                selector);
0052         if (!fname) {
0053             dev_err(pctldev->dev, "pinmux ops has no name for function%u\n",
0054                 selector);
0055             return -EINVAL;
0056         }
0057         selector++;
0058     }
0059 
0060     return 0;
0061 }
0062 
0063 int pinmux_validate_map(const struct pinctrl_map *map, int i)
0064 {
0065     if (!map->data.mux.function) {
0066         pr_err("failed to register map %s (%d): no function given\n",
0067                map->name, i);
0068         return -EINVAL;
0069     }
0070 
0071     return 0;
0072 }
0073 
0074 /**
0075  * pinmux_can_be_used_for_gpio() - check if a specific pin
0076  *  is either muxed to a different function or used as gpio.
0077  *
0078  * @pctldev: the associated pin controller device
0079  * @pin: the pin number in the global pin space
0080  *
0081  * Controllers not defined as strict will always return true,
0082  * menaning that the gpio can be used.
0083  */
0084 bool pinmux_can_be_used_for_gpio(struct pinctrl_dev *pctldev, unsigned pin)
0085 {
0086     struct pin_desc *desc = pin_desc_get(pctldev, pin);
0087     const struct pinmux_ops *ops = pctldev->desc->pmxops;
0088 
0089     /* Can't inspect pin, assume it can be used */
0090     if (!desc || !ops)
0091         return true;
0092 
0093     if (ops->strict && desc->mux_usecount)
0094         return false;
0095 
0096     return !(ops->strict && !!desc->gpio_owner);
0097 }
0098 
0099 /**
0100  * pin_request() - request a single pin to be muxed in, typically for GPIO
0101  * @pctldev: the associated pin controller device
0102  * @pin: the pin number in the global pin space
0103  * @owner: a representation of the owner of this pin; typically the device
0104  *  name that controls its mux function, or the requested GPIO name
0105  * @gpio_range: the range matching the GPIO pin if this is a request for a
0106  *  single GPIO pin
0107  */
0108 static int pin_request(struct pinctrl_dev *pctldev,
0109                int pin, const char *owner,
0110                struct pinctrl_gpio_range *gpio_range)
0111 {
0112     struct pin_desc *desc;
0113     const struct pinmux_ops *ops = pctldev->desc->pmxops;
0114     int status = -EINVAL;
0115 
0116     desc = pin_desc_get(pctldev, pin);
0117     if (desc == NULL) {
0118         dev_err(pctldev->dev,
0119             "pin %d is not registered so it cannot be requested\n",
0120             pin);
0121         goto out;
0122     }
0123 
0124     dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
0125         pin, desc->name, owner);
0126 
0127     if ((!gpio_range || ops->strict) &&
0128         desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
0129         dev_err(pctldev->dev,
0130             "pin %s already requested by %s; cannot claim for %s\n",
0131             desc->name, desc->mux_owner, owner);
0132         goto out;
0133     }
0134 
0135     if ((gpio_range || ops->strict) && desc->gpio_owner) {
0136         dev_err(pctldev->dev,
0137             "pin %s already requested by %s; cannot claim for %s\n",
0138             desc->name, desc->gpio_owner, owner);
0139         goto out;
0140     }
0141 
0142     if (gpio_range) {
0143         desc->gpio_owner = owner;
0144     } else {
0145         desc->mux_usecount++;
0146         if (desc->mux_usecount > 1)
0147             return 0;
0148 
0149         desc->mux_owner = owner;
0150     }
0151 
0152     /* Let each pin increase references to this module */
0153     if (!try_module_get(pctldev->owner)) {
0154         dev_err(pctldev->dev,
0155             "could not increase module refcount for pin %d\n",
0156             pin);
0157         status = -EINVAL;
0158         goto out_free_pin;
0159     }
0160 
0161     /*
0162      * If there is no kind of request function for the pin we just assume
0163      * we got it by default and proceed.
0164      */
0165     if (gpio_range && ops->gpio_request_enable)
0166         /* This requests and enables a single GPIO pin */
0167         status = ops->gpio_request_enable(pctldev, gpio_range, pin);
0168     else if (ops->request)
0169         status = ops->request(pctldev, pin);
0170     else
0171         status = 0;
0172 
0173     if (status) {
0174         dev_err(pctldev->dev, "request() failed for pin %d\n", pin);
0175         module_put(pctldev->owner);
0176     }
0177 
0178 out_free_pin:
0179     if (status) {
0180         if (gpio_range) {
0181             desc->gpio_owner = NULL;
0182         } else {
0183             desc->mux_usecount--;
0184             if (!desc->mux_usecount)
0185                 desc->mux_owner = NULL;
0186         }
0187     }
0188 out:
0189     if (status)
0190         dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
0191             pin, owner, status);
0192 
0193     return status;
0194 }
0195 
0196 /**
0197  * pin_free() - release a single muxed in pin so something else can be muxed
0198  * @pctldev: pin controller device handling this pin
0199  * @pin: the pin to free
0200  * @gpio_range: the range matching the GPIO pin if this is a request for a
0201  *  single GPIO pin
0202  *
0203  * This function returns a pointer to the previous owner. This is used
0204  * for callers that dynamically allocate an owner name so it can be freed
0205  * once the pin is free. This is done for GPIO request functions.
0206  */
0207 static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
0208                 struct pinctrl_gpio_range *gpio_range)
0209 {
0210     const struct pinmux_ops *ops = pctldev->desc->pmxops;
0211     struct pin_desc *desc;
0212     const char *owner;
0213 
0214     desc = pin_desc_get(pctldev, pin);
0215     if (desc == NULL) {
0216         dev_err(pctldev->dev,
0217             "pin is not registered so it cannot be freed\n");
0218         return NULL;
0219     }
0220 
0221     if (!gpio_range) {
0222         /*
0223          * A pin should not be freed more times than allocated.
0224          */
0225         if (WARN_ON(!desc->mux_usecount))
0226             return NULL;
0227         desc->mux_usecount--;
0228         if (desc->mux_usecount)
0229             return NULL;
0230     }
0231 
0232     /*
0233      * If there is no kind of request function for the pin we just assume
0234      * we got it by default and proceed.
0235      */
0236     if (gpio_range && ops->gpio_disable_free)
0237         ops->gpio_disable_free(pctldev, gpio_range, pin);
0238     else if (ops->free)
0239         ops->free(pctldev, pin);
0240 
0241     if (gpio_range) {
0242         owner = desc->gpio_owner;
0243         desc->gpio_owner = NULL;
0244     } else {
0245         owner = desc->mux_owner;
0246         desc->mux_owner = NULL;
0247         desc->mux_setting = NULL;
0248     }
0249 
0250     module_put(pctldev->owner);
0251 
0252     return owner;
0253 }
0254 
0255 /**
0256  * pinmux_request_gpio() - request pinmuxing for a GPIO pin
0257  * @pctldev: pin controller device affected
0258  * @pin: the pin to mux in for GPIO
0259  * @range: the applicable GPIO range
0260  * @gpio: number of requested GPIO
0261  */
0262 int pinmux_request_gpio(struct pinctrl_dev *pctldev,
0263             struct pinctrl_gpio_range *range,
0264             unsigned pin, unsigned gpio)
0265 {
0266     const char *owner;
0267     int ret;
0268 
0269     /* Conjure some name stating what chip and pin this is taken by */
0270     owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
0271     if (!owner)
0272         return -ENOMEM;
0273 
0274     ret = pin_request(pctldev, pin, owner, range);
0275     if (ret < 0)
0276         kfree(owner);
0277 
0278     return ret;
0279 }
0280 
0281 /**
0282  * pinmux_free_gpio() - release a pin from GPIO muxing
0283  * @pctldev: the pin controller device for the pin
0284  * @pin: the affected currently GPIO-muxed in pin
0285  * @range: applicable GPIO range
0286  */
0287 void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
0288               struct pinctrl_gpio_range *range)
0289 {
0290     const char *owner;
0291 
0292     owner = pin_free(pctldev, pin, range);
0293     kfree(owner);
0294 }
0295 
0296 /**
0297  * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
0298  * @pctldev: the pin controller handling this pin
0299  * @range: applicable GPIO range
0300  * @pin: the affected GPIO pin in this controller
0301  * @input: true if we set the pin as input, false for output
0302  */
0303 int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
0304               struct pinctrl_gpio_range *range,
0305               unsigned pin, bool input)
0306 {
0307     const struct pinmux_ops *ops;
0308     int ret;
0309 
0310     ops = pctldev->desc->pmxops;
0311 
0312     if (ops->gpio_set_direction)
0313         ret = ops->gpio_set_direction(pctldev, range, pin, input);
0314     else
0315         ret = 0;
0316 
0317     return ret;
0318 }
0319 
0320 static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
0321                     const char *function)
0322 {
0323     const struct pinmux_ops *ops = pctldev->desc->pmxops;
0324     unsigned nfuncs = ops->get_functions_count(pctldev);
0325     unsigned selector = 0;
0326 
0327     /* See if this pctldev has this function */
0328     while (selector < nfuncs) {
0329         const char *fname = ops->get_function_name(pctldev, selector);
0330 
0331         if (!strcmp(function, fname))
0332             return selector;
0333 
0334         selector++;
0335     }
0336 
0337     return -EINVAL;
0338 }
0339 
0340 int pinmux_map_to_setting(const struct pinctrl_map *map,
0341               struct pinctrl_setting *setting)
0342 {
0343     struct pinctrl_dev *pctldev = setting->pctldev;
0344     const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
0345     char const * const *groups;
0346     unsigned num_groups;
0347     int ret;
0348     const char *group;
0349 
0350     if (!pmxops) {
0351         dev_err(pctldev->dev, "does not support mux function\n");
0352         return -EINVAL;
0353     }
0354 
0355     ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
0356     if (ret < 0) {
0357         dev_err(pctldev->dev, "invalid function %s in map table\n",
0358             map->data.mux.function);
0359         return ret;
0360     }
0361     setting->data.mux.func = ret;
0362 
0363     ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
0364                       &groups, &num_groups);
0365     if (ret < 0) {
0366         dev_err(pctldev->dev, "can't query groups for function %s\n",
0367             map->data.mux.function);
0368         return ret;
0369     }
0370     if (!num_groups) {
0371         dev_err(pctldev->dev,
0372             "function %s can't be selected on any group\n",
0373             map->data.mux.function);
0374         return -EINVAL;
0375     }
0376     if (map->data.mux.group) {
0377         group = map->data.mux.group;
0378         ret = match_string(groups, num_groups, group);
0379         if (ret < 0) {
0380             dev_err(pctldev->dev,
0381                 "invalid group \"%s\" for function \"%s\"\n",
0382                 group, map->data.mux.function);
0383             return ret;
0384         }
0385     } else {
0386         group = groups[0];
0387     }
0388 
0389     ret = pinctrl_get_group_selector(pctldev, group);
0390     if (ret < 0) {
0391         dev_err(pctldev->dev, "invalid group %s in map table\n",
0392             map->data.mux.group);
0393         return ret;
0394     }
0395     setting->data.mux.group = ret;
0396 
0397     return 0;
0398 }
0399 
0400 void pinmux_free_setting(const struct pinctrl_setting *setting)
0401 {
0402     /* This function is currently unused */
0403 }
0404 
0405 int pinmux_enable_setting(const struct pinctrl_setting *setting)
0406 {
0407     struct pinctrl_dev *pctldev = setting->pctldev;
0408     const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
0409     const struct pinmux_ops *ops = pctldev->desc->pmxops;
0410     int ret = 0;
0411     const unsigned *pins = NULL;
0412     unsigned num_pins = 0;
0413     int i;
0414     struct pin_desc *desc;
0415 
0416     if (pctlops->get_group_pins)
0417         ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
0418                           &pins, &num_pins);
0419 
0420     if (ret) {
0421         const char *gname;
0422 
0423         /* errors only affect debug data, so just warn */
0424         gname = pctlops->get_group_name(pctldev,
0425                         setting->data.mux.group);
0426         dev_warn(pctldev->dev,
0427              "could not get pins for group %s\n",
0428              gname);
0429         num_pins = 0;
0430     }
0431 
0432     /* Try to allocate all pins in this group, one by one */
0433     for (i = 0; i < num_pins; i++) {
0434         ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
0435         if (ret) {
0436             const char *gname;
0437             const char *pname;
0438 
0439             desc = pin_desc_get(pctldev, pins[i]);
0440             pname = desc ? desc->name : "non-existing";
0441             gname = pctlops->get_group_name(pctldev,
0442                         setting->data.mux.group);
0443             dev_err(pctldev->dev,
0444                 "could not request pin %d (%s) from group %s "
0445                 " on device %s\n",
0446                 pins[i], pname, gname,
0447                 pinctrl_dev_get_name(pctldev));
0448             goto err_pin_request;
0449         }
0450     }
0451 
0452     /* Now that we have acquired the pins, encode the mux setting */
0453     for (i = 0; i < num_pins; i++) {
0454         desc = pin_desc_get(pctldev, pins[i]);
0455         if (desc == NULL) {
0456             dev_warn(pctldev->dev,
0457                  "could not get pin desc for pin %d\n",
0458                  pins[i]);
0459             continue;
0460         }
0461         desc->mux_setting = &(setting->data.mux);
0462     }
0463 
0464     ret = ops->set_mux(pctldev, setting->data.mux.func,
0465                setting->data.mux.group);
0466 
0467     if (ret)
0468         goto err_set_mux;
0469 
0470     return 0;
0471 
0472 err_set_mux:
0473     for (i = 0; i < num_pins; i++) {
0474         desc = pin_desc_get(pctldev, pins[i]);
0475         if (desc)
0476             desc->mux_setting = NULL;
0477     }
0478 err_pin_request:
0479     /* On error release all taken pins */
0480     while (--i >= 0)
0481         pin_free(pctldev, pins[i], NULL);
0482 
0483     return ret;
0484 }
0485 
0486 void pinmux_disable_setting(const struct pinctrl_setting *setting)
0487 {
0488     struct pinctrl_dev *pctldev = setting->pctldev;
0489     const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
0490     int ret = 0;
0491     const unsigned *pins = NULL;
0492     unsigned num_pins = 0;
0493     int i;
0494     struct pin_desc *desc;
0495 
0496     if (pctlops->get_group_pins)
0497         ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
0498                           &pins, &num_pins);
0499     if (ret) {
0500         const char *gname;
0501 
0502         /* errors only affect debug data, so just warn */
0503         gname = pctlops->get_group_name(pctldev,
0504                         setting->data.mux.group);
0505         dev_warn(pctldev->dev,
0506              "could not get pins for group %s\n",
0507              gname);
0508         num_pins = 0;
0509     }
0510 
0511     /* Flag the descs that no setting is active */
0512     for (i = 0; i < num_pins; i++) {
0513         desc = pin_desc_get(pctldev, pins[i]);
0514         if (desc == NULL) {
0515             dev_warn(pctldev->dev,
0516                  "could not get pin desc for pin %d\n",
0517                  pins[i]);
0518             continue;
0519         }
0520         if (desc->mux_setting == &(setting->data.mux)) {
0521             pin_free(pctldev, pins[i], NULL);
0522         } else {
0523             const char *gname;
0524 
0525             gname = pctlops->get_group_name(pctldev,
0526                         setting->data.mux.group);
0527             dev_warn(pctldev->dev,
0528                  "not freeing pin %d (%s) as part of "
0529                  "deactivating group %s - it is already "
0530                  "used for some other setting",
0531                  pins[i], desc->name, gname);
0532         }
0533     }
0534 }
0535 
0536 #ifdef CONFIG_DEBUG_FS
0537 
0538 /* Called from pincontrol core */
0539 static int pinmux_functions_show(struct seq_file *s, void *what)
0540 {
0541     struct pinctrl_dev *pctldev = s->private;
0542     const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
0543     unsigned nfuncs;
0544     unsigned func_selector = 0;
0545 
0546     if (!pmxops)
0547         return 0;
0548 
0549     mutex_lock(&pctldev->mutex);
0550     nfuncs = pmxops->get_functions_count(pctldev);
0551     while (func_selector < nfuncs) {
0552         const char *func = pmxops->get_function_name(pctldev,
0553                               func_selector);
0554         const char * const *groups;
0555         unsigned num_groups;
0556         int ret;
0557         int i;
0558 
0559         ret = pmxops->get_function_groups(pctldev, func_selector,
0560                           &groups, &num_groups);
0561         if (ret) {
0562             seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
0563                    func);
0564             func_selector++;
0565             continue;
0566         }
0567 
0568         seq_printf(s, "function %d: %s, groups = [ ", func_selector, func);
0569         for (i = 0; i < num_groups; i++)
0570             seq_printf(s, "%s ", groups[i]);
0571         seq_puts(s, "]\n");
0572 
0573         func_selector++;
0574     }
0575 
0576     mutex_unlock(&pctldev->mutex);
0577 
0578     return 0;
0579 }
0580 
0581 static int pinmux_pins_show(struct seq_file *s, void *what)
0582 {
0583     struct pinctrl_dev *pctldev = s->private;
0584     const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
0585     const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
0586     unsigned i, pin;
0587 
0588     if (!pmxops)
0589         return 0;
0590 
0591     seq_puts(s, "Pinmux settings per pin\n");
0592     if (pmxops->strict)
0593         seq_puts(s,
0594          "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
0595     else
0596         seq_puts(s,
0597         "Format: pin (name): mux_owner gpio_owner hog?\n");
0598 
0599     mutex_lock(&pctldev->mutex);
0600 
0601     /* The pin number can be retrived from the pin controller descriptor */
0602     for (i = 0; i < pctldev->desc->npins; i++) {
0603         struct pin_desc *desc;
0604         bool is_hog = false;
0605 
0606         pin = pctldev->desc->pins[i].number;
0607         desc = pin_desc_get(pctldev, pin);
0608         /* Skip if we cannot search the pin */
0609         if (desc == NULL)
0610             continue;
0611 
0612         if (desc->mux_owner &&
0613             !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
0614             is_hog = true;
0615 
0616         if (pmxops->strict) {
0617             if (desc->mux_owner)
0618                 seq_printf(s, "pin %d (%s): device %s%s",
0619                        pin, desc->name, desc->mux_owner,
0620                        is_hog ? " (HOG)" : "");
0621             else if (desc->gpio_owner)
0622                 seq_printf(s, "pin %d (%s): GPIO %s",
0623                        pin, desc->name, desc->gpio_owner);
0624             else
0625                 seq_printf(s, "pin %d (%s): UNCLAIMED",
0626                        pin, desc->name);
0627         } else {
0628             /* For non-strict controllers */
0629             seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name,
0630                    desc->mux_owner ? desc->mux_owner
0631                    : "(MUX UNCLAIMED)",
0632                    desc->gpio_owner ? desc->gpio_owner
0633                    : "(GPIO UNCLAIMED)",
0634                    is_hog ? " (HOG)" : "");
0635         }
0636 
0637         /* If mux: print function+group claiming the pin */
0638         if (desc->mux_setting)
0639             seq_printf(s, " function %s group %s\n",
0640                    pmxops->get_function_name(pctldev,
0641                     desc->mux_setting->func),
0642                    pctlops->get_group_name(pctldev,
0643                     desc->mux_setting->group));
0644         else
0645             seq_putc(s, '\n');
0646     }
0647 
0648     mutex_unlock(&pctldev->mutex);
0649 
0650     return 0;
0651 }
0652 
0653 void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map)
0654 {
0655     seq_printf(s, "group %s\nfunction %s\n",
0656         map->data.mux.group ? map->data.mux.group : "(default)",
0657         map->data.mux.function);
0658 }
0659 
0660 void pinmux_show_setting(struct seq_file *s,
0661              const struct pinctrl_setting *setting)
0662 {
0663     struct pinctrl_dev *pctldev = setting->pctldev;
0664     const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
0665     const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
0666 
0667     seq_printf(s, "group: %s (%u) function: %s (%u)\n",
0668            pctlops->get_group_name(pctldev, setting->data.mux.group),
0669            setting->data.mux.group,
0670            pmxops->get_function_name(pctldev, setting->data.mux.func),
0671            setting->data.mux.func);
0672 }
0673 
0674 DEFINE_SHOW_ATTRIBUTE(pinmux_functions);
0675 DEFINE_SHOW_ATTRIBUTE(pinmux_pins);
0676 
0677 #define PINMUX_SELECT_MAX 128
0678 static ssize_t pinmux_select(struct file *file, const char __user *user_buf,
0679                    size_t len, loff_t *ppos)
0680 {
0681     struct seq_file *sfile = file->private_data;
0682     struct pinctrl_dev *pctldev = sfile->private;
0683     const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
0684     const char *const *groups;
0685     char *buf, *gname, *fname;
0686     unsigned int num_groups;
0687     int fsel, gsel, ret;
0688 
0689     if (len > PINMUX_SELECT_MAX)
0690         return -ENOMEM;
0691 
0692     buf = kzalloc(PINMUX_SELECT_MAX, GFP_KERNEL);
0693     if (!buf)
0694         return -ENOMEM;
0695 
0696     ret = strncpy_from_user(buf, user_buf, PINMUX_SELECT_MAX);
0697     if (ret < 0)
0698         goto exit_free_buf;
0699     buf[len-1] = '\0';
0700 
0701     /* remove leading and trailing spaces of input buffer */
0702     gname = strstrip(buf);
0703     if (*gname == '\0') {
0704         ret = -EINVAL;
0705         goto exit_free_buf;
0706     }
0707 
0708     /* find a separator which is a spacelike character */
0709     for (fname = gname; !isspace(*fname); fname++) {
0710         if (*fname == '\0') {
0711             ret = -EINVAL;
0712             goto exit_free_buf;
0713         }
0714     }
0715     *fname = '\0';
0716 
0717     /* drop extra spaces between function and group names */
0718     fname = skip_spaces(fname + 1);
0719     if (*fname == '\0') {
0720         ret = -EINVAL;
0721         goto exit_free_buf;
0722     }
0723 
0724     ret = pinmux_func_name_to_selector(pctldev, fname);
0725     if (ret < 0) {
0726         dev_err(pctldev->dev, "invalid function %s in map table\n", fname);
0727         goto exit_free_buf;
0728     }
0729     fsel = ret;
0730 
0731     ret = pmxops->get_function_groups(pctldev, fsel, &groups, &num_groups);
0732     if (ret) {
0733         dev_err(pctldev->dev, "no groups for function %d (%s)", fsel, fname);
0734         goto exit_free_buf;
0735     }
0736 
0737     ret = match_string(groups, num_groups, gname);
0738     if (ret < 0) {
0739         dev_err(pctldev->dev, "invalid group %s", gname);
0740         goto exit_free_buf;
0741     }
0742 
0743     ret = pinctrl_get_group_selector(pctldev, gname);
0744     if (ret < 0) {
0745         dev_err(pctldev->dev, "failed to get group selector for %s", gname);
0746         goto exit_free_buf;
0747     }
0748     gsel = ret;
0749 
0750     ret = pmxops->set_mux(pctldev, fsel, gsel);
0751     if (ret) {
0752         dev_err(pctldev->dev, "set_mux() failed: %d", ret);
0753         goto exit_free_buf;
0754     }
0755     ret = len;
0756 
0757 exit_free_buf:
0758     kfree(buf);
0759 
0760     return ret;
0761 }
0762 
0763 static int pinmux_select_open(struct inode *inode, struct file *file)
0764 {
0765     return single_open(file, NULL, inode->i_private);
0766 }
0767 
0768 static const struct file_operations pinmux_select_ops = {
0769     .owner = THIS_MODULE,
0770     .open = pinmux_select_open,
0771     .write = pinmux_select,
0772     .llseek = no_llseek,
0773     .release = single_release,
0774 };
0775 
0776 void pinmux_init_device_debugfs(struct dentry *devroot,
0777              struct pinctrl_dev *pctldev)
0778 {
0779     debugfs_create_file("pinmux-functions", 0444,
0780                 devroot, pctldev, &pinmux_functions_fops);
0781     debugfs_create_file("pinmux-pins", 0444,
0782                 devroot, pctldev, &pinmux_pins_fops);
0783     debugfs_create_file("pinmux-select", 0200,
0784                 devroot, pctldev, &pinmux_select_ops);
0785 }
0786 
0787 #endif /* CONFIG_DEBUG_FS */
0788 
0789 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
0790 
0791 /**
0792  * pinmux_generic_get_function_count() - returns number of functions
0793  * @pctldev: pin controller device
0794  */
0795 int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)
0796 {
0797     return pctldev->num_functions;
0798 }
0799 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);
0800 
0801 /**
0802  * pinmux_generic_get_function_name() - returns the function name
0803  * @pctldev: pin controller device
0804  * @selector: function number
0805  */
0806 const char *
0807 pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,
0808                  unsigned int selector)
0809 {
0810     struct function_desc *function;
0811 
0812     function = radix_tree_lookup(&pctldev->pin_function_tree,
0813                      selector);
0814     if (!function)
0815         return NULL;
0816 
0817     return function->name;
0818 }
0819 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);
0820 
0821 /**
0822  * pinmux_generic_get_function_groups() - gets the function groups
0823  * @pctldev: pin controller device
0824  * @selector: function number
0825  * @groups: array of pin groups
0826  * @num_groups: number of pin groups
0827  */
0828 int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,
0829                        unsigned int selector,
0830                        const char * const **groups,
0831                        unsigned * const num_groups)
0832 {
0833     struct function_desc *function;
0834 
0835     function = radix_tree_lookup(&pctldev->pin_function_tree,
0836                      selector);
0837     if (!function) {
0838         dev_err(pctldev->dev, "%s could not find function%i\n",
0839             __func__, selector);
0840         return -EINVAL;
0841     }
0842     *groups = function->group_names;
0843     *num_groups = function->num_group_names;
0844 
0845     return 0;
0846 }
0847 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);
0848 
0849 /**
0850  * pinmux_generic_get_function() - returns a function based on the number
0851  * @pctldev: pin controller device
0852  * @selector: function number
0853  */
0854 struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev,
0855                           unsigned int selector)
0856 {
0857     struct function_desc *function;
0858 
0859     function = radix_tree_lookup(&pctldev->pin_function_tree,
0860                      selector);
0861     if (!function)
0862         return NULL;
0863 
0864     return function;
0865 }
0866 EXPORT_SYMBOL_GPL(pinmux_generic_get_function);
0867 
0868 /**
0869  * pinmux_generic_add_function() - adds a function group
0870  * @pctldev: pin controller device
0871  * @name: name of the function
0872  * @groups: array of pin groups
0873  * @num_groups: number of pin groups
0874  * @data: pin controller driver specific data
0875  */
0876 int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
0877                 const char *name,
0878                 const char * const *groups,
0879                 const unsigned int num_groups,
0880                 void *data)
0881 {
0882     struct function_desc *function;
0883     int selector;
0884 
0885     if (!name)
0886         return -EINVAL;
0887 
0888     selector = pinmux_func_name_to_selector(pctldev, name);
0889     if (selector >= 0)
0890         return selector;
0891 
0892     selector = pctldev->num_functions;
0893 
0894     function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
0895     if (!function)
0896         return -ENOMEM;
0897 
0898     function->name = name;
0899     function->group_names = groups;
0900     function->num_group_names = num_groups;
0901     function->data = data;
0902 
0903     radix_tree_insert(&pctldev->pin_function_tree, selector, function);
0904 
0905     pctldev->num_functions++;
0906 
0907     return selector;
0908 }
0909 EXPORT_SYMBOL_GPL(pinmux_generic_add_function);
0910 
0911 /**
0912  * pinmux_generic_remove_function() - removes a numbered function
0913  * @pctldev: pin controller device
0914  * @selector: function number
0915  *
0916  * Note that the caller must take care of locking.
0917  */
0918 int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
0919                    unsigned int selector)
0920 {
0921     struct function_desc *function;
0922 
0923     function = radix_tree_lookup(&pctldev->pin_function_tree,
0924                      selector);
0925     if (!function)
0926         return -ENOENT;
0927 
0928     radix_tree_delete(&pctldev->pin_function_tree, selector);
0929     devm_kfree(pctldev->dev, function);
0930 
0931     pctldev->num_functions--;
0932 
0933     return 0;
0934 }
0935 EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
0936 
0937 /**
0938  * pinmux_generic_free_functions() - removes all functions
0939  * @pctldev: pin controller device
0940  *
0941  * Note that the caller must take care of locking. The pinctrl
0942  * functions are allocated with devm_kzalloc() so no need to free
0943  * them here.
0944  */
0945 void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
0946 {
0947     struct radix_tree_iter iter;
0948     void __rcu **slot;
0949 
0950     radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
0951         radix_tree_delete(&pctldev->pin_function_tree, iter.index);
0952 
0953     pctldev->num_functions = 0;
0954 }
0955 
0956 #endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */