Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Core driver for the pin control subsystem |
| 3 | * |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 4 | * Copyright (C) 2011-2012 ST-Ericsson SA |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 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" |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 31 | #include "pinconf.h" |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 32 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 33 | /** |
| 34 | * struct pinctrl_hog - a list item to stash control hogs |
| 35 | * @node: pin control hog list node |
| 36 | * @map: map entry responsible for this hogging |
| 37 | * @pmx: the pin control hogged by this item |
| 38 | */ |
| 39 | struct pinctrl_hog { |
| 40 | struct list_head node; |
| 41 | struct pinctrl_map const *map; |
| 42 | struct pinctrl *p; |
| 43 | }; |
| 44 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 45 | /* Global list of pin control devices */ |
| 46 | static DEFINE_MUTEX(pinctrldev_list_mutex); |
| 47 | static LIST_HEAD(pinctrldev_list); |
| 48 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 49 | /* List of pin controller handles */ |
| 50 | static DEFINE_MUTEX(pinctrl_list_mutex); |
| 51 | static LIST_HEAD(pinctrl_list); |
| 52 | |
| 53 | /* Global pinctrl maps */ |
| 54 | static struct pinctrl_map *pinctrl_maps; |
| 55 | static unsigned pinctrl_maps_num; |
| 56 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 57 | const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev) |
| 58 | { |
| 59 | /* We're not allowed to register devices without name */ |
| 60 | return pctldev->desc->name; |
| 61 | } |
| 62 | EXPORT_SYMBOL_GPL(pinctrl_dev_get_name); |
| 63 | |
| 64 | void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev) |
| 65 | { |
| 66 | return pctldev->driver_data; |
| 67 | } |
| 68 | EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata); |
| 69 | |
| 70 | /** |
Linus Walleij | 9dfac4f | 2012-02-01 18:02:47 +0100 | [diff] [blame] | 71 | * get_pinctrl_dev_from_devname() - look up pin controller device |
| 72 | * @devname: the name of a device instance, as returned by dev_name() |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 73 | * |
| 74 | * Looks up a pin control device matching a certain device name or pure device |
| 75 | * pointer, the pure device pointer will take precedence. |
| 76 | */ |
Linus Walleij | 9dfac4f | 2012-02-01 18:02:47 +0100 | [diff] [blame] | 77 | struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 78 | { |
| 79 | struct pinctrl_dev *pctldev = NULL; |
| 80 | bool found = false; |
| 81 | |
Linus Walleij | 9dfac4f | 2012-02-01 18:02:47 +0100 | [diff] [blame] | 82 | if (!devname) |
| 83 | return NULL; |
| 84 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 85 | mutex_lock(&pinctrldev_list_mutex); |
| 86 | list_for_each_entry(pctldev, &pinctrldev_list, node) { |
Linus Walleij | 9dfac4f | 2012-02-01 18:02:47 +0100 | [diff] [blame] | 87 | if (!strcmp(dev_name(pctldev->dev), devname)) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 88 | /* Matched on device name */ |
| 89 | found = true; |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | mutex_unlock(&pinctrldev_list_mutex); |
| 94 | |
| 95 | return found ? pctldev : NULL; |
| 96 | } |
| 97 | |
Marek Belisko | 33d5894 | 2011-10-31 21:27:52 +0100 | [diff] [blame] | 98 | 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] | 99 | { |
| 100 | struct pin_desc *pindesc; |
| 101 | unsigned long flags; |
| 102 | |
| 103 | spin_lock_irqsave(&pctldev->pin_desc_tree_lock, flags); |
| 104 | pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, pin); |
| 105 | spin_unlock_irqrestore(&pctldev->pin_desc_tree_lock, flags); |
| 106 | |
| 107 | return pindesc; |
| 108 | } |
| 109 | |
| 110 | /** |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 111 | * pin_get_from_name() - look up a pin number from a name |
| 112 | * @pctldev: the pin control device to lookup the pin on |
| 113 | * @name: the name of the pin to look up |
| 114 | */ |
| 115 | int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name) |
| 116 | { |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 117 | unsigned i, pin; |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 118 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 119 | /* The pin number can be retrived from the pin controller descriptor */ |
| 120 | for (i = 0; i < pctldev->desc->npins; i++) { |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 121 | struct pin_desc *desc; |
| 122 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 123 | pin = pctldev->desc->pins[i].number; |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 124 | desc = pin_desc_get(pctldev, pin); |
| 125 | /* Pin space may be sparse */ |
| 126 | if (desc == NULL) |
| 127 | continue; |
| 128 | if (desc->name && !strcmp(name, desc->name)) |
| 129 | return pin; |
| 130 | } |
| 131 | |
| 132 | return -EINVAL; |
| 133 | } |
| 134 | |
| 135 | /** |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 136 | * pin_is_valid() - check if pin exists on controller |
| 137 | * @pctldev: the pin control device to check the pin on |
| 138 | * @pin: pin to check, use the local pin controller index number |
| 139 | * |
| 140 | * This tells us whether a certain pin exist on a certain pin controller or |
| 141 | * not. Pin lists may be sparse, so some pins may not exist. |
| 142 | */ |
| 143 | bool pin_is_valid(struct pinctrl_dev *pctldev, int pin) |
| 144 | { |
| 145 | struct pin_desc *pindesc; |
| 146 | |
| 147 | if (pin < 0) |
| 148 | return false; |
| 149 | |
| 150 | pindesc = pin_desc_get(pctldev, pin); |
| 151 | if (pindesc == NULL) |
| 152 | return false; |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | EXPORT_SYMBOL_GPL(pin_is_valid); |
| 157 | |
| 158 | /* Deletes a range of pin descriptors */ |
| 159 | static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev, |
| 160 | const struct pinctrl_pin_desc *pins, |
| 161 | unsigned num_pins) |
| 162 | { |
| 163 | int i; |
| 164 | |
| 165 | spin_lock(&pctldev->pin_desc_tree_lock); |
| 166 | for (i = 0; i < num_pins; i++) { |
| 167 | struct pin_desc *pindesc; |
| 168 | |
| 169 | pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, |
| 170 | pins[i].number); |
| 171 | if (pindesc != NULL) { |
| 172 | radix_tree_delete(&pctldev->pin_desc_tree, |
| 173 | pins[i].number); |
Linus Walleij | ca53c5f | 2011-12-14 20:33:37 +0100 | [diff] [blame] | 174 | if (pindesc->dynamic_name) |
| 175 | kfree(pindesc->name); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 176 | } |
| 177 | kfree(pindesc); |
| 178 | } |
| 179 | spin_unlock(&pctldev->pin_desc_tree_lock); |
| 180 | } |
| 181 | |
| 182 | static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev, |
| 183 | unsigned number, const char *name) |
| 184 | { |
| 185 | struct pin_desc *pindesc; |
| 186 | |
| 187 | pindesc = pin_desc_get(pctldev, number); |
| 188 | if (pindesc != NULL) { |
| 189 | pr_err("pin %d already registered on %s\n", number, |
| 190 | pctldev->desc->name); |
| 191 | return -EINVAL; |
| 192 | } |
| 193 | |
| 194 | pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL); |
| 195 | if (pindesc == NULL) |
| 196 | return -ENOMEM; |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 197 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 198 | spin_lock_init(&pindesc->lock); |
| 199 | |
| 200 | /* Set owner */ |
| 201 | pindesc->pctldev = pctldev; |
| 202 | |
Stephen Warren | 9af1e44 | 2011-10-19 16:19:27 -0600 | [diff] [blame] | 203 | /* Copy basic pin info */ |
Linus Walleij | 8dc6ae4 | 2012-02-01 18:11:40 +0100 | [diff] [blame] | 204 | if (name) { |
Linus Walleij | ca53c5f | 2011-12-14 20:33:37 +0100 | [diff] [blame] | 205 | pindesc->name = name; |
| 206 | } else { |
| 207 | pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number); |
| 208 | if (pindesc->name == NULL) |
| 209 | return -ENOMEM; |
| 210 | pindesc->dynamic_name = true; |
| 211 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 212 | |
| 213 | spin_lock(&pctldev->pin_desc_tree_lock); |
| 214 | radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc); |
| 215 | spin_unlock(&pctldev->pin_desc_tree_lock); |
| 216 | pr_debug("registered pin %d (%s) on %s\n", |
Linus Walleij | ca53c5f | 2011-12-14 20:33:37 +0100 | [diff] [blame] | 217 | number, pindesc->name, pctldev->desc->name); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int pinctrl_register_pins(struct pinctrl_dev *pctldev, |
| 222 | struct pinctrl_pin_desc const *pins, |
| 223 | unsigned num_descs) |
| 224 | { |
| 225 | unsigned i; |
| 226 | int ret = 0; |
| 227 | |
| 228 | for (i = 0; i < num_descs; i++) { |
| 229 | ret = pinctrl_register_one_pin(pctldev, |
| 230 | pins[i].number, pins[i].name); |
| 231 | if (ret) |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range |
| 240 | * @pctldev: pin controller device to check |
| 241 | * @gpio: gpio pin to check taken from the global GPIO pin space |
| 242 | * |
| 243 | * Tries to match a GPIO pin number to the ranges handled by a certain pin |
| 244 | * controller, return the range or NULL |
| 245 | */ |
| 246 | static struct pinctrl_gpio_range * |
| 247 | pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio) |
| 248 | { |
| 249 | struct pinctrl_gpio_range *range = NULL; |
| 250 | |
| 251 | /* Loop over the ranges */ |
| 252 | mutex_lock(&pctldev->gpio_ranges_lock); |
| 253 | list_for_each_entry(range, &pctldev->gpio_ranges, node) { |
| 254 | /* Check if we're in the valid range */ |
| 255 | if (gpio >= range->base && |
| 256 | gpio < range->base + range->npins) { |
| 257 | mutex_unlock(&pctldev->gpio_ranges_lock); |
| 258 | return range; |
| 259 | } |
| 260 | } |
| 261 | mutex_unlock(&pctldev->gpio_ranges_lock); |
| 262 | |
| 263 | return NULL; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * pinctrl_get_device_gpio_range() - find device for GPIO range |
| 268 | * @gpio: the pin to locate the pin controller for |
| 269 | * @outdev: the pin control device if found |
| 270 | * @outrange: the GPIO range if found |
| 271 | * |
| 272 | * Find the pin controller handling a certain GPIO pin from the pinspace of |
| 273 | * the GPIO subsystem, return the device and the matching GPIO range. Returns |
| 274 | * negative if the GPIO range could not be found in any device. |
| 275 | */ |
| 276 | int pinctrl_get_device_gpio_range(unsigned gpio, |
| 277 | struct pinctrl_dev **outdev, |
| 278 | struct pinctrl_gpio_range **outrange) |
| 279 | { |
| 280 | struct pinctrl_dev *pctldev = NULL; |
| 281 | |
| 282 | /* Loop over the pin controllers */ |
| 283 | mutex_lock(&pinctrldev_list_mutex); |
| 284 | list_for_each_entry(pctldev, &pinctrldev_list, node) { |
| 285 | struct pinctrl_gpio_range *range; |
| 286 | |
| 287 | range = pinctrl_match_gpio_range(pctldev, gpio); |
| 288 | if (range != NULL) { |
| 289 | *outdev = pctldev; |
| 290 | *outrange = range; |
| 291 | mutex_unlock(&pinctrldev_list_mutex); |
| 292 | return 0; |
| 293 | } |
| 294 | } |
| 295 | mutex_unlock(&pinctrldev_list_mutex); |
| 296 | |
| 297 | return -EINVAL; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * pinctrl_add_gpio_range() - register a GPIO range for a controller |
| 302 | * @pctldev: pin controller device to add the range to |
| 303 | * @range: the GPIO range to add |
| 304 | * |
| 305 | * This adds a range of GPIOs to be handled by a certain pin controller. Call |
| 306 | * this to register handled ranges after registering your pin controller. |
| 307 | */ |
| 308 | void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev, |
| 309 | struct pinctrl_gpio_range *range) |
| 310 | { |
| 311 | mutex_lock(&pctldev->gpio_ranges_lock); |
| 312 | list_add(&range->node, &pctldev->gpio_ranges); |
| 313 | mutex_unlock(&pctldev->gpio_ranges_lock); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller |
| 318 | * @pctldev: pin controller device to remove the range from |
| 319 | * @range: the GPIO range to remove |
| 320 | */ |
| 321 | void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev, |
| 322 | struct pinctrl_gpio_range *range) |
| 323 | { |
| 324 | mutex_lock(&pctldev->gpio_ranges_lock); |
| 325 | list_del(&range->node); |
| 326 | mutex_unlock(&pctldev->gpio_ranges_lock); |
| 327 | } |
| 328 | |
Linus Walleij | 7afde8b | 2011-10-19 17:07:16 +0200 | [diff] [blame] | 329 | /** |
| 330 | * pinctrl_get_group_selector() - returns the group selector for a group |
| 331 | * @pctldev: the pin controller handling the group |
| 332 | * @pin_group: the pin group to look up |
| 333 | */ |
| 334 | int pinctrl_get_group_selector(struct pinctrl_dev *pctldev, |
| 335 | const char *pin_group) |
| 336 | { |
| 337 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
| 338 | unsigned group_selector = 0; |
| 339 | |
| 340 | while (pctlops->list_groups(pctldev, group_selector) >= 0) { |
| 341 | const char *gname = pctlops->get_group_name(pctldev, |
| 342 | group_selector); |
| 343 | if (!strcmp(gname, pin_group)) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 344 | dev_dbg(pctldev->dev, |
Linus Walleij | 7afde8b | 2011-10-19 17:07:16 +0200 | [diff] [blame] | 345 | "found group selector %u for %s\n", |
| 346 | group_selector, |
| 347 | pin_group); |
| 348 | return group_selector; |
| 349 | } |
| 350 | |
| 351 | group_selector++; |
| 352 | } |
| 353 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 354 | dev_err(pctldev->dev, "does not have pin group %s\n", |
Linus Walleij | 7afde8b | 2011-10-19 17:07:16 +0200 | [diff] [blame] | 355 | pin_group); |
| 356 | |
| 357 | return -EINVAL; |
| 358 | } |
| 359 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 360 | /** |
| 361 | * pinctrl_request_gpio() - request a single pin to be used in as GPIO |
| 362 | * @gpio: the GPIO pin number from the GPIO subsystem number space |
| 363 | * |
| 364 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, |
| 365 | * as part of their gpio_request() semantics, platforms and individual drivers |
| 366 | * shall *NOT* request GPIO pins to be muxed in. |
| 367 | */ |
| 368 | int pinctrl_request_gpio(unsigned gpio) |
| 369 | { |
| 370 | struct pinctrl_dev *pctldev; |
| 371 | struct pinctrl_gpio_range *range; |
| 372 | int ret; |
| 373 | int pin; |
| 374 | |
| 375 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); |
| 376 | if (ret) |
| 377 | return -EINVAL; |
| 378 | |
| 379 | /* Convert to the pin controllers number space */ |
| 380 | pin = gpio - range->base + range->pin_base; |
| 381 | |
| 382 | return pinmux_request_gpio(pctldev, range, pin, gpio); |
| 383 | } |
| 384 | EXPORT_SYMBOL_GPL(pinctrl_request_gpio); |
| 385 | |
| 386 | /** |
| 387 | * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO |
| 388 | * @gpio: the GPIO pin number from the GPIO subsystem number space |
| 389 | * |
| 390 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, |
| 391 | * as part of their gpio_free() semantics, platforms and individual drivers |
| 392 | * shall *NOT* request GPIO pins to be muxed out. |
| 393 | */ |
| 394 | void pinctrl_free_gpio(unsigned gpio) |
| 395 | { |
| 396 | struct pinctrl_dev *pctldev; |
| 397 | struct pinctrl_gpio_range *range; |
| 398 | int ret; |
| 399 | int pin; |
| 400 | |
| 401 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); |
| 402 | if (ret) |
| 403 | return; |
| 404 | |
| 405 | /* Convert to the pin controllers number space */ |
| 406 | pin = gpio - range->base + range->pin_base; |
| 407 | |
| 408 | return pinmux_free_gpio(pctldev, pin, range); |
| 409 | } |
| 410 | EXPORT_SYMBOL_GPL(pinctrl_free_gpio); |
| 411 | |
| 412 | static int pinctrl_gpio_direction(unsigned gpio, bool input) |
| 413 | { |
| 414 | struct pinctrl_dev *pctldev; |
| 415 | struct pinctrl_gpio_range *range; |
| 416 | int ret; |
| 417 | int pin; |
| 418 | |
| 419 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); |
| 420 | if (ret) |
| 421 | return ret; |
| 422 | |
| 423 | /* Convert to the pin controllers number space */ |
| 424 | pin = gpio - range->base + range->pin_base; |
| 425 | |
| 426 | return pinmux_gpio_direction(pctldev, range, pin, input); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode |
| 431 | * @gpio: the GPIO pin number from the GPIO subsystem number space |
| 432 | * |
| 433 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, |
| 434 | * as part of their gpio_direction_input() semantics, platforms and individual |
| 435 | * drivers shall *NOT* touch pin control GPIO calls. |
| 436 | */ |
| 437 | int pinctrl_gpio_direction_input(unsigned gpio) |
| 438 | { |
| 439 | return pinctrl_gpio_direction(gpio, true); |
| 440 | } |
| 441 | EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input); |
| 442 | |
| 443 | /** |
| 444 | * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode |
| 445 | * @gpio: the GPIO pin number from the GPIO subsystem number space |
| 446 | * |
| 447 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, |
| 448 | * as part of their gpio_direction_output() semantics, platforms and individual |
| 449 | * drivers shall *NOT* touch pin control GPIO calls. |
| 450 | */ |
| 451 | int pinctrl_gpio_direction_output(unsigned gpio) |
| 452 | { |
| 453 | return pinctrl_gpio_direction(gpio, false); |
| 454 | } |
| 455 | EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output); |
| 456 | |
| 457 | /** |
| 458 | * pinctrl_get() - retrieves the pin controller handle for a certain device |
| 459 | * @dev: the device to get the pin controller handle for |
| 460 | * @name: an optional specific control mapping name or NULL, the name is only |
| 461 | * needed if you want to have more than one mapping per device, or if you |
| 462 | * need an anonymous pin control (not tied to any specific device) |
| 463 | */ |
| 464 | struct pinctrl *pinctrl_get(struct device *dev, const char *name) |
| 465 | { |
| 466 | struct pinctrl_map const *map = NULL; |
| 467 | struct pinctrl_dev *pctldev = NULL; |
| 468 | const char *devname = NULL; |
| 469 | struct pinctrl *p; |
| 470 | bool found_map; |
| 471 | unsigned num_maps = 0; |
| 472 | int ret = -ENODEV; |
| 473 | int i; |
| 474 | |
| 475 | /* We must have dev or ID or both */ |
| 476 | if (!dev && !name) |
| 477 | return ERR_PTR(-EINVAL); |
| 478 | |
| 479 | if (dev) |
| 480 | devname = dev_name(dev); |
| 481 | |
| 482 | pr_debug("get pin control handle %s for device %s\n", name, |
| 483 | devname ? devname : "(none)"); |
| 484 | |
| 485 | /* |
| 486 | * create the state cookie holder struct pinctrl for each |
| 487 | * mapping, this is what consumers will get when requesting |
| 488 | * a pin control handle with pinctrl_get() |
| 489 | */ |
| 490 | p = kzalloc(sizeof(struct pinctrl), GFP_KERNEL); |
| 491 | if (p == NULL) |
| 492 | return ERR_PTR(-ENOMEM); |
| 493 | mutex_init(&p->mutex); |
| 494 | pinmux_init_pinctrl_handle(p); |
| 495 | |
| 496 | /* Iterate over the pin control maps to locate the right ones */ |
| 497 | for (i = 0; i < pinctrl_maps_num; i++) { |
| 498 | map = &pinctrl_maps[i]; |
| 499 | found_map = false; |
| 500 | |
| 501 | /* |
| 502 | * First, try to find the pctldev given in the map |
| 503 | */ |
| 504 | pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name); |
| 505 | if (!pctldev) { |
| 506 | pr_warning("could not find a pinctrl device for pinmux function %s, fishy, they shall all have one\n", |
| 507 | map->function); |
| 508 | pr_warning("given pinctrl device name: %s", |
| 509 | map->ctrl_dev_name); |
| 510 | |
| 511 | /* Continue to check the other mappings anyway... */ |
| 512 | continue; |
| 513 | } |
| 514 | |
| 515 | pr_debug("in map, found pctldev %s to handle function %s", |
| 516 | dev_name(pctldev->dev), map->function); |
| 517 | |
| 518 | |
| 519 | /* |
| 520 | * If we're looking for a specific named map, this must match, |
| 521 | * else we loop and look for the next. |
| 522 | */ |
| 523 | if (name != NULL) { |
| 524 | if (map->name == NULL) |
| 525 | continue; |
| 526 | if (strcmp(map->name, name)) |
| 527 | continue; |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * This is for the case where no device name is given, we |
| 532 | * already know that the function name matches from above |
| 533 | * code. |
| 534 | */ |
| 535 | if (!map->dev_name && (name != NULL)) |
| 536 | found_map = true; |
| 537 | |
| 538 | /* If the mapping has a device set up it must match */ |
| 539 | if (map->dev_name && |
| 540 | (!devname || !strcmp(map->dev_name, devname))) |
| 541 | /* MATCH! */ |
| 542 | found_map = true; |
| 543 | |
| 544 | /* If this map is applicable, then apply it */ |
| 545 | if (found_map) { |
| 546 | ret = pinmux_apply_muxmap(pctldev, p, dev, |
| 547 | devname, map); |
| 548 | if (ret) { |
| 549 | kfree(p); |
| 550 | return ERR_PTR(ret); |
| 551 | } |
| 552 | num_maps++; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | /* We should have atleast one map, right */ |
| 557 | if (!num_maps) { |
| 558 | pr_err("could not find any mux maps for device %s, ID %s\n", |
| 559 | devname ? devname : "(anonymous)", |
| 560 | name ? name : "(undefined)"); |
| 561 | kfree(p); |
| 562 | return ERR_PTR(-EINVAL); |
| 563 | } |
| 564 | |
| 565 | pr_debug("found %u mux maps for device %s, UD %s\n", |
| 566 | num_maps, |
| 567 | devname ? devname : "(anonymous)", |
| 568 | name ? name : "(undefined)"); |
| 569 | |
| 570 | /* Add the pinmux to the global list */ |
| 571 | mutex_lock(&pinctrl_list_mutex); |
| 572 | list_add(&p->node, &pinctrl_list); |
| 573 | mutex_unlock(&pinctrl_list_mutex); |
| 574 | |
| 575 | return p; |
| 576 | } |
| 577 | EXPORT_SYMBOL_GPL(pinctrl_get); |
| 578 | |
| 579 | /** |
| 580 | * pinctrl_put() - release a previously claimed pin control handle |
| 581 | * @p: a pin control handle previously claimed by pinctrl_get() |
| 582 | */ |
| 583 | void pinctrl_put(struct pinctrl *p) |
| 584 | { |
| 585 | if (p == NULL) |
| 586 | return; |
| 587 | |
| 588 | mutex_lock(&p->mutex); |
| 589 | if (p->usecount) |
| 590 | pr_warn("releasing pin control handle with active users!\n"); |
| 591 | /* Free the groups and all acquired pins */ |
| 592 | pinmux_put(p); |
| 593 | mutex_unlock(&p->mutex); |
| 594 | |
| 595 | /* Remove from list */ |
| 596 | mutex_lock(&pinctrl_list_mutex); |
| 597 | list_del(&p->node); |
| 598 | mutex_unlock(&pinctrl_list_mutex); |
| 599 | |
| 600 | kfree(p); |
| 601 | } |
| 602 | EXPORT_SYMBOL_GPL(pinctrl_put); |
| 603 | |
| 604 | /** |
| 605 | * pinctrl_enable() - enable a certain pin controller setting |
| 606 | * @p: the pin control handle to enable, previously claimed by pinctrl_get() |
| 607 | */ |
| 608 | int pinctrl_enable(struct pinctrl *p) |
| 609 | { |
| 610 | int ret = 0; |
| 611 | |
| 612 | if (p == NULL) |
| 613 | return -EINVAL; |
| 614 | mutex_lock(&p->mutex); |
| 615 | if (p->usecount++ == 0) { |
| 616 | ret = pinmux_enable(p); |
| 617 | if (ret) |
| 618 | p->usecount--; |
| 619 | } |
| 620 | mutex_unlock(&p->mutex); |
| 621 | return ret; |
| 622 | } |
| 623 | EXPORT_SYMBOL_GPL(pinctrl_enable); |
| 624 | |
| 625 | /** |
| 626 | * pinctrl_disable() - disable a certain pin control setting |
| 627 | * @p: the pin control handle to disable, previously claimed by pinctrl_get() |
| 628 | */ |
| 629 | void pinctrl_disable(struct pinctrl *p) |
| 630 | { |
| 631 | if (p == NULL) |
| 632 | return; |
| 633 | |
| 634 | mutex_lock(&p->mutex); |
| 635 | if (--p->usecount == 0) { |
| 636 | pinmux_disable(p); |
| 637 | } |
| 638 | mutex_unlock(&p->mutex); |
| 639 | } |
| 640 | EXPORT_SYMBOL_GPL(pinctrl_disable); |
| 641 | |
| 642 | /** |
| 643 | * pinctrl_register_mappings() - register a set of pin controller mappings |
| 644 | * @maps: the pincontrol mappings table to register, this should be marked with |
| 645 | * __initdata so it can be discarded after boot, this function will |
| 646 | * perform a shallow copy for the mapping entries. |
| 647 | * @num_maps: the number of maps in the mapping table |
| 648 | * |
| 649 | * Only call this once during initialization of your machine, the function is |
| 650 | * tagged as __init and won't be callable after init has completed. The map |
| 651 | * passed into this function will be owned by the pinmux core and cannot be |
| 652 | * freed. |
| 653 | */ |
| 654 | int __init pinctrl_register_mappings(struct pinctrl_map const *maps, |
| 655 | unsigned num_maps) |
| 656 | { |
| 657 | void *tmp_maps; |
| 658 | int i; |
| 659 | |
| 660 | pr_debug("add %d pinmux maps\n", num_maps); |
| 661 | |
| 662 | /* First sanity check the new mapping */ |
| 663 | for (i = 0; i < num_maps; i++) { |
| 664 | if (!maps[i].name) { |
| 665 | pr_err("failed to register map %d: no map name given\n", |
| 666 | i); |
| 667 | return -EINVAL; |
| 668 | } |
| 669 | |
| 670 | if (!maps[i].ctrl_dev_name) { |
| 671 | pr_err("failed to register map %s (%d): no pin control device given\n", |
| 672 | maps[i].name, i); |
| 673 | return -EINVAL; |
| 674 | } |
| 675 | |
| 676 | if (!maps[i].function) { |
| 677 | pr_err("failed to register map %s (%d): no function ID given\n", |
| 678 | maps[i].name, i); |
| 679 | return -EINVAL; |
| 680 | } |
| 681 | |
| 682 | if (!maps[i].dev_name) |
| 683 | pr_debug("add system map %s function %s with no device\n", |
| 684 | maps[i].name, |
| 685 | maps[i].function); |
| 686 | else |
| 687 | pr_debug("register map %s, function %s\n", |
| 688 | maps[i].name, |
| 689 | maps[i].function); |
| 690 | } |
| 691 | |
| 692 | /* |
| 693 | * Make a copy of the map array - string pointers will end up in the |
| 694 | * kernel const section anyway so these do not need to be deep copied. |
| 695 | */ |
| 696 | if (!pinctrl_maps_num) { |
| 697 | /* On first call, just copy them */ |
| 698 | tmp_maps = kmemdup(maps, |
| 699 | sizeof(struct pinctrl_map) * num_maps, |
| 700 | GFP_KERNEL); |
| 701 | if (!tmp_maps) |
| 702 | return -ENOMEM; |
| 703 | } else { |
| 704 | /* Subsequent calls, reallocate array to new size */ |
| 705 | size_t oldsize = sizeof(struct pinctrl_map) * pinctrl_maps_num; |
| 706 | size_t newsize = sizeof(struct pinctrl_map) * num_maps; |
| 707 | |
| 708 | tmp_maps = krealloc(pinctrl_maps, |
| 709 | oldsize + newsize, GFP_KERNEL); |
| 710 | if (!tmp_maps) |
| 711 | return -ENOMEM; |
| 712 | memcpy((tmp_maps + oldsize), maps, newsize); |
| 713 | } |
| 714 | |
| 715 | pinctrl_maps = tmp_maps; |
| 716 | pinctrl_maps_num += num_maps; |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | /* Hog a single map entry and add to the hoglist */ |
| 721 | static int pinctrl_hog_map(struct pinctrl_dev *pctldev, |
| 722 | struct pinctrl_map const *map) |
| 723 | { |
| 724 | struct pinctrl_hog *hog; |
| 725 | struct pinctrl *p; |
| 726 | int ret; |
| 727 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 728 | hog = kzalloc(sizeof(struct pinctrl_hog), GFP_KERNEL); |
| 729 | if (!hog) |
| 730 | return -ENOMEM; |
| 731 | |
Stephen Warren | 449d16b | 2012-02-14 10:50:41 -0700 | [diff] [blame^] | 732 | p = pinctrl_get(pctldev->dev, map->name); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 733 | if (IS_ERR(p)) { |
| 734 | kfree(hog); |
| 735 | dev_err(pctldev->dev, |
| 736 | "could not get the %s pin control mapping for hogging\n", |
| 737 | map->name); |
| 738 | return PTR_ERR(p); |
| 739 | } |
| 740 | |
| 741 | ret = pinctrl_enable(p); |
| 742 | if (ret) { |
| 743 | pinctrl_put(p); |
| 744 | kfree(hog); |
| 745 | dev_err(pctldev->dev, |
| 746 | "could not enable the %s pin control mapping for hogging\n", |
| 747 | map->name); |
| 748 | return ret; |
| 749 | } |
| 750 | |
| 751 | hog->map = map; |
| 752 | hog->p = p; |
| 753 | |
| 754 | dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name, |
| 755 | map->function); |
| 756 | mutex_lock(&pctldev->pinctrl_hogs_lock); |
| 757 | list_add(&hog->node, &pctldev->pinctrl_hogs); |
| 758 | mutex_unlock(&pctldev->pinctrl_hogs_lock); |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * pinctrl_hog_maps() - hog specific map entries on controller device |
| 765 | * @pctldev: the pin control device to hog entries on |
| 766 | * |
| 767 | * When the pin controllers are registered, there may be some specific pinmux |
| 768 | * map entries that need to be hogged, i.e. get+enabled until the system shuts |
| 769 | * down. |
| 770 | */ |
| 771 | int pinctrl_hog_maps(struct pinctrl_dev *pctldev) |
| 772 | { |
| 773 | struct device *dev = pctldev->dev; |
| 774 | const char *devname = dev_name(dev); |
| 775 | int ret; |
| 776 | int i; |
| 777 | |
| 778 | INIT_LIST_HEAD(&pctldev->pinctrl_hogs); |
| 779 | mutex_init(&pctldev->pinctrl_hogs_lock); |
| 780 | |
| 781 | for (i = 0; i < pinctrl_maps_num; i++) { |
| 782 | struct pinctrl_map const *map = &pinctrl_maps[i]; |
| 783 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 784 | if (map->ctrl_dev_name && |
Linus Walleij | 77a5988 | 2012-02-10 01:34:12 +0100 | [diff] [blame] | 785 | !strcmp(map->ctrl_dev_name, devname) && |
| 786 | !strcmp(map->dev_name, devname)) { |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 787 | /* OK time to hog! */ |
| 788 | ret = pinctrl_hog_map(pctldev, map); |
| 789 | if (ret) |
| 790 | return ret; |
| 791 | } |
| 792 | } |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | /** |
| 797 | * pinctrl_unhog_maps() - unhog specific map entries on controller device |
| 798 | * @pctldev: the pin control device to unhog entries on |
| 799 | */ |
| 800 | void pinctrl_unhog_maps(struct pinctrl_dev *pctldev) |
| 801 | { |
| 802 | struct list_head *node, *tmp; |
| 803 | |
| 804 | mutex_lock(&pctldev->pinctrl_hogs_lock); |
| 805 | list_for_each_safe(node, tmp, &pctldev->pinctrl_hogs) { |
| 806 | struct pinctrl_hog *hog = |
| 807 | list_entry(node, struct pinctrl_hog, node); |
| 808 | pinctrl_disable(hog->p); |
| 809 | pinctrl_put(hog->p); |
| 810 | list_del(node); |
| 811 | kfree(hog); |
| 812 | } |
| 813 | mutex_unlock(&pctldev->pinctrl_hogs_lock); |
| 814 | } |
| 815 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 816 | #ifdef CONFIG_DEBUG_FS |
| 817 | |
| 818 | static int pinctrl_pins_show(struct seq_file *s, void *what) |
| 819 | { |
| 820 | struct pinctrl_dev *pctldev = s->private; |
| 821 | const struct pinctrl_ops *ops = pctldev->desc->pctlops; |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 822 | unsigned i, pin; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 823 | |
| 824 | seq_printf(s, "registered pins: %d\n", pctldev->desc->npins); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 825 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 826 | /* The pin number can be retrived from the pin controller descriptor */ |
| 827 | for (i = 0; i < pctldev->desc->npins; i++) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 828 | struct pin_desc *desc; |
| 829 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 830 | pin = pctldev->desc->pins[i].number; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 831 | desc = pin_desc_get(pctldev, pin); |
| 832 | /* Pin space may be sparse */ |
| 833 | if (desc == NULL) |
| 834 | continue; |
| 835 | |
| 836 | seq_printf(s, "pin %d (%s) ", pin, |
| 837 | desc->name ? desc->name : "unnamed"); |
| 838 | |
| 839 | /* Driver-specific info per pin */ |
| 840 | if (ops->pin_dbg_show) |
| 841 | ops->pin_dbg_show(pctldev, s, pin); |
| 842 | |
| 843 | seq_puts(s, "\n"); |
| 844 | } |
| 845 | |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | static int pinctrl_groups_show(struct seq_file *s, void *what) |
| 850 | { |
| 851 | struct pinctrl_dev *pctldev = s->private; |
| 852 | const struct pinctrl_ops *ops = pctldev->desc->pctlops; |
| 853 | unsigned selector = 0; |
| 854 | |
| 855 | /* No grouping */ |
| 856 | if (!ops) |
| 857 | return 0; |
| 858 | |
| 859 | seq_puts(s, "registered pin groups:\n"); |
| 860 | while (ops->list_groups(pctldev, selector) >= 0) { |
Stephen Warren | a5818a8 | 2011-10-19 16:19:25 -0600 | [diff] [blame] | 861 | const unsigned *pins; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 862 | unsigned num_pins; |
| 863 | const char *gname = ops->get_group_name(pctldev, selector); |
| 864 | int ret; |
| 865 | int i; |
| 866 | |
| 867 | ret = ops->get_group_pins(pctldev, selector, |
| 868 | &pins, &num_pins); |
| 869 | if (ret) |
| 870 | seq_printf(s, "%s [ERROR GETTING PINS]\n", |
| 871 | gname); |
| 872 | else { |
| 873 | seq_printf(s, "group: %s, pins = [ ", gname); |
| 874 | for (i = 0; i < num_pins; i++) |
| 875 | seq_printf(s, "%d ", pins[i]); |
| 876 | seq_puts(s, "]\n"); |
| 877 | } |
| 878 | selector++; |
| 879 | } |
| 880 | |
| 881 | |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | static int pinctrl_gpioranges_show(struct seq_file *s, void *what) |
| 886 | { |
| 887 | struct pinctrl_dev *pctldev = s->private; |
| 888 | struct pinctrl_gpio_range *range = NULL; |
| 889 | |
| 890 | seq_puts(s, "GPIO ranges handled:\n"); |
| 891 | |
| 892 | /* Loop over the ranges */ |
| 893 | mutex_lock(&pctldev->gpio_ranges_lock); |
| 894 | list_for_each_entry(range, &pctldev->gpio_ranges, node) { |
Linus Walleij | 75d6642 | 2011-11-16 09:58:51 +0100 | [diff] [blame] | 895 | seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n", |
| 896 | range->id, range->name, |
| 897 | range->base, (range->base + range->npins - 1), |
| 898 | range->pin_base, |
| 899 | (range->pin_base + range->npins - 1)); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 900 | } |
| 901 | mutex_unlock(&pctldev->gpio_ranges_lock); |
| 902 | |
| 903 | return 0; |
| 904 | } |
| 905 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 906 | static int pinctrl_maps_show(struct seq_file *s, void *what) |
| 907 | { |
| 908 | int i; |
| 909 | |
| 910 | seq_puts(s, "Pinctrl maps:\n"); |
| 911 | |
| 912 | for (i = 0; i < pinctrl_maps_num; i++) { |
| 913 | struct pinctrl_map const *map = &pinctrl_maps[i]; |
| 914 | |
| 915 | seq_printf(s, "%s:\n", map->name); |
| 916 | if (map->dev_name) |
| 917 | seq_printf(s, " device: %s\n", |
| 918 | map->dev_name); |
| 919 | else |
| 920 | seq_printf(s, " SYSTEM MUX\n"); |
| 921 | seq_printf(s, " controlling device %s\n", |
| 922 | map->ctrl_dev_name); |
| 923 | seq_printf(s, " function: %s\n", map->function); |
| 924 | seq_printf(s, " group: %s\n", map->group ? map->group : |
| 925 | "(default)"); |
| 926 | } |
| 927 | return 0; |
| 928 | } |
| 929 | |
| 930 | static int pinmux_hogs_show(struct seq_file *s, void *what) |
| 931 | { |
| 932 | struct pinctrl_dev *pctldev = s->private; |
| 933 | struct pinctrl_hog *hog; |
| 934 | |
| 935 | seq_puts(s, "Pin control map hogs held by device\n"); |
| 936 | |
| 937 | list_for_each_entry(hog, &pctldev->pinctrl_hogs, node) |
| 938 | seq_printf(s, "%s\n", hog->map->name); |
| 939 | |
| 940 | return 0; |
| 941 | } |
| 942 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 943 | static int pinctrl_devices_show(struct seq_file *s, void *what) |
| 944 | { |
| 945 | struct pinctrl_dev *pctldev; |
| 946 | |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 947 | seq_puts(s, "name [pinmux] [pinconf]\n"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 948 | mutex_lock(&pinctrldev_list_mutex); |
| 949 | list_for_each_entry(pctldev, &pinctrldev_list, node) { |
| 950 | seq_printf(s, "%s ", pctldev->desc->name); |
| 951 | if (pctldev->desc->pmxops) |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 952 | seq_puts(s, "yes "); |
| 953 | else |
| 954 | seq_puts(s, "no "); |
| 955 | if (pctldev->desc->confops) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 956 | seq_puts(s, "yes"); |
| 957 | else |
| 958 | seq_puts(s, "no"); |
| 959 | seq_puts(s, "\n"); |
| 960 | } |
| 961 | mutex_unlock(&pinctrldev_list_mutex); |
| 962 | |
| 963 | return 0; |
| 964 | } |
| 965 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 966 | static int pinctrl_show(struct seq_file *s, void *what) |
| 967 | { |
| 968 | struct pinctrl *p; |
| 969 | |
| 970 | seq_puts(s, "Requested pin control handlers their pinmux maps:\n"); |
| 971 | list_for_each_entry(p, &pinctrl_list, node) { |
| 972 | struct pinctrl_dev *pctldev = p->pctldev; |
| 973 | |
| 974 | if (!pctldev) { |
| 975 | seq_puts(s, "NO PIN CONTROLLER DEVICE\n"); |
| 976 | continue; |
| 977 | } |
| 978 | |
| 979 | seq_printf(s, "device: %s", |
| 980 | pinctrl_dev_get_name(p->pctldev)); |
| 981 | |
| 982 | pinmux_dbg_show(s, p); |
| 983 | |
| 984 | seq_printf(s, " users: %u map-> %s\n", |
| 985 | p->usecount, |
| 986 | p->dev ? dev_name(p->dev) : "(system)"); |
| 987 | } |
| 988 | |
| 989 | return 0; |
| 990 | } |
| 991 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 992 | static int pinctrl_pins_open(struct inode *inode, struct file *file) |
| 993 | { |
| 994 | return single_open(file, pinctrl_pins_show, inode->i_private); |
| 995 | } |
| 996 | |
| 997 | static int pinctrl_groups_open(struct inode *inode, struct file *file) |
| 998 | { |
| 999 | return single_open(file, pinctrl_groups_show, inode->i_private); |
| 1000 | } |
| 1001 | |
| 1002 | static int pinctrl_gpioranges_open(struct inode *inode, struct file *file) |
| 1003 | { |
| 1004 | return single_open(file, pinctrl_gpioranges_show, inode->i_private); |
| 1005 | } |
| 1006 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1007 | static int pinctrl_maps_open(struct inode *inode, struct file *file) |
| 1008 | { |
| 1009 | return single_open(file, pinctrl_maps_show, inode->i_private); |
| 1010 | } |
| 1011 | |
| 1012 | static int pinmux_hogs_open(struct inode *inode, struct file *file) |
| 1013 | { |
| 1014 | return single_open(file, pinmux_hogs_show, inode->i_private); |
| 1015 | } |
| 1016 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1017 | static int pinctrl_devices_open(struct inode *inode, struct file *file) |
| 1018 | { |
| 1019 | return single_open(file, pinctrl_devices_show, NULL); |
| 1020 | } |
| 1021 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1022 | static int pinctrl_open(struct inode *inode, struct file *file) |
| 1023 | { |
| 1024 | return single_open(file, pinctrl_show, NULL); |
| 1025 | } |
| 1026 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1027 | static const struct file_operations pinctrl_pins_ops = { |
| 1028 | .open = pinctrl_pins_open, |
| 1029 | .read = seq_read, |
| 1030 | .llseek = seq_lseek, |
| 1031 | .release = single_release, |
| 1032 | }; |
| 1033 | |
| 1034 | static const struct file_operations pinctrl_groups_ops = { |
| 1035 | .open = pinctrl_groups_open, |
| 1036 | .read = seq_read, |
| 1037 | .llseek = seq_lseek, |
| 1038 | .release = single_release, |
| 1039 | }; |
| 1040 | |
| 1041 | static const struct file_operations pinctrl_gpioranges_ops = { |
| 1042 | .open = pinctrl_gpioranges_open, |
| 1043 | .read = seq_read, |
| 1044 | .llseek = seq_lseek, |
| 1045 | .release = single_release, |
| 1046 | }; |
| 1047 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1048 | static const struct file_operations pinctrl_maps_ops = { |
| 1049 | .open = pinctrl_maps_open, |
| 1050 | .read = seq_read, |
| 1051 | .llseek = seq_lseek, |
| 1052 | .release = single_release, |
| 1053 | }; |
| 1054 | |
| 1055 | static const struct file_operations pinmux_hogs_ops = { |
| 1056 | .open = pinmux_hogs_open, |
| 1057 | .read = seq_read, |
| 1058 | .llseek = seq_lseek, |
| 1059 | .release = single_release, |
| 1060 | }; |
| 1061 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1062 | static const struct file_operations pinctrl_devices_ops = { |
| 1063 | .open = pinctrl_devices_open, |
| 1064 | .read = seq_read, |
| 1065 | .llseek = seq_lseek, |
| 1066 | .release = single_release, |
| 1067 | }; |
| 1068 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1069 | static const struct file_operations pinctrl_ops = { |
| 1070 | .open = pinctrl_open, |
| 1071 | .read = seq_read, |
| 1072 | .llseek = seq_lseek, |
| 1073 | .release = single_release, |
| 1074 | }; |
| 1075 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1076 | static struct dentry *debugfs_root; |
| 1077 | |
| 1078 | static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) |
| 1079 | { |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1080 | struct dentry *device_root; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1081 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1082 | device_root = debugfs_create_dir(dev_name(pctldev->dev), |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1083 | debugfs_root); |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1084 | pctldev->device_root = device_root; |
| 1085 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1086 | if (IS_ERR(device_root) || !device_root) { |
| 1087 | pr_warn("failed to create debugfs directory for %s\n", |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1088 | dev_name(pctldev->dev)); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1089 | return; |
| 1090 | } |
| 1091 | debugfs_create_file("pins", S_IFREG | S_IRUGO, |
| 1092 | device_root, pctldev, &pinctrl_pins_ops); |
| 1093 | debugfs_create_file("pingroups", S_IFREG | S_IRUGO, |
| 1094 | device_root, pctldev, &pinctrl_groups_ops); |
| 1095 | debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO, |
| 1096 | device_root, pctldev, &pinctrl_gpioranges_ops); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1097 | debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO, |
| 1098 | device_root, pctldev, &pinctrl_maps_ops); |
| 1099 | debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO, |
| 1100 | device_root, pctldev, &pinmux_hogs_ops); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1101 | pinmux_init_device_debugfs(device_root, pctldev); |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 1102 | pinconf_init_device_debugfs(device_root, pctldev); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1103 | } |
| 1104 | |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1105 | static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev) |
| 1106 | { |
| 1107 | debugfs_remove_recursive(pctldev->device_root); |
| 1108 | } |
| 1109 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1110 | static void pinctrl_init_debugfs(void) |
| 1111 | { |
| 1112 | debugfs_root = debugfs_create_dir("pinctrl", NULL); |
| 1113 | if (IS_ERR(debugfs_root) || !debugfs_root) { |
| 1114 | pr_warn("failed to create debugfs directory\n"); |
| 1115 | debugfs_root = NULL; |
| 1116 | return; |
| 1117 | } |
| 1118 | |
| 1119 | debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO, |
| 1120 | debugfs_root, NULL, &pinctrl_devices_ops); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1121 | debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO, |
| 1122 | debugfs_root, NULL, &pinctrl_ops); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | #else /* CONFIG_DEBUG_FS */ |
| 1126 | |
| 1127 | static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) |
| 1128 | { |
| 1129 | } |
| 1130 | |
| 1131 | static void pinctrl_init_debugfs(void) |
| 1132 | { |
| 1133 | } |
| 1134 | |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1135 | static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev) |
| 1136 | { |
| 1137 | } |
| 1138 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1139 | #endif |
| 1140 | |
| 1141 | /** |
| 1142 | * pinctrl_register() - register a pin controller device |
| 1143 | * @pctldesc: descriptor for this pin controller |
| 1144 | * @dev: parent device for this pin controller |
| 1145 | * @driver_data: private pin controller data for this pin controller |
| 1146 | */ |
| 1147 | struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc, |
| 1148 | struct device *dev, void *driver_data) |
| 1149 | { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1150 | struct pinctrl_dev *pctldev; |
| 1151 | int ret; |
| 1152 | |
| 1153 | if (pctldesc == NULL) |
| 1154 | return NULL; |
| 1155 | if (pctldesc->name == NULL) |
| 1156 | return NULL; |
| 1157 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1158 | pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL); |
| 1159 | if (pctldev == NULL) |
| 1160 | return NULL; |
| 1161 | |
| 1162 | /* Initialize pin control device struct */ |
| 1163 | pctldev->owner = pctldesc->owner; |
| 1164 | pctldev->desc = pctldesc; |
| 1165 | pctldev->driver_data = driver_data; |
| 1166 | INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL); |
| 1167 | spin_lock_init(&pctldev->pin_desc_tree_lock); |
| 1168 | INIT_LIST_HEAD(&pctldev->gpio_ranges); |
| 1169 | mutex_init(&pctldev->gpio_ranges_lock); |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1170 | pctldev->dev = dev; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1171 | |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 1172 | /* If we're implementing pinmuxing, check the ops for sanity */ |
| 1173 | if (pctldesc->pmxops) { |
| 1174 | ret = pinmux_check_ops(pctldev); |
| 1175 | if (ret) { |
| 1176 | pr_err("%s pinmux ops lacks necessary functions\n", |
| 1177 | pctldesc->name); |
| 1178 | goto out_err; |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | /* If we're implementing pinconfig, check the ops for sanity */ |
| 1183 | if (pctldesc->confops) { |
| 1184 | ret = pinconf_check_ops(pctldev); |
| 1185 | if (ret) { |
| 1186 | pr_err("%s pin config ops lacks necessary functions\n", |
| 1187 | pctldesc->name); |
| 1188 | goto out_err; |
| 1189 | } |
| 1190 | } |
| 1191 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1192 | /* Register all the pins */ |
| 1193 | pr_debug("try to register %d pins on %s...\n", |
| 1194 | pctldesc->npins, pctldesc->name); |
| 1195 | ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins); |
| 1196 | if (ret) { |
| 1197 | pr_err("error during pin registration\n"); |
| 1198 | pinctrl_free_pindescs(pctldev, pctldesc->pins, |
| 1199 | pctldesc->npins); |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1200 | goto out_err; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | pinctrl_init_device_debugfs(pctldev); |
| 1204 | mutex_lock(&pinctrldev_list_mutex); |
| 1205 | list_add(&pctldev->node, &pinctrldev_list); |
| 1206 | mutex_unlock(&pinctrldev_list_mutex); |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame] | 1207 | pinctrl_hog_maps(pctldev); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1208 | return pctldev; |
| 1209 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1210 | out_err: |
| 1211 | kfree(pctldev); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1212 | return NULL; |
| 1213 | } |
| 1214 | EXPORT_SYMBOL_GPL(pinctrl_register); |
| 1215 | |
| 1216 | /** |
| 1217 | * pinctrl_unregister() - unregister pinmux |
| 1218 | * @pctldev: pin controller to unregister |
| 1219 | * |
| 1220 | * Called by pinmux drivers to unregister a pinmux. |
| 1221 | */ |
| 1222 | void pinctrl_unregister(struct pinctrl_dev *pctldev) |
| 1223 | { |
| 1224 | if (pctldev == NULL) |
| 1225 | return; |
| 1226 | |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1227 | pinctrl_remove_device_debugfs(pctldev); |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame] | 1228 | pinctrl_unhog_maps(pctldev); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1229 | /* TODO: check that no pinmuxes are still active? */ |
| 1230 | mutex_lock(&pinctrldev_list_mutex); |
| 1231 | list_del(&pctldev->node); |
| 1232 | mutex_unlock(&pinctrldev_list_mutex); |
| 1233 | /* Destroy descriptor tree */ |
| 1234 | pinctrl_free_pindescs(pctldev, pctldev->desc->pins, |
| 1235 | pctldev->desc->npins); |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1236 | kfree(pctldev); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1237 | } |
| 1238 | EXPORT_SYMBOL_GPL(pinctrl_unregister); |
| 1239 | |
| 1240 | static int __init pinctrl_init(void) |
| 1241 | { |
| 1242 | pr_info("initialized pinctrl subsystem\n"); |
| 1243 | pinctrl_init_debugfs(); |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | /* init early since many drivers really need to initialized pinmux early */ |
| 1248 | core_initcall(pinctrl_init); |