Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 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> |
Stephen Rothwell | a5a697c | 2011-09-30 14:39:04 +1000 | [diff] [blame] | 15 | #include <linux/export.h> |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 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 */ |
| 33 | static DEFINE_MUTEX(pinctrldev_list_mutex); |
| 34 | static LIST_HEAD(pinctrldev_list); |
| 35 | |
| 36 | static void pinctrl_dev_release(struct device *dev) |
| 37 | { |
| 38 | struct pinctrl_dev *pctldev = dev_get_drvdata(dev); |
| 39 | kfree(pctldev); |
| 40 | } |
| 41 | |
| 42 | const 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 | } |
| 47 | EXPORT_SYMBOL_GPL(pinctrl_dev_get_name); |
| 48 | |
| 49 | void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev) |
| 50 | { |
| 51 | return pctldev->driver_data; |
| 52 | } |
| 53 | EXPORT_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 | */ |
| 65 | struct 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 | |
Marek Belisko | 33d5894 | 2011-10-31 21:27:52 +0100 | [diff] [blame] | 91 | struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, unsigned int pin) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 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 | */ |
| 111 | bool 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 | } |
| 124 | EXPORT_SYMBOL_GPL(pin_is_valid); |
| 125 | |
| 126 | /* Deletes a range of pin descriptors */ |
| 127 | static 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 | |
| 148 | static 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 | |
Stephen Warren | 9af1e44 | 2011-10-19 16:19:27 -0600 | [diff] [blame] | 168 | /* Copy basic pin info */ |
| 169 | pindesc->name = name; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 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 | |
| 179 | static 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 | */ |
| 204 | static struct pinctrl_gpio_range * |
| 205 | pinctrl_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 | */ |
| 234 | int 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 | */ |
| 266 | void 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 | */ |
| 279 | void 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 | |
Linus Walleij | 7afde8b | 2011-10-19 17:07:16 +0200 | [diff] [blame] | 287 | /** |
| 288 | * pinctrl_get_group_selector() - returns the group selector for a group |
| 289 | * @pctldev: the pin controller handling the group |
| 290 | * @pin_group: the pin group to look up |
| 291 | */ |
| 292 | int pinctrl_get_group_selector(struct pinctrl_dev *pctldev, |
| 293 | const char *pin_group) |
| 294 | { |
| 295 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
| 296 | unsigned group_selector = 0; |
| 297 | |
| 298 | while (pctlops->list_groups(pctldev, group_selector) >= 0) { |
| 299 | const char *gname = pctlops->get_group_name(pctldev, |
| 300 | group_selector); |
| 301 | if (!strcmp(gname, pin_group)) { |
| 302 | dev_dbg(&pctldev->dev, |
| 303 | "found group selector %u for %s\n", |
| 304 | group_selector, |
| 305 | pin_group); |
| 306 | return group_selector; |
| 307 | } |
| 308 | |
| 309 | group_selector++; |
| 310 | } |
| 311 | |
| 312 | dev_err(&pctldev->dev, "does not have pin group %s\n", |
| 313 | pin_group); |
| 314 | |
| 315 | return -EINVAL; |
| 316 | } |
| 317 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 318 | #ifdef CONFIG_DEBUG_FS |
| 319 | |
| 320 | static int pinctrl_pins_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 pin; |
| 325 | |
| 326 | seq_printf(s, "registered pins: %d\n", pctldev->desc->npins); |
| 327 | seq_printf(s, "max pin number: %d\n", pctldev->desc->maxpin); |
| 328 | |
| 329 | /* The highest pin number need to be included in the loop, thus <= */ |
| 330 | for (pin = 0; pin <= pctldev->desc->maxpin; pin++) { |
| 331 | struct pin_desc *desc; |
| 332 | |
| 333 | desc = pin_desc_get(pctldev, pin); |
| 334 | /* Pin space may be sparse */ |
| 335 | if (desc == NULL) |
| 336 | continue; |
| 337 | |
| 338 | seq_printf(s, "pin %d (%s) ", pin, |
| 339 | desc->name ? desc->name : "unnamed"); |
| 340 | |
| 341 | /* Driver-specific info per pin */ |
| 342 | if (ops->pin_dbg_show) |
| 343 | ops->pin_dbg_show(pctldev, s, pin); |
| 344 | |
| 345 | seq_puts(s, "\n"); |
| 346 | } |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static int pinctrl_groups_show(struct seq_file *s, void *what) |
| 352 | { |
| 353 | struct pinctrl_dev *pctldev = s->private; |
| 354 | const struct pinctrl_ops *ops = pctldev->desc->pctlops; |
| 355 | unsigned selector = 0; |
| 356 | |
| 357 | /* No grouping */ |
| 358 | if (!ops) |
| 359 | return 0; |
| 360 | |
| 361 | seq_puts(s, "registered pin groups:\n"); |
| 362 | while (ops->list_groups(pctldev, selector) >= 0) { |
Stephen Warren | a5818a8 | 2011-10-19 16:19:25 -0600 | [diff] [blame] | 363 | const unsigned *pins; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 364 | unsigned num_pins; |
| 365 | const char *gname = ops->get_group_name(pctldev, selector); |
| 366 | int ret; |
| 367 | int i; |
| 368 | |
| 369 | ret = ops->get_group_pins(pctldev, selector, |
| 370 | &pins, &num_pins); |
| 371 | if (ret) |
| 372 | seq_printf(s, "%s [ERROR GETTING PINS]\n", |
| 373 | gname); |
| 374 | else { |
| 375 | seq_printf(s, "group: %s, pins = [ ", gname); |
| 376 | for (i = 0; i < num_pins; i++) |
| 377 | seq_printf(s, "%d ", pins[i]); |
| 378 | seq_puts(s, "]\n"); |
| 379 | } |
| 380 | selector++; |
| 381 | } |
| 382 | |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static int pinctrl_gpioranges_show(struct seq_file *s, void *what) |
| 388 | { |
| 389 | struct pinctrl_dev *pctldev = s->private; |
| 390 | struct pinctrl_gpio_range *range = NULL; |
| 391 | |
| 392 | seq_puts(s, "GPIO ranges handled:\n"); |
| 393 | |
| 394 | /* Loop over the ranges */ |
| 395 | mutex_lock(&pctldev->gpio_ranges_lock); |
| 396 | list_for_each_entry(range, &pctldev->gpio_ranges, node) { |
Linus Walleij | 75d6642 | 2011-11-16 09:58:51 +0100 | [diff] [blame^] | 397 | seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n", |
| 398 | range->id, range->name, |
| 399 | range->base, (range->base + range->npins - 1), |
| 400 | range->pin_base, |
| 401 | (range->pin_base + range->npins - 1)); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 402 | } |
| 403 | mutex_unlock(&pctldev->gpio_ranges_lock); |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static int pinctrl_devices_show(struct seq_file *s, void *what) |
| 409 | { |
| 410 | struct pinctrl_dev *pctldev; |
| 411 | |
| 412 | seq_puts(s, "name [pinmux]\n"); |
| 413 | mutex_lock(&pinctrldev_list_mutex); |
| 414 | list_for_each_entry(pctldev, &pinctrldev_list, node) { |
| 415 | seq_printf(s, "%s ", pctldev->desc->name); |
| 416 | if (pctldev->desc->pmxops) |
| 417 | seq_puts(s, "yes"); |
| 418 | else |
| 419 | seq_puts(s, "no"); |
| 420 | seq_puts(s, "\n"); |
| 421 | } |
| 422 | mutex_unlock(&pinctrldev_list_mutex); |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static int pinctrl_pins_open(struct inode *inode, struct file *file) |
| 428 | { |
| 429 | return single_open(file, pinctrl_pins_show, inode->i_private); |
| 430 | } |
| 431 | |
| 432 | static int pinctrl_groups_open(struct inode *inode, struct file *file) |
| 433 | { |
| 434 | return single_open(file, pinctrl_groups_show, inode->i_private); |
| 435 | } |
| 436 | |
| 437 | static int pinctrl_gpioranges_open(struct inode *inode, struct file *file) |
| 438 | { |
| 439 | return single_open(file, pinctrl_gpioranges_show, inode->i_private); |
| 440 | } |
| 441 | |
| 442 | static int pinctrl_devices_open(struct inode *inode, struct file *file) |
| 443 | { |
| 444 | return single_open(file, pinctrl_devices_show, NULL); |
| 445 | } |
| 446 | |
| 447 | static const struct file_operations pinctrl_pins_ops = { |
| 448 | .open = pinctrl_pins_open, |
| 449 | .read = seq_read, |
| 450 | .llseek = seq_lseek, |
| 451 | .release = single_release, |
| 452 | }; |
| 453 | |
| 454 | static const struct file_operations pinctrl_groups_ops = { |
| 455 | .open = pinctrl_groups_open, |
| 456 | .read = seq_read, |
| 457 | .llseek = seq_lseek, |
| 458 | .release = single_release, |
| 459 | }; |
| 460 | |
| 461 | static const struct file_operations pinctrl_gpioranges_ops = { |
| 462 | .open = pinctrl_gpioranges_open, |
| 463 | .read = seq_read, |
| 464 | .llseek = seq_lseek, |
| 465 | .release = single_release, |
| 466 | }; |
| 467 | |
| 468 | static const struct file_operations pinctrl_devices_ops = { |
| 469 | .open = pinctrl_devices_open, |
| 470 | .read = seq_read, |
| 471 | .llseek = seq_lseek, |
| 472 | .release = single_release, |
| 473 | }; |
| 474 | |
| 475 | static struct dentry *debugfs_root; |
| 476 | |
| 477 | static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) |
| 478 | { |
| 479 | static struct dentry *device_root; |
| 480 | |
| 481 | device_root = debugfs_create_dir(dev_name(&pctldev->dev), |
| 482 | debugfs_root); |
| 483 | if (IS_ERR(device_root) || !device_root) { |
| 484 | pr_warn("failed to create debugfs directory for %s\n", |
| 485 | dev_name(&pctldev->dev)); |
| 486 | return; |
| 487 | } |
| 488 | debugfs_create_file("pins", S_IFREG | S_IRUGO, |
| 489 | device_root, pctldev, &pinctrl_pins_ops); |
| 490 | debugfs_create_file("pingroups", S_IFREG | S_IRUGO, |
| 491 | device_root, pctldev, &pinctrl_groups_ops); |
| 492 | debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO, |
| 493 | device_root, pctldev, &pinctrl_gpioranges_ops); |
| 494 | pinmux_init_device_debugfs(device_root, pctldev); |
| 495 | } |
| 496 | |
| 497 | static void pinctrl_init_debugfs(void) |
| 498 | { |
| 499 | debugfs_root = debugfs_create_dir("pinctrl", NULL); |
| 500 | if (IS_ERR(debugfs_root) || !debugfs_root) { |
| 501 | pr_warn("failed to create debugfs directory\n"); |
| 502 | debugfs_root = NULL; |
| 503 | return; |
| 504 | } |
| 505 | |
| 506 | debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO, |
| 507 | debugfs_root, NULL, &pinctrl_devices_ops); |
| 508 | pinmux_init_debugfs(debugfs_root); |
| 509 | } |
| 510 | |
| 511 | #else /* CONFIG_DEBUG_FS */ |
| 512 | |
| 513 | static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) |
| 514 | { |
| 515 | } |
| 516 | |
| 517 | static void pinctrl_init_debugfs(void) |
| 518 | { |
| 519 | } |
| 520 | |
| 521 | #endif |
| 522 | |
| 523 | /** |
| 524 | * pinctrl_register() - register a pin controller device |
| 525 | * @pctldesc: descriptor for this pin controller |
| 526 | * @dev: parent device for this pin controller |
| 527 | * @driver_data: private pin controller data for this pin controller |
| 528 | */ |
| 529 | struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc, |
| 530 | struct device *dev, void *driver_data) |
| 531 | { |
| 532 | static atomic_t pinmux_no = ATOMIC_INIT(0); |
| 533 | struct pinctrl_dev *pctldev; |
| 534 | int ret; |
| 535 | |
| 536 | if (pctldesc == NULL) |
| 537 | return NULL; |
| 538 | if (pctldesc->name == NULL) |
| 539 | return NULL; |
| 540 | |
| 541 | /* If we're implementing pinmuxing, check the ops for sanity */ |
| 542 | if (pctldesc->pmxops) { |
| 543 | ret = pinmux_check_ops(pctldesc->pmxops); |
| 544 | if (ret) { |
| 545 | pr_err("%s pinmux ops lacks necessary functions\n", |
| 546 | pctldesc->name); |
| 547 | return NULL; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL); |
| 552 | if (pctldev == NULL) |
| 553 | return NULL; |
| 554 | |
| 555 | /* Initialize pin control device struct */ |
| 556 | pctldev->owner = pctldesc->owner; |
| 557 | pctldev->desc = pctldesc; |
| 558 | pctldev->driver_data = driver_data; |
| 559 | INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL); |
| 560 | spin_lock_init(&pctldev->pin_desc_tree_lock); |
| 561 | INIT_LIST_HEAD(&pctldev->gpio_ranges); |
| 562 | mutex_init(&pctldev->gpio_ranges_lock); |
| 563 | |
| 564 | /* Register device */ |
| 565 | pctldev->dev.parent = dev; |
| 566 | dev_set_name(&pctldev->dev, "pinctrl.%d", |
| 567 | atomic_inc_return(&pinmux_no) - 1); |
| 568 | pctldev->dev.release = pinctrl_dev_release; |
| 569 | ret = device_register(&pctldev->dev); |
| 570 | if (ret != 0) { |
| 571 | pr_err("error in device registration\n"); |
| 572 | goto out_reg_dev_err; |
| 573 | } |
| 574 | dev_set_drvdata(&pctldev->dev, pctldev); |
| 575 | |
| 576 | /* Register all the pins */ |
| 577 | pr_debug("try to register %d pins on %s...\n", |
| 578 | pctldesc->npins, pctldesc->name); |
| 579 | ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins); |
| 580 | if (ret) { |
| 581 | pr_err("error during pin registration\n"); |
| 582 | pinctrl_free_pindescs(pctldev, pctldesc->pins, |
| 583 | pctldesc->npins); |
| 584 | goto out_reg_pins_err; |
| 585 | } |
| 586 | |
| 587 | pinctrl_init_device_debugfs(pctldev); |
| 588 | mutex_lock(&pinctrldev_list_mutex); |
| 589 | list_add(&pctldev->node, &pinctrldev_list); |
| 590 | mutex_unlock(&pinctrldev_list_mutex); |
| 591 | pinmux_hog_maps(pctldev); |
| 592 | return pctldev; |
| 593 | |
| 594 | out_reg_pins_err: |
| 595 | device_del(&pctldev->dev); |
| 596 | out_reg_dev_err: |
| 597 | put_device(&pctldev->dev); |
| 598 | return NULL; |
| 599 | } |
| 600 | EXPORT_SYMBOL_GPL(pinctrl_register); |
| 601 | |
| 602 | /** |
| 603 | * pinctrl_unregister() - unregister pinmux |
| 604 | * @pctldev: pin controller to unregister |
| 605 | * |
| 606 | * Called by pinmux drivers to unregister a pinmux. |
| 607 | */ |
| 608 | void pinctrl_unregister(struct pinctrl_dev *pctldev) |
| 609 | { |
| 610 | if (pctldev == NULL) |
| 611 | return; |
| 612 | |
| 613 | pinmux_unhog_maps(pctldev); |
| 614 | /* TODO: check that no pinmuxes are still active? */ |
| 615 | mutex_lock(&pinctrldev_list_mutex); |
| 616 | list_del(&pctldev->node); |
| 617 | mutex_unlock(&pinctrldev_list_mutex); |
| 618 | /* Destroy descriptor tree */ |
| 619 | pinctrl_free_pindescs(pctldev, pctldev->desc->pins, |
| 620 | pctldev->desc->npins); |
| 621 | device_unregister(&pctldev->dev); |
| 622 | } |
| 623 | EXPORT_SYMBOL_GPL(pinctrl_unregister); |
| 624 | |
| 625 | static int __init pinctrl_init(void) |
| 626 | { |
| 627 | pr_info("initialized pinctrl subsystem\n"); |
| 628 | pinctrl_init_debugfs(); |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | /* init early since many drivers really need to initialized pinmux early */ |
| 633 | core_initcall(pinctrl_init); |