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