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