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