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