linux/drivers/pinctrl/core.c
<<
>>
Prefs
   1/*
   2 * Core driver for the pin control subsystem
   3 *
   4 * Copyright (C) 2011 ST-Ericsson SA
   5 * Written on behalf of Linaro for ST-Ericsson
   6 * Based on bits of regulator core, gpio core and clk core
   7 *
   8 * Author: Linus Walleij <linus.walleij@linaro.org>
   9 *
  10 * License terms: GNU General Public License (GPL) version 2
  11 */
  12#define pr_fmt(fmt) "pinctrl core: " fmt
  13
  14#include <linux/kernel.h>
  15#include <linux/export.h>
  16#include <linux/init.h>
  17#include <linux/device.h>
  18#include <linux/slab.h>
  19#include <linux/radix-tree.h>
  20#include <linux/err.h>
  21#include <linux/list.h>
  22#include <linux/mutex.h>
  23#include <linux/spinlock.h>
  24#include <linux/sysfs.h>
  25#include <linux/debugfs.h>
  26#include <linux/seq_file.h>
  27#include <linux/pinctrl/pinctrl.h>
  28#include <linux/pinctrl/machine.h>
  29#include "core.h"
  30#include "pinmux.h"
  31
  32/* Global list of pin control devices */
  33static DEFINE_MUTEX(pinctrldev_list_mutex);
  34static LIST_HEAD(pinctrldev_list);
  35
  36static void pinctrl_dev_release(struct device *dev)
  37{
  38        struct pinctrl_dev *pctldev = dev_get_drvdata(dev);
  39        kfree(pctldev);
  40}
  41
  42const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
  43{
  44        /* We're not allowed to register devices without name */
  45        return pctldev->desc->name;
  46}
  47EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
  48
  49void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
  50{
  51        return pctldev->driver_data;
  52}
  53EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
  54
  55/**
  56 * get_pinctrl_dev_from_dev() - look up pin controller device
  57 * @dev: a device pointer, this may be NULL but then devname needs to be
  58 *      defined instead
  59 * @devname: the name of a device instance, as returned by dev_name(), this
  60 *      may be NULL but then dev needs to be defined instead
  61 *
  62 * Looks up a pin control device matching a certain device name or pure device
  63 * pointer, the pure device pointer will take precedence.
  64 */
  65struct pinctrl_dev *get_pinctrl_dev_from_dev(struct device *dev,
  66                                             const char *devname)
  67{
  68        struct pinctrl_dev *pctldev = NULL;
  69        bool found = false;
  70
  71        mutex_lock(&pinctrldev_list_mutex);
  72        list_for_each_entry(pctldev, &pinctrldev_list, node) {
  73                if (dev &&  &pctldev->dev == dev) {
  74                        /* Matched on device pointer */
  75                        found = true;
  76                        break;
  77                }
  78
  79                if (devname &&
  80                    !strcmp(dev_name(&pctldev->dev), devname)) {
  81                        /* Matched on device name */
  82                        found = true;
  83                        break;
  84                }
  85        }
  86        mutex_unlock(&pinctrldev_list_mutex);
  87
  88        return found ? pctldev : NULL;
  89}
  90
  91struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, int pin)
  92{
  93        struct pin_desc *pindesc;
  94        unsigned long flags;
  95
  96        spin_lock_irqsave(&pctldev->pin_desc_tree_lock, flags);
  97        pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, pin);
  98        spin_unlock_irqrestore(&pctldev->pin_desc_tree_lock, flags);
  99
 100        return pindesc;
 101}
 102
 103/**
 104 * pin_is_valid() - check if pin exists on controller
 105 * @pctldev: the pin control device to check the pin on
 106 * @pin: pin to check, use the local pin controller index number
 107 *
 108 * This tells us whether a certain pin exist on a certain pin controller or
 109 * not. Pin lists may be sparse, so some pins may not exist.
 110 */
 111bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
 112{
 113        struct pin_desc *pindesc;
 114
 115        if (pin < 0)
 116                return false;
 117
 118        pindesc = pin_desc_get(pctldev, pin);
 119        if (pindesc == NULL)
 120                return false;
 121
 122        return true;
 123}
 124EXPORT_SYMBOL_GPL(pin_is_valid);
 125
 126/* Deletes a range of pin descriptors */
 127static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
 128                                  const struct pinctrl_pin_desc *pins,
 129                                  unsigned num_pins)
 130{
 131        int i;
 132
 133        spin_lock(&pctldev->pin_desc_tree_lock);
 134        for (i = 0; i < num_pins; i++) {
 135                struct pin_desc *pindesc;
 136
 137                pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
 138                                            pins[i].number);
 139                if (pindesc != NULL) {
 140                        radix_tree_delete(&pctldev->pin_desc_tree,
 141                                          pins[i].number);
 142                }
 143                kfree(pindesc);
 144        }
 145        spin_unlock(&pctldev->pin_desc_tree_lock);
 146}
 147
 148static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
 149                                    unsigned number, const char *name)
 150{
 151        struct pin_desc *pindesc;
 152
 153        pindesc = pin_desc_get(pctldev, number);
 154        if (pindesc != NULL) {
 155                pr_err("pin %d already registered on %s\n", number,
 156                       pctldev->desc->name);
 157                return -EINVAL;
 158        }
 159
 160        pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
 161        if (pindesc == NULL)
 162                return -ENOMEM;
 163        spin_lock_init(&pindesc->lock);
 164
 165        /* Set owner */
 166        pindesc->pctldev = pctldev;
 167
 168        /* Copy basic pin info */
 169        pindesc->name = name;
 170
 171        spin_lock(&pctldev->pin_desc_tree_lock);
 172        radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
 173        spin_unlock(&pctldev->pin_desc_tree_lock);
 174        pr_debug("registered pin %d (%s) on %s\n",
 175                 number, name ? name : "(unnamed)", pctldev->desc->name);
 176        return 0;
 177}
 178
 179static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
 180                                 struct pinctrl_pin_desc const *pins,
 181                                 unsigned num_descs)
 182{
 183        unsigned i;
 184        int ret = 0;
 185
 186        for (i = 0; i < num_descs; i++) {
 187                ret = pinctrl_register_one_pin(pctldev,
 188                                               pins[i].number, pins[i].name);
 189                if (ret)
 190                        return ret;
 191        }
 192
 193        return 0;
 194}
 195
 196/**
 197 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
 198 * @pctldev: pin controller device to check
 199 * @gpio: gpio pin to check taken from the global GPIO pin space
 200 *
 201 * Tries to match a GPIO pin number to the ranges handled by a certain pin
 202 * controller, return the range or NULL
 203 */
 204static struct pinctrl_gpio_range *
 205pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
 206{
 207        struct pinctrl_gpio_range *range = NULL;
 208
 209        /* Loop over the ranges */
 210        mutex_lock(&pctldev->gpio_ranges_lock);
 211        list_for_each_entry(range, &pctldev->gpio_ranges, node) {
 212                /* Check if we're in the valid range */
 213                if (gpio >= range->base &&
 214                    gpio < range->base + range->npins) {
 215                        mutex_unlock(&pctldev->gpio_ranges_lock);
 216                        return range;
 217                }
 218        }
 219        mutex_unlock(&pctldev->gpio_ranges_lock);
 220
 221        return NULL;
 222}
 223
 224/**
 225 * pinctrl_get_device_gpio_range() - find device for GPIO range
 226 * @gpio: the pin to locate the pin controller for
 227 * @outdev: the pin control device if found
 228 * @outrange: the GPIO range if found
 229 *
 230 * Find the pin controller handling a certain GPIO pin from the pinspace of
 231 * the GPIO subsystem, return the device and the matching GPIO range. Returns
 232 * negative if the GPIO range could not be found in any device.
 233 */
 234int pinctrl_get_device_gpio_range(unsigned gpio,
 235                                struct pinctrl_dev **outdev,
 236                                struct pinctrl_gpio_range **outrange)
 237{
 238        struct pinctrl_dev *pctldev = NULL;
 239
 240        /* Loop over the pin controllers */
 241        mutex_lock(&pinctrldev_list_mutex);
 242        list_for_each_entry(pctldev, &pinctrldev_list, node) {
 243                struct pinctrl_gpio_range *range;
 244
 245                range = pinctrl_match_gpio_range(pctldev, gpio);
 246                if (range != NULL) {
 247                        *outdev = pctldev;
 248                        *outrange = range;
 249                        mutex_unlock(&pinctrldev_list_mutex);
 250                        return 0;
 251                }
 252        }
 253        mutex_unlock(&pinctrldev_list_mutex);
 254
 255        return -EINVAL;
 256}
 257
 258/**
 259 * pinctrl_add_gpio_range() - register a GPIO range for a controller
 260 * @pctldev: pin controller device to add the range to
 261 * @range: the GPIO range to add
 262 *
 263 * This adds a range of GPIOs to be handled by a certain pin controller. Call
 264 * this to register handled ranges after registering your pin controller.
 265 */
 266void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
 267                            struct pinctrl_gpio_range *range)
 268{
 269        mutex_lock(&pctldev->gpio_ranges_lock);
 270        list_add(&range->node, &pctldev->gpio_ranges);
 271        mutex_unlock(&pctldev->gpio_ranges_lock);
 272}
 273
 274/**
 275 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
 276 * @pctldev: pin controller device to remove the range from
 277 * @range: the GPIO range to remove
 278 */
 279void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
 280                               struct pinctrl_gpio_range *range)
 281{
 282        mutex_lock(&pctldev->gpio_ranges_lock);
 283        list_del(&range->node);
 284        mutex_unlock(&pctldev->gpio_ranges_lock);
 285}
 286
 287#ifdef CONFIG_DEBUG_FS
 288
 289static int pinctrl_pins_show(struct seq_file *s, void *what)
 290{
 291        struct pinctrl_dev *pctldev = s->private;
 292        const struct pinctrl_ops *ops = pctldev->desc->pctlops;
 293        unsigned pin;
 294
 295        seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
 296        seq_printf(s, "max pin number: %d\n", pctldev->desc->maxpin);
 297
 298        /* The highest pin number need to be included in the loop, thus <= */
 299        for (pin = 0; pin <= pctldev->desc->maxpin; pin++) {
 300                struct pin_desc *desc;
 301
 302                desc = pin_desc_get(pctldev, pin);
 303                /* Pin space may be sparse */
 304                if (desc == NULL)
 305                        continue;
 306
 307                seq_printf(s, "pin %d (%s) ", pin,
 308                           desc->name ? desc->name : "unnamed");
 309
 310                /* Driver-specific info per pin */
 311                if (ops->pin_dbg_show)
 312                        ops->pin_dbg_show(pctldev, s, pin);
 313
 314                seq_puts(s, "\n");
 315        }
 316
 317        return 0;
 318}
 319
 320static int pinctrl_groups_show(struct seq_file *s, void *what)
 321{
 322        struct pinctrl_dev *pctldev = s->private;
 323        const struct pinctrl_ops *ops = pctldev->desc->pctlops;
 324        unsigned selector = 0;
 325
 326        /* No grouping */
 327        if (!ops)
 328                return 0;
 329
 330        seq_puts(s, "registered pin groups:\n");
 331        while (ops->list_groups(pctldev, selector) >= 0) {
 332                const unsigned *pins;
 333                unsigned num_pins;
 334                const char *gname = ops->get_group_name(pctldev, selector);
 335                int ret;
 336                int i;
 337
 338                ret = ops->get_group_pins(pctldev, selector,
 339                                          &pins, &num_pins);
 340                if (ret)
 341                        seq_printf(s, "%s [ERROR GETTING PINS]\n",
 342                                   gname);
 343                else {
 344                        seq_printf(s, "group: %s, pins = [ ", gname);
 345                        for (i = 0; i < num_pins; i++)
 346                                seq_printf(s, "%d ", pins[i]);
 347                        seq_puts(s, "]\n");
 348                }
 349                selector++;
 350        }
 351
 352
 353        return 0;
 354}
 355
 356static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
 357{
 358        struct pinctrl_dev *pctldev = s->private;
 359        struct pinctrl_gpio_range *range = NULL;
 360
 361        seq_puts(s, "GPIO ranges handled:\n");
 362
 363        /* Loop over the ranges */
 364        mutex_lock(&pctldev->gpio_ranges_lock);
 365        list_for_each_entry(range, &pctldev->gpio_ranges, node) {
 366                seq_printf(s, "%u: %s [%u - %u]\n", range->id, range->name,
 367                           range->base, (range->base + range->npins - 1));
 368        }
 369        mutex_unlock(&pctldev->gpio_ranges_lock);
 370
 371        return 0;
 372}
 373
 374static int pinctrl_devices_show(struct seq_file *s, void *what)
 375{
 376        struct pinctrl_dev *pctldev;
 377
 378        seq_puts(s, "name [pinmux]\n");
 379        mutex_lock(&pinctrldev_list_mutex);
 380        list_for_each_entry(pctldev, &pinctrldev_list, node) {
 381                seq_printf(s, "%s ", pctldev->desc->name);
 382                if (pctldev->desc->pmxops)
 383                        seq_puts(s, "yes");
 384                else
 385                        seq_puts(s, "no");
 386                seq_puts(s, "\n");
 387        }
 388        mutex_unlock(&pinctrldev_list_mutex);
 389
 390        return 0;
 391}
 392
 393static int pinctrl_pins_open(struct inode *inode, struct file *file)
 394{
 395        return single_open(file, pinctrl_pins_show, inode->i_private);
 396}
 397
 398static int pinctrl_groups_open(struct inode *inode, struct file *file)
 399{
 400        return single_open(file, pinctrl_groups_show, inode->i_private);
 401}
 402
 403static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
 404{
 405        return single_open(file, pinctrl_gpioranges_show, inode->i_private);
 406}
 407
 408static int pinctrl_devices_open(struct inode *inode, struct file *file)
 409{
 410        return single_open(file, pinctrl_devices_show, NULL);
 411}
 412
 413static const struct file_operations pinctrl_pins_ops = {
 414        .open           = pinctrl_pins_open,
 415        .read           = seq_read,
 416        .llseek         = seq_lseek,
 417        .release        = single_release,
 418};
 419
 420static const struct file_operations pinctrl_groups_ops = {
 421        .open           = pinctrl_groups_open,
 422        .read           = seq_read,
 423        .llseek         = seq_lseek,
 424        .release        = single_release,
 425};
 426
 427static const struct file_operations pinctrl_gpioranges_ops = {
 428        .open           = pinctrl_gpioranges_open,
 429        .read           = seq_read,
 430        .llseek         = seq_lseek,
 431        .release        = single_release,
 432};
 433
 434static const struct file_operations pinctrl_devices_ops = {
 435        .open           = pinctrl_devices_open,
 436        .read           = seq_read,
 437        .llseek         = seq_lseek,
 438        .release        = single_release,
 439};
 440
 441static struct dentry *debugfs_root;
 442
 443static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
 444{
 445        static struct dentry *device_root;
 446
 447        device_root = debugfs_create_dir(dev_name(&pctldev->dev),
 448                                         debugfs_root);
 449        if (IS_ERR(device_root) || !device_root) {
 450                pr_warn("failed to create debugfs directory for %s\n",
 451                        dev_name(&pctldev->dev));
 452                return;
 453        }
 454        debugfs_create_file("pins", S_IFREG | S_IRUGO,
 455                            device_root, pctldev, &pinctrl_pins_ops);
 456        debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
 457                            device_root, pctldev, &pinctrl_groups_ops);
 458        debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
 459                            device_root, pctldev, &pinctrl_gpioranges_ops);
 460        pinmux_init_device_debugfs(device_root, pctldev);
 461}
 462
 463static void pinctrl_init_debugfs(void)
 464{
 465        debugfs_root = debugfs_create_dir("pinctrl", NULL);
 466        if (IS_ERR(debugfs_root) || !debugfs_root) {
 467                pr_warn("failed to create debugfs directory\n");
 468                debugfs_root = NULL;
 469                return;
 470        }
 471
 472        debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
 473                            debugfs_root, NULL, &pinctrl_devices_ops);
 474        pinmux_init_debugfs(debugfs_root);
 475}
 476
 477#else /* CONFIG_DEBUG_FS */
 478
 479static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
 480{
 481}
 482
 483static void pinctrl_init_debugfs(void)
 484{
 485}
 486
 487#endif
 488
 489/**
 490 * pinctrl_register() - register a pin controller device
 491 * @pctldesc: descriptor for this pin controller
 492 * @dev: parent device for this pin controller
 493 * @driver_data: private pin controller data for this pin controller
 494 */
 495struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
 496                                    struct device *dev, void *driver_data)
 497{
 498        static atomic_t pinmux_no = ATOMIC_INIT(0);
 499        struct pinctrl_dev *pctldev;
 500        int ret;
 501
 502        if (pctldesc == NULL)
 503                return NULL;
 504        if (pctldesc->name == NULL)
 505                return NULL;
 506
 507        /* If we're implementing pinmuxing, check the ops for sanity */
 508        if (pctldesc->pmxops) {
 509                ret = pinmux_check_ops(pctldesc->pmxops);
 510                if (ret) {
 511                        pr_err("%s pinmux ops lacks necessary functions\n",
 512                               pctldesc->name);
 513                        return NULL;
 514                }
 515        }
 516
 517        pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
 518        if (pctldev == NULL)
 519                return NULL;
 520
 521        /* Initialize pin control device struct */
 522        pctldev->owner = pctldesc->owner;
 523        pctldev->desc = pctldesc;
 524        pctldev->driver_data = driver_data;
 525        INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
 526        spin_lock_init(&pctldev->pin_desc_tree_lock);
 527        INIT_LIST_HEAD(&pctldev->gpio_ranges);
 528        mutex_init(&pctldev->gpio_ranges_lock);
 529
 530        /* Register device */
 531        pctldev->dev.parent = dev;
 532        dev_set_name(&pctldev->dev, "pinctrl.%d",
 533                     atomic_inc_return(&pinmux_no) - 1);
 534        pctldev->dev.release = pinctrl_dev_release;
 535        ret = device_register(&pctldev->dev);
 536        if (ret != 0) {
 537                pr_err("error in device registration\n");
 538                goto out_reg_dev_err;
 539        }
 540        dev_set_drvdata(&pctldev->dev, pctldev);
 541
 542        /* Register all the pins */
 543        pr_debug("try to register %d pins on %s...\n",
 544                 pctldesc->npins, pctldesc->name);
 545        ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
 546        if (ret) {
 547                pr_err("error during pin registration\n");
 548                pinctrl_free_pindescs(pctldev, pctldesc->pins,
 549                                      pctldesc->npins);
 550                goto out_reg_pins_err;
 551        }
 552
 553        pinctrl_init_device_debugfs(pctldev);
 554        mutex_lock(&pinctrldev_list_mutex);
 555        list_add(&pctldev->node, &pinctrldev_list);
 556        mutex_unlock(&pinctrldev_list_mutex);
 557        pinmux_hog_maps(pctldev);
 558        return pctldev;
 559
 560out_reg_pins_err:
 561        device_del(&pctldev->dev);
 562out_reg_dev_err:
 563        put_device(&pctldev->dev);
 564        return NULL;
 565}
 566EXPORT_SYMBOL_GPL(pinctrl_register);
 567
 568/**
 569 * pinctrl_unregister() - unregister pinmux
 570 * @pctldev: pin controller to unregister
 571 *
 572 * Called by pinmux drivers to unregister a pinmux.
 573 */
 574void pinctrl_unregister(struct pinctrl_dev *pctldev)
 575{
 576        if (pctldev == NULL)
 577                return;
 578
 579        pinmux_unhog_maps(pctldev);
 580        /* TODO: check that no pinmuxes are still active? */
 581        mutex_lock(&pinctrldev_list_mutex);
 582        list_del(&pctldev->node);
 583        mutex_unlock(&pinctrldev_list_mutex);
 584        /* Destroy descriptor tree */
 585        pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
 586                              pctldev->desc->npins);
 587        device_unregister(&pctldev->dev);
 588}
 589EXPORT_SYMBOL_GPL(pinctrl_unregister);
 590
 591static int __init pinctrl_init(void)
 592{
 593        pr_info("initialized pinctrl subsystem\n");
 594        pinctrl_init_debugfs();
 595        return 0;
 596}
 597
 598/* init early since many drivers really need to initialized pinmux early */
 599core_initcall(pinctrl_init);
 600