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> |
Stephen Warren | 6d4ca1f | 2012-04-16 10:51:00 -0600 | [diff] [blame] | 26 | #include <linux/pinctrl/consumer.h> |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 27 | #include <linux/pinctrl/pinctrl.h> |
| 28 | #include <linux/pinctrl/machine.h> |
| 29 | #include "core.h" |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 30 | #include "devicetree.h" |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 31 | #include "pinmux.h" |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 32 | #include "pinconf.h" |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 33 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 34 | /** |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 35 | * struct pinctrl_maps - a list item containing part of the mapping table |
| 36 | * @node: mapping table list node |
| 37 | * @maps: array of mapping table entries |
| 38 | * @num_maps: the number of entries in @maps |
| 39 | */ |
| 40 | struct pinctrl_maps { |
| 41 | struct list_head node; |
| 42 | struct pinctrl_map const *maps; |
| 43 | unsigned num_maps; |
| 44 | }; |
| 45 | |
Dong Aisheng | 5b3aa5f | 2012-04-26 16:15:50 +0800 | [diff] [blame] | 46 | static bool pinctrl_dummy_state; |
| 47 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 48 | /* Mutex taken by all entry points */ |
| 49 | DEFINE_MUTEX(pinctrl_mutex); |
| 50 | |
| 51 | /* Global list of pin control devices (struct pinctrl_dev) */ |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 52 | LIST_HEAD(pinctrldev_list); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 53 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 54 | /* List of pin controller handles (struct pinctrl) */ |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 55 | static LIST_HEAD(pinctrl_list); |
| 56 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 57 | /* List of pinctrl maps (struct pinctrl_maps) */ |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 58 | static LIST_HEAD(pinctrl_maps); |
| 59 | |
| 60 | #define for_each_maps(_maps_node_, _i_, _map_) \ |
| 61 | list_for_each_entry(_maps_node_, &pinctrl_maps, node) \ |
| 62 | for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \ |
| 63 | _i_ < _maps_node_->num_maps; \ |
Guennadi Liakhovetski | bc66468 | 2012-05-23 00:20:17 +0200 | [diff] [blame] | 64 | _i_++, _map_ = &_maps_node_->maps[_i_]) |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 65 | |
Dong Aisheng | 5b3aa5f | 2012-04-26 16:15:50 +0800 | [diff] [blame] | 66 | /** |
| 67 | * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support |
| 68 | * |
| 69 | * Usually this function is called by platforms without pinctrl driver support |
| 70 | * but run with some shared drivers using pinctrl APIs. |
| 71 | * After calling this function, the pinctrl core will return successfully |
| 72 | * with creating a dummy state for the driver to keep going smoothly. |
| 73 | */ |
| 74 | void pinctrl_provide_dummies(void) |
| 75 | { |
| 76 | pinctrl_dummy_state = true; |
| 77 | } |
| 78 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 79 | const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev) |
| 80 | { |
| 81 | /* We're not allowed to register devices without name */ |
| 82 | return pctldev->desc->name; |
| 83 | } |
| 84 | EXPORT_SYMBOL_GPL(pinctrl_dev_get_name); |
| 85 | |
| 86 | void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev) |
| 87 | { |
| 88 | return pctldev->driver_data; |
| 89 | } |
| 90 | EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata); |
| 91 | |
| 92 | /** |
Linus Walleij | 9dfac4f | 2012-02-01 18:02:47 +0100 | [diff] [blame] | 93 | * get_pinctrl_dev_from_devname() - look up pin controller device |
| 94 | * @devname: the name of a device instance, as returned by dev_name() |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 95 | * |
| 96 | * Looks up a pin control device matching a certain device name or pure device |
| 97 | * pointer, the pure device pointer will take precedence. |
| 98 | */ |
Linus Walleij | 9dfac4f | 2012-02-01 18:02:47 +0100 | [diff] [blame] | 99 | struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 100 | { |
| 101 | struct pinctrl_dev *pctldev = NULL; |
| 102 | bool found = false; |
| 103 | |
Linus Walleij | 9dfac4f | 2012-02-01 18:02:47 +0100 | [diff] [blame] | 104 | if (!devname) |
| 105 | return NULL; |
| 106 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 107 | list_for_each_entry(pctldev, &pinctrldev_list, node) { |
Linus Walleij | 9dfac4f | 2012-02-01 18:02:47 +0100 | [diff] [blame] | 108 | if (!strcmp(dev_name(pctldev->dev), devname)) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 109 | /* Matched on device name */ |
| 110 | found = true; |
| 111 | break; |
| 112 | } |
| 113 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 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 | /** |
Dong Aisheng | dcb5dbc | 2012-04-17 15:00:46 +0800 | [diff] [blame] | 144 | * pin_get_name_from_id() - look up a pin name from a pin id |
| 145 | * @pctldev: the pin control device to lookup the pin on |
| 146 | * @name: the name of the pin to look up |
| 147 | */ |
| 148 | const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin) |
| 149 | { |
| 150 | const struct pin_desc *desc; |
| 151 | |
| 152 | desc = pin_desc_get(pctldev, pin); |
| 153 | if (desc == NULL) { |
| 154 | dev_err(pctldev->dev, "failed to get pin(%d) name\n", |
| 155 | pin); |
| 156 | return NULL; |
| 157 | } |
| 158 | |
| 159 | return desc->name; |
| 160 | } |
| 161 | |
| 162 | /** |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 163 | * pin_is_valid() - check if pin exists on controller |
| 164 | * @pctldev: the pin control device to check the pin on |
| 165 | * @pin: pin to check, use the local pin controller index number |
| 166 | * |
| 167 | * This tells us whether a certain pin exist on a certain pin controller or |
| 168 | * not. Pin lists may be sparse, so some pins may not exist. |
| 169 | */ |
| 170 | bool pin_is_valid(struct pinctrl_dev *pctldev, int pin) |
| 171 | { |
| 172 | struct pin_desc *pindesc; |
| 173 | |
| 174 | if (pin < 0) |
| 175 | return false; |
| 176 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 177 | mutex_lock(&pinctrl_mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 178 | pindesc = pin_desc_get(pctldev, pin); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 179 | mutex_unlock(&pinctrl_mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 180 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 181 | return pindesc != NULL; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 182 | } |
| 183 | EXPORT_SYMBOL_GPL(pin_is_valid); |
| 184 | |
| 185 | /* Deletes a range of pin descriptors */ |
| 186 | static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev, |
| 187 | const struct pinctrl_pin_desc *pins, |
| 188 | unsigned num_pins) |
| 189 | { |
| 190 | int i; |
| 191 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 192 | for (i = 0; i < num_pins; i++) { |
| 193 | struct pin_desc *pindesc; |
| 194 | |
| 195 | pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, |
| 196 | pins[i].number); |
| 197 | if (pindesc != NULL) { |
| 198 | radix_tree_delete(&pctldev->pin_desc_tree, |
| 199 | pins[i].number); |
Linus Walleij | ca53c5f | 2011-12-14 20:33:37 +0100 | [diff] [blame] | 200 | if (pindesc->dynamic_name) |
| 201 | kfree(pindesc->name); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 202 | } |
| 203 | kfree(pindesc); |
| 204 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev, |
| 208 | unsigned number, const char *name) |
| 209 | { |
| 210 | struct pin_desc *pindesc; |
| 211 | |
| 212 | pindesc = pin_desc_get(pctldev, number); |
| 213 | if (pindesc != NULL) { |
| 214 | pr_err("pin %d already registered on %s\n", number, |
| 215 | pctldev->desc->name); |
| 216 | return -EINVAL; |
| 217 | } |
| 218 | |
| 219 | pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL); |
Stephen Warren | 95dcd4a | 2012-02-22 14:25:59 -0700 | [diff] [blame] | 220 | if (pindesc == NULL) { |
| 221 | dev_err(pctldev->dev, "failed to alloc struct pin_desc\n"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 222 | return -ENOMEM; |
Stephen Warren | 95dcd4a | 2012-02-22 14:25:59 -0700 | [diff] [blame] | 223 | } |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 224 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 225 | /* Set owner */ |
| 226 | pindesc->pctldev = pctldev; |
| 227 | |
Stephen Warren | 9af1e44 | 2011-10-19 16:19:27 -0600 | [diff] [blame] | 228 | /* Copy basic pin info */ |
Linus Walleij | 8dc6ae4 | 2012-02-01 18:11:40 +0100 | [diff] [blame] | 229 | if (name) { |
Linus Walleij | ca53c5f | 2011-12-14 20:33:37 +0100 | [diff] [blame] | 230 | pindesc->name = name; |
| 231 | } else { |
| 232 | pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number); |
| 233 | if (pindesc->name == NULL) |
| 234 | return -ENOMEM; |
| 235 | pindesc->dynamic_name = true; |
| 236 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 237 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 238 | radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 239 | pr_debug("registered pin %d (%s) on %s\n", |
Linus Walleij | ca53c5f | 2011-12-14 20:33:37 +0100 | [diff] [blame] | 240 | number, pindesc->name, pctldev->desc->name); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static int pinctrl_register_pins(struct pinctrl_dev *pctldev, |
| 245 | struct pinctrl_pin_desc const *pins, |
| 246 | unsigned num_descs) |
| 247 | { |
| 248 | unsigned i; |
| 249 | int ret = 0; |
| 250 | |
| 251 | for (i = 0; i < num_descs; i++) { |
| 252 | ret = pinctrl_register_one_pin(pctldev, |
| 253 | pins[i].number, pins[i].name); |
| 254 | if (ret) |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range |
| 263 | * @pctldev: pin controller device to check |
| 264 | * @gpio: gpio pin to check taken from the global GPIO pin space |
| 265 | * |
| 266 | * Tries to match a GPIO pin number to the ranges handled by a certain pin |
| 267 | * controller, return the range or NULL |
| 268 | */ |
| 269 | static struct pinctrl_gpio_range * |
| 270 | pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio) |
| 271 | { |
| 272 | struct pinctrl_gpio_range *range = NULL; |
| 273 | |
| 274 | /* Loop over the ranges */ |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 275 | list_for_each_entry(range, &pctldev->gpio_ranges, node) { |
| 276 | /* Check if we're in the valid range */ |
| 277 | if (gpio >= range->base && |
| 278 | gpio < range->base + range->npins) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 279 | return range; |
| 280 | } |
| 281 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 282 | |
| 283 | return NULL; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * pinctrl_get_device_gpio_range() - find device for GPIO range |
| 288 | * @gpio: the pin to locate the pin controller for |
| 289 | * @outdev: the pin control device if found |
| 290 | * @outrange: the GPIO range if found |
| 291 | * |
| 292 | * Find the pin controller handling a certain GPIO pin from the pinspace of |
| 293 | * the GPIO subsystem, return the device and the matching GPIO range. Returns |
Dong Aisheng | 4650b7c | 2012-04-25 19:38:13 +0800 | [diff] [blame] | 294 | * -EPROBE_DEFER if the GPIO range could not be found in any device since it |
| 295 | * may still have not been registered. |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 296 | */ |
Stephen Warren | 4ecce45d | 2012-02-19 23:45:47 -0700 | [diff] [blame] | 297 | static int pinctrl_get_device_gpio_range(unsigned gpio, |
| 298 | struct pinctrl_dev **outdev, |
| 299 | struct pinctrl_gpio_range **outrange) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 300 | { |
| 301 | struct pinctrl_dev *pctldev = NULL; |
| 302 | |
| 303 | /* Loop over the pin controllers */ |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 304 | list_for_each_entry(pctldev, &pinctrldev_list, node) { |
| 305 | struct pinctrl_gpio_range *range; |
| 306 | |
| 307 | range = pinctrl_match_gpio_range(pctldev, gpio); |
| 308 | if (range != NULL) { |
| 309 | *outdev = pctldev; |
| 310 | *outrange = range; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 311 | return 0; |
| 312 | } |
| 313 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 314 | |
Dong Aisheng | 4650b7c | 2012-04-25 19:38:13 +0800 | [diff] [blame] | 315 | return -EPROBE_DEFER; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /** |
| 319 | * pinctrl_add_gpio_range() - register a GPIO range for a controller |
| 320 | * @pctldev: pin controller device to add the range to |
| 321 | * @range: the GPIO range to add |
| 322 | * |
| 323 | * This adds a range of GPIOs to be handled by a certain pin controller. Call |
| 324 | * this to register handled ranges after registering your pin controller. |
| 325 | */ |
| 326 | void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev, |
| 327 | struct pinctrl_gpio_range *range) |
| 328 | { |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 329 | mutex_lock(&pinctrl_mutex); |
Stephen Warren | 8b9c139 | 2012-02-19 23:45:42 -0700 | [diff] [blame] | 330 | list_add_tail(&range->node, &pctldev->gpio_ranges); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 331 | mutex_unlock(&pinctrl_mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 332 | } |
Stephen Warren | 4ecce45d | 2012-02-19 23:45:47 -0700 | [diff] [blame] | 333 | EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 334 | |
| 335 | /** |
Linus Walleij | 7afde8b | 2011-10-19 17:07:16 +0200 | [diff] [blame] | 336 | * pinctrl_get_group_selector() - returns the group selector for a group |
| 337 | * @pctldev: the pin controller handling the group |
| 338 | * @pin_group: the pin group to look up |
| 339 | */ |
| 340 | int pinctrl_get_group_selector(struct pinctrl_dev *pctldev, |
| 341 | const char *pin_group) |
| 342 | { |
| 343 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 344 | unsigned ngroups = pctlops->get_groups_count(pctldev); |
Linus Walleij | 7afde8b | 2011-10-19 17:07:16 +0200 | [diff] [blame] | 345 | unsigned group_selector = 0; |
| 346 | |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 347 | while (group_selector < ngroups) { |
Linus Walleij | 7afde8b | 2011-10-19 17:07:16 +0200 | [diff] [blame] | 348 | const char *gname = pctlops->get_group_name(pctldev, |
| 349 | group_selector); |
| 350 | if (!strcmp(gname, pin_group)) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 351 | dev_dbg(pctldev->dev, |
Linus Walleij | 7afde8b | 2011-10-19 17:07:16 +0200 | [diff] [blame] | 352 | "found group selector %u for %s\n", |
| 353 | group_selector, |
| 354 | pin_group); |
| 355 | return group_selector; |
| 356 | } |
| 357 | |
| 358 | group_selector++; |
| 359 | } |
| 360 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 361 | dev_err(pctldev->dev, "does not have pin group %s\n", |
Linus Walleij | 7afde8b | 2011-10-19 17:07:16 +0200 | [diff] [blame] | 362 | pin_group); |
| 363 | |
| 364 | return -EINVAL; |
| 365 | } |
| 366 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 367 | /** |
| 368 | * pinctrl_request_gpio() - request a single pin to be used in as GPIO |
| 369 | * @gpio: the GPIO pin number from the GPIO subsystem number space |
| 370 | * |
| 371 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, |
| 372 | * as part of their gpio_request() semantics, platforms and individual drivers |
| 373 | * shall *NOT* request GPIO pins to be muxed in. |
| 374 | */ |
| 375 | int pinctrl_request_gpio(unsigned gpio) |
| 376 | { |
| 377 | struct pinctrl_dev *pctldev; |
| 378 | struct pinctrl_gpio_range *range; |
| 379 | int ret; |
| 380 | int pin; |
| 381 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 382 | mutex_lock(&pinctrl_mutex); |
| 383 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 384 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 385 | if (ret) { |
| 386 | mutex_unlock(&pinctrl_mutex); |
Dong Aisheng | 4650b7c | 2012-04-25 19:38:13 +0800 | [diff] [blame] | 387 | return ret; |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 388 | } |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 389 | |
| 390 | /* Convert to the pin controllers number space */ |
| 391 | pin = gpio - range->base + range->pin_base; |
| 392 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 393 | ret = pinmux_request_gpio(pctldev, range, pin, gpio); |
| 394 | |
| 395 | mutex_unlock(&pinctrl_mutex); |
| 396 | return ret; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 397 | } |
| 398 | EXPORT_SYMBOL_GPL(pinctrl_request_gpio); |
| 399 | |
| 400 | /** |
| 401 | * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO |
| 402 | * @gpio: the GPIO pin number from the GPIO subsystem number space |
| 403 | * |
| 404 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, |
| 405 | * as part of their gpio_free() semantics, platforms and individual drivers |
| 406 | * shall *NOT* request GPIO pins to be muxed out. |
| 407 | */ |
| 408 | void pinctrl_free_gpio(unsigned gpio) |
| 409 | { |
| 410 | struct pinctrl_dev *pctldev; |
| 411 | struct pinctrl_gpio_range *range; |
| 412 | int ret; |
| 413 | int pin; |
| 414 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 415 | mutex_lock(&pinctrl_mutex); |
| 416 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 417 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 418 | if (ret) { |
| 419 | mutex_unlock(&pinctrl_mutex); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 420 | return; |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 421 | } |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 422 | |
| 423 | /* Convert to the pin controllers number space */ |
| 424 | pin = gpio - range->base + range->pin_base; |
| 425 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 426 | pinmux_free_gpio(pctldev, pin, range); |
| 427 | |
| 428 | mutex_unlock(&pinctrl_mutex); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 429 | } |
| 430 | EXPORT_SYMBOL_GPL(pinctrl_free_gpio); |
| 431 | |
| 432 | static int pinctrl_gpio_direction(unsigned gpio, bool input) |
| 433 | { |
| 434 | struct pinctrl_dev *pctldev; |
| 435 | struct pinctrl_gpio_range *range; |
| 436 | int ret; |
| 437 | int pin; |
| 438 | |
| 439 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); |
| 440 | if (ret) |
| 441 | return ret; |
| 442 | |
| 443 | /* Convert to the pin controllers number space */ |
| 444 | pin = gpio - range->base + range->pin_base; |
| 445 | |
| 446 | return pinmux_gpio_direction(pctldev, range, pin, input); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode |
| 451 | * @gpio: the GPIO pin number from the GPIO subsystem number space |
| 452 | * |
| 453 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, |
| 454 | * as part of their gpio_direction_input() semantics, platforms and individual |
| 455 | * drivers shall *NOT* touch pin control GPIO calls. |
| 456 | */ |
| 457 | int pinctrl_gpio_direction_input(unsigned gpio) |
| 458 | { |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 459 | int ret; |
| 460 | mutex_lock(&pinctrl_mutex); |
| 461 | ret = pinctrl_gpio_direction(gpio, true); |
| 462 | mutex_unlock(&pinctrl_mutex); |
| 463 | return ret; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 464 | } |
| 465 | EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input); |
| 466 | |
| 467 | /** |
| 468 | * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode |
| 469 | * @gpio: the GPIO pin number from the GPIO subsystem number space |
| 470 | * |
| 471 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, |
| 472 | * as part of their gpio_direction_output() semantics, platforms and individual |
| 473 | * drivers shall *NOT* touch pin control GPIO calls. |
| 474 | */ |
| 475 | int pinctrl_gpio_direction_output(unsigned gpio) |
| 476 | { |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 477 | int ret; |
| 478 | mutex_lock(&pinctrl_mutex); |
| 479 | ret = pinctrl_gpio_direction(gpio, false); |
| 480 | mutex_unlock(&pinctrl_mutex); |
| 481 | return ret; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 482 | } |
| 483 | EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output); |
| 484 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 485 | static struct pinctrl_state *find_state(struct pinctrl *p, |
| 486 | const char *name) |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 487 | { |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 488 | struct pinctrl_state *state; |
| 489 | |
| 490 | list_for_each_entry(state, &p->states, node) |
| 491 | if (!strcmp(state->name, name)) |
| 492 | return state; |
| 493 | |
| 494 | return NULL; |
| 495 | } |
| 496 | |
| 497 | static struct pinctrl_state *create_state(struct pinctrl *p, |
| 498 | const char *name) |
| 499 | { |
| 500 | struct pinctrl_state *state; |
| 501 | |
| 502 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
| 503 | if (state == NULL) { |
| 504 | dev_err(p->dev, |
| 505 | "failed to alloc struct pinctrl_state\n"); |
| 506 | return ERR_PTR(-ENOMEM); |
| 507 | } |
| 508 | |
| 509 | state->name = name; |
| 510 | INIT_LIST_HEAD(&state->settings); |
| 511 | |
| 512 | list_add_tail(&state->node, &p->states); |
| 513 | |
| 514 | return state; |
| 515 | } |
| 516 | |
| 517 | static int add_setting(struct pinctrl *p, struct pinctrl_map const *map) |
| 518 | { |
| 519 | struct pinctrl_state *state; |
| 520 | struct pinctrl_setting *setting; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 521 | int ret; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 522 | |
| 523 | state = find_state(p, map->name); |
| 524 | if (!state) |
| 525 | state = create_state(p, map->name); |
| 526 | if (IS_ERR(state)) |
| 527 | return PTR_ERR(state); |
| 528 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 529 | if (map->type == PIN_MAP_TYPE_DUMMY_STATE) |
| 530 | return 0; |
| 531 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 532 | setting = kzalloc(sizeof(*setting), GFP_KERNEL); |
| 533 | if (setting == NULL) { |
| 534 | dev_err(p->dev, |
| 535 | "failed to alloc struct pinctrl_setting\n"); |
| 536 | return -ENOMEM; |
| 537 | } |
| 538 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 539 | setting->type = map->type; |
| 540 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 541 | setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name); |
| 542 | if (setting->pctldev == NULL) { |
Linus Walleij | c05127c | 2012-04-10 10:00:38 +0200 | [diff] [blame] | 543 | dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe", |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 544 | map->ctrl_dev_name); |
| 545 | kfree(setting); |
Linus Walleij | c05127c | 2012-04-10 10:00:38 +0200 | [diff] [blame] | 546 | /* |
| 547 | * OK let us guess that the driver is not there yet, and |
| 548 | * let's defer obtaining this pinctrl handle to later... |
| 549 | */ |
| 550 | return -EPROBE_DEFER; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 553 | switch (map->type) { |
| 554 | case PIN_MAP_TYPE_MUX_GROUP: |
| 555 | ret = pinmux_map_to_setting(map, setting); |
| 556 | break; |
| 557 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 558 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 559 | ret = pinconf_map_to_setting(map, setting); |
| 560 | break; |
| 561 | default: |
| 562 | ret = -EINVAL; |
| 563 | break; |
| 564 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 565 | if (ret < 0) { |
| 566 | kfree(setting); |
| 567 | return ret; |
| 568 | } |
| 569 | |
| 570 | list_add_tail(&setting->node, &state->settings); |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | static struct pinctrl *find_pinctrl(struct device *dev) |
| 576 | { |
| 577 | struct pinctrl *p; |
| 578 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 579 | list_for_each_entry(p, &pinctrl_list, node) |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 580 | if (p->dev == dev) |
| 581 | return p; |
| 582 | |
| 583 | return NULL; |
| 584 | } |
| 585 | |
| 586 | static void pinctrl_put_locked(struct pinctrl *p, bool inlist); |
| 587 | |
| 588 | static struct pinctrl *create_pinctrl(struct device *dev) |
| 589 | { |
| 590 | struct pinctrl *p; |
| 591 | const char *devname; |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 592 | struct pinctrl_maps *maps_node; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 593 | int i; |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 594 | struct pinctrl_map const *map; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 595 | int ret; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 596 | |
| 597 | /* |
| 598 | * create the state cookie holder struct pinctrl for each |
| 599 | * mapping, this is what consumers will get when requesting |
| 600 | * a pin control handle with pinctrl_get() |
| 601 | */ |
Stephen Warren | 02f5b98 | 2012-02-22 14:26:00 -0700 | [diff] [blame] | 602 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
Stephen Warren | 95dcd4a | 2012-02-22 14:25:59 -0700 | [diff] [blame] | 603 | if (p == NULL) { |
| 604 | dev_err(dev, "failed to alloc struct pinctrl\n"); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 605 | return ERR_PTR(-ENOMEM); |
Stephen Warren | 95dcd4a | 2012-02-22 14:25:59 -0700 | [diff] [blame] | 606 | } |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 607 | p->dev = dev; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 608 | INIT_LIST_HEAD(&p->states); |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 609 | INIT_LIST_HEAD(&p->dt_maps); |
| 610 | |
| 611 | ret = pinctrl_dt_to_map(p); |
| 612 | if (ret < 0) { |
| 613 | kfree(p); |
| 614 | return ERR_PTR(ret); |
| 615 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 616 | |
| 617 | devname = dev_name(dev); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 618 | |
| 619 | /* Iterate over the pin control maps to locate the right ones */ |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 620 | for_each_maps(maps_node, i, map) { |
Stephen Warren | 1681f5a | 2012-02-22 14:25:58 -0700 | [diff] [blame] | 621 | /* Map must be for this device */ |
| 622 | if (strcmp(map->dev_name, devname)) |
| 623 | continue; |
| 624 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 625 | ret = add_setting(p, map); |
| 626 | if (ret < 0) { |
| 627 | pinctrl_put_locked(p, false); |
| 628 | return ERR_PTR(ret); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 632 | /* Add the pinmux to the global list */ |
Stephen Warren | 8b9c139 | 2012-02-19 23:45:42 -0700 | [diff] [blame] | 633 | list_add_tail(&p->node, &pinctrl_list); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 634 | |
| 635 | return p; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 636 | } |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 637 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 638 | static struct pinctrl *pinctrl_get_locked(struct device *dev) |
| 639 | { |
| 640 | struct pinctrl *p; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 641 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 642 | if (WARN_ON(!dev)) |
| 643 | return ERR_PTR(-EINVAL); |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 644 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 645 | p = find_pinctrl(dev); |
| 646 | if (p != NULL) |
| 647 | return ERR_PTR(-EBUSY); |
| 648 | |
| 649 | p = create_pinctrl(dev); |
| 650 | if (IS_ERR(p)) |
| 651 | return p; |
| 652 | |
| 653 | return p; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 654 | } |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 655 | |
| 656 | /** |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 657 | * pinctrl_get() - retrieves the pinctrl handle for a device |
| 658 | * @dev: the device to obtain the handle for |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 659 | */ |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 660 | struct pinctrl *pinctrl_get(struct device *dev) |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 661 | { |
| 662 | struct pinctrl *p; |
| 663 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 664 | mutex_lock(&pinctrl_mutex); |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 665 | p = pinctrl_get_locked(dev); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 666 | mutex_unlock(&pinctrl_mutex); |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 667 | |
| 668 | return p; |
| 669 | } |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 670 | EXPORT_SYMBOL_GPL(pinctrl_get); |
| 671 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 672 | static void pinctrl_put_locked(struct pinctrl *p, bool inlist) |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 673 | { |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 674 | struct pinctrl_state *state, *n1; |
| 675 | struct pinctrl_setting *setting, *n2; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 676 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 677 | list_for_each_entry_safe(state, n1, &p->states, node) { |
| 678 | list_for_each_entry_safe(setting, n2, &state->settings, node) { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 679 | switch (setting->type) { |
| 680 | case PIN_MAP_TYPE_MUX_GROUP: |
| 681 | if (state == p->state) |
| 682 | pinmux_disable_setting(setting); |
| 683 | pinmux_free_setting(setting); |
| 684 | break; |
| 685 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 686 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 687 | pinconf_free_setting(setting); |
| 688 | break; |
| 689 | default: |
| 690 | break; |
| 691 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 692 | list_del(&setting->node); |
| 693 | kfree(setting); |
| 694 | } |
| 695 | list_del(&state->node); |
| 696 | kfree(state); |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 697 | } |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 698 | |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 699 | pinctrl_dt_free_maps(p); |
| 700 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 701 | if (inlist) |
| 702 | list_del(&p->node); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 703 | kfree(p); |
| 704 | } |
| 705 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 706 | /** |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 707 | * pinctrl_put() - release a previously claimed pinctrl handle |
| 708 | * @p: the pinctrl handle to release |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 709 | */ |
| 710 | void pinctrl_put(struct pinctrl *p) |
| 711 | { |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 712 | mutex_lock(&pinctrl_mutex); |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 713 | pinctrl_put_locked(p, true); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 714 | mutex_unlock(&pinctrl_mutex); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 715 | } |
| 716 | EXPORT_SYMBOL_GPL(pinctrl_put); |
| 717 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 718 | static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p, |
| 719 | const char *name) |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 720 | { |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 721 | struct pinctrl_state *state; |
| 722 | |
| 723 | state = find_state(p, name); |
Dong Aisheng | 5b3aa5f | 2012-04-26 16:15:50 +0800 | [diff] [blame] | 724 | if (!state) { |
| 725 | if (pinctrl_dummy_state) { |
| 726 | /* create dummy state */ |
| 727 | dev_dbg(p->dev, "using pinctrl dummy state (%s)\n", |
| 728 | name); |
| 729 | state = create_state(p, name); |
| 730 | if (IS_ERR(state)) |
| 731 | return state; |
| 732 | } else { |
| 733 | return ERR_PTR(-ENODEV); |
| 734 | } |
| 735 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 736 | |
| 737 | return state; |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle |
| 742 | * @p: the pinctrl handle to retrieve the state from |
| 743 | * @name: the state name to retrieve |
| 744 | */ |
| 745 | struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name) |
| 746 | { |
| 747 | struct pinctrl_state *s; |
| 748 | |
| 749 | mutex_lock(&pinctrl_mutex); |
| 750 | s = pinctrl_lookup_state_locked(p, name); |
| 751 | mutex_unlock(&pinctrl_mutex); |
| 752 | |
| 753 | return s; |
| 754 | } |
| 755 | EXPORT_SYMBOL_GPL(pinctrl_lookup_state); |
| 756 | |
| 757 | static int pinctrl_select_state_locked(struct pinctrl *p, |
| 758 | struct pinctrl_state *state) |
| 759 | { |
| 760 | struct pinctrl_setting *setting, *setting2; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 761 | int ret; |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 762 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 763 | if (p->state == state) |
| 764 | return 0; |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 765 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 766 | if (p->state) { |
| 767 | /* |
| 768 | * The set of groups with a mux configuration in the old state |
| 769 | * may not be identical to the set of groups with a mux setting |
| 770 | * in the new state. While this might be unusual, it's entirely |
| 771 | * possible for the "user"-supplied mapping table to be written |
| 772 | * that way. For each group that was configured in the old state |
| 773 | * but not in the new state, this code puts that group into a |
| 774 | * safe/disabled state. |
| 775 | */ |
| 776 | list_for_each_entry(setting, &p->state->settings, node) { |
| 777 | bool found = false; |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 778 | if (setting->type != PIN_MAP_TYPE_MUX_GROUP) |
| 779 | continue; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 780 | list_for_each_entry(setting2, &state->settings, node) { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 781 | if (setting2->type != PIN_MAP_TYPE_MUX_GROUP) |
| 782 | continue; |
| 783 | if (setting2->data.mux.group == |
| 784 | setting->data.mux.group) { |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 785 | found = true; |
| 786 | break; |
| 787 | } |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 788 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 789 | if (!found) |
| 790 | pinmux_disable_setting(setting); |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | p->state = state; |
| 795 | |
| 796 | /* Apply all the settings for the new state */ |
| 797 | list_for_each_entry(setting, &state->settings, node) { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 798 | switch (setting->type) { |
| 799 | case PIN_MAP_TYPE_MUX_GROUP: |
| 800 | ret = pinmux_enable_setting(setting); |
| 801 | break; |
| 802 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 803 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 804 | ret = pinconf_apply_setting(setting); |
| 805 | break; |
| 806 | default: |
| 807 | ret = -EINVAL; |
| 808 | break; |
| 809 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 810 | if (ret < 0) { |
| 811 | /* FIXME: Difficult to return to prev state */ |
| 812 | return ret; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 813 | } |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 816 | return 0; |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 819 | /** |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 820 | * pinctrl_select() - select/activate/program a pinctrl state to HW |
| 821 | * @p: the pinctrl handle for the device that requests configuratio |
| 822 | * @state: the state handle to select/activate/program |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 823 | */ |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 824 | int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state) |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 825 | { |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 826 | int ret; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 827 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 828 | mutex_lock(&pinctrl_mutex); |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 829 | ret = pinctrl_select_state_locked(p, state); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 830 | mutex_unlock(&pinctrl_mutex); |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 831 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 832 | return ret; |
| 833 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 834 | EXPORT_SYMBOL_GPL(pinctrl_select_state); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 835 | |
Stephen Warren | 6d4ca1f | 2012-04-16 10:51:00 -0600 | [diff] [blame] | 836 | static void devm_pinctrl_release(struct device *dev, void *res) |
| 837 | { |
| 838 | pinctrl_put(*(struct pinctrl **)res); |
| 839 | } |
| 840 | |
| 841 | /** |
| 842 | * struct devm_pinctrl_get() - Resource managed pinctrl_get() |
| 843 | * @dev: the device to obtain the handle for |
| 844 | * |
| 845 | * If there is a need to explicitly destroy the returned struct pinctrl, |
| 846 | * devm_pinctrl_put() should be used, rather than plain pinctrl_put(). |
| 847 | */ |
| 848 | struct pinctrl *devm_pinctrl_get(struct device *dev) |
| 849 | { |
| 850 | struct pinctrl **ptr, *p; |
| 851 | |
| 852 | ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL); |
| 853 | if (!ptr) |
| 854 | return ERR_PTR(-ENOMEM); |
| 855 | |
| 856 | p = pinctrl_get(dev); |
| 857 | if (!IS_ERR(p)) { |
| 858 | *ptr = p; |
| 859 | devres_add(dev, ptr); |
| 860 | } else { |
| 861 | devres_free(ptr); |
| 862 | } |
| 863 | |
| 864 | return p; |
| 865 | } |
| 866 | EXPORT_SYMBOL_GPL(devm_pinctrl_get); |
| 867 | |
| 868 | static int devm_pinctrl_match(struct device *dev, void *res, void *data) |
| 869 | { |
| 870 | struct pinctrl **p = res; |
| 871 | |
| 872 | return *p == data; |
| 873 | } |
| 874 | |
| 875 | /** |
| 876 | * devm_pinctrl_put() - Resource managed pinctrl_put() |
| 877 | * @p: the pinctrl handle to release |
| 878 | * |
| 879 | * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally |
| 880 | * this function will not need to be called and the resource management |
| 881 | * code will ensure that the resource is freed. |
| 882 | */ |
| 883 | void devm_pinctrl_put(struct pinctrl *p) |
| 884 | { |
| 885 | WARN_ON(devres_destroy(p->dev, devm_pinctrl_release, |
| 886 | devm_pinctrl_match, p)); |
| 887 | pinctrl_put(p); |
| 888 | } |
| 889 | EXPORT_SYMBOL_GPL(devm_pinctrl_put); |
| 890 | |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 891 | int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps, |
| 892 | bool dup, bool locked) |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 893 | { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 894 | int i, ret; |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 895 | struct pinctrl_maps *maps_node; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 896 | |
| 897 | pr_debug("add %d pinmux maps\n", num_maps); |
| 898 | |
| 899 | /* First sanity check the new mapping */ |
| 900 | for (i = 0; i < num_maps; i++) { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 901 | if (!maps[i].dev_name) { |
| 902 | pr_err("failed to register map %s (%d): no device given\n", |
| 903 | maps[i].name, i); |
| 904 | return -EINVAL; |
| 905 | } |
| 906 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 907 | if (!maps[i].name) { |
| 908 | pr_err("failed to register map %d: no map name given\n", |
Stephen Warren | 95dcd4a | 2012-02-22 14:25:59 -0700 | [diff] [blame] | 909 | i); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 910 | return -EINVAL; |
| 911 | } |
| 912 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 913 | if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE && |
| 914 | !maps[i].ctrl_dev_name) { |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 915 | pr_err("failed to register map %s (%d): no pin control device given\n", |
| 916 | maps[i].name, i); |
| 917 | return -EINVAL; |
| 918 | } |
| 919 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 920 | switch (maps[i].type) { |
| 921 | case PIN_MAP_TYPE_DUMMY_STATE: |
| 922 | break; |
| 923 | case PIN_MAP_TYPE_MUX_GROUP: |
| 924 | ret = pinmux_validate_map(&maps[i], i); |
| 925 | if (ret < 0) |
Stephen Warren | fde04f4 | 2012-04-25 10:32:16 -0600 | [diff] [blame] | 926 | return ret; |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 927 | break; |
| 928 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 929 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 930 | ret = pinconf_validate_map(&maps[i], i); |
| 931 | if (ret < 0) |
Stephen Warren | fde04f4 | 2012-04-25 10:32:16 -0600 | [diff] [blame] | 932 | return ret; |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 933 | break; |
| 934 | default: |
| 935 | pr_err("failed to register map %s (%d): invalid type given\n", |
Stephen Warren | 95dcd4a | 2012-02-22 14:25:59 -0700 | [diff] [blame] | 936 | maps[i].name, i); |
Stephen Warren | 1681f5a | 2012-02-22 14:25:58 -0700 | [diff] [blame] | 937 | return -EINVAL; |
| 938 | } |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 939 | } |
| 940 | |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 941 | maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL); |
| 942 | if (!maps_node) { |
| 943 | pr_err("failed to alloc struct pinctrl_maps\n"); |
| 944 | return -ENOMEM; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 945 | } |
| 946 | |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 947 | maps_node->num_maps = num_maps; |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 948 | if (dup) { |
| 949 | maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps, |
| 950 | GFP_KERNEL); |
| 951 | if (!maps_node->maps) { |
| 952 | pr_err("failed to duplicate mapping table\n"); |
| 953 | kfree(maps_node); |
| 954 | return -ENOMEM; |
| 955 | } |
| 956 | } else { |
| 957 | maps_node->maps = maps; |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 960 | if (!locked) |
| 961 | mutex_lock(&pinctrl_mutex); |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 962 | list_add_tail(&maps_node->node, &pinctrl_maps); |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 963 | if (!locked) |
| 964 | mutex_unlock(&pinctrl_mutex); |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 965 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 966 | return 0; |
| 967 | } |
| 968 | |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 969 | /** |
| 970 | * pinctrl_register_mappings() - register a set of pin controller mappings |
| 971 | * @maps: the pincontrol mappings table to register. This should probably be |
| 972 | * marked with __initdata so it can be discarded after boot. This |
| 973 | * function will perform a shallow copy for the mapping entries. |
| 974 | * @num_maps: the number of maps in the mapping table |
| 975 | */ |
| 976 | int pinctrl_register_mappings(struct pinctrl_map const *maps, |
| 977 | unsigned num_maps) |
| 978 | { |
| 979 | return pinctrl_register_map(maps, num_maps, true, false); |
| 980 | } |
| 981 | |
| 982 | void pinctrl_unregister_map(struct pinctrl_map const *map) |
| 983 | { |
| 984 | struct pinctrl_maps *maps_node; |
| 985 | |
| 986 | list_for_each_entry(maps_node, &pinctrl_maps, node) { |
| 987 | if (maps_node->maps == map) { |
| 988 | list_del(&maps_node->node); |
| 989 | return; |
| 990 | } |
| 991 | } |
| 992 | } |
| 993 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 994 | #ifdef CONFIG_DEBUG_FS |
| 995 | |
| 996 | static int pinctrl_pins_show(struct seq_file *s, void *what) |
| 997 | { |
| 998 | struct pinctrl_dev *pctldev = s->private; |
| 999 | const struct pinctrl_ops *ops = pctldev->desc->pctlops; |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 1000 | unsigned i, pin; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1001 | |
| 1002 | seq_printf(s, "registered pins: %d\n", pctldev->desc->npins); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1003 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1004 | mutex_lock(&pinctrl_mutex); |
| 1005 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 1006 | /* The pin number can be retrived from the pin controller descriptor */ |
| 1007 | for (i = 0; i < pctldev->desc->npins; i++) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1008 | struct pin_desc *desc; |
| 1009 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 1010 | pin = pctldev->desc->pins[i].number; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1011 | desc = pin_desc_get(pctldev, pin); |
| 1012 | /* Pin space may be sparse */ |
| 1013 | if (desc == NULL) |
| 1014 | continue; |
| 1015 | |
| 1016 | seq_printf(s, "pin %d (%s) ", pin, |
| 1017 | desc->name ? desc->name : "unnamed"); |
| 1018 | |
| 1019 | /* Driver-specific info per pin */ |
| 1020 | if (ops->pin_dbg_show) |
| 1021 | ops->pin_dbg_show(pctldev, s, pin); |
| 1022 | |
| 1023 | seq_puts(s, "\n"); |
| 1024 | } |
| 1025 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1026 | mutex_unlock(&pinctrl_mutex); |
| 1027 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1028 | return 0; |
| 1029 | } |
| 1030 | |
| 1031 | static int pinctrl_groups_show(struct seq_file *s, void *what) |
| 1032 | { |
| 1033 | struct pinctrl_dev *pctldev = s->private; |
| 1034 | const struct pinctrl_ops *ops = pctldev->desc->pctlops; |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 1035 | unsigned ngroups, selector = 0; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1036 | |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 1037 | ngroups = ops->get_groups_count(pctldev); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1038 | mutex_lock(&pinctrl_mutex); |
| 1039 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1040 | seq_puts(s, "registered pin groups:\n"); |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 1041 | while (selector < ngroups) { |
Stephen Warren | a5818a8 | 2011-10-19 16:19:25 -0600 | [diff] [blame] | 1042 | const unsigned *pins; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1043 | unsigned num_pins; |
| 1044 | const char *gname = ops->get_group_name(pctldev, selector); |
Dong Aisheng | dcb5dbc | 2012-04-17 15:00:46 +0800 | [diff] [blame] | 1045 | const char *pname; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1046 | int ret; |
| 1047 | int i; |
| 1048 | |
| 1049 | ret = ops->get_group_pins(pctldev, selector, |
| 1050 | &pins, &num_pins); |
| 1051 | if (ret) |
| 1052 | seq_printf(s, "%s [ERROR GETTING PINS]\n", |
| 1053 | gname); |
| 1054 | else { |
Dong Aisheng | dcb5dbc | 2012-04-17 15:00:46 +0800 | [diff] [blame] | 1055 | seq_printf(s, "group: %s\n", gname); |
| 1056 | for (i = 0; i < num_pins; i++) { |
| 1057 | pname = pin_get_name(pctldev, pins[i]); |
| 1058 | if (WARN_ON(!pname)) |
| 1059 | return -EINVAL; |
| 1060 | seq_printf(s, "pin %d (%s)\n", pins[i], pname); |
| 1061 | } |
| 1062 | seq_puts(s, "\n"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1063 | } |
| 1064 | selector++; |
| 1065 | } |
| 1066 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1067 | mutex_unlock(&pinctrl_mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1068 | |
| 1069 | return 0; |
| 1070 | } |
| 1071 | |
| 1072 | static int pinctrl_gpioranges_show(struct seq_file *s, void *what) |
| 1073 | { |
| 1074 | struct pinctrl_dev *pctldev = s->private; |
| 1075 | struct pinctrl_gpio_range *range = NULL; |
| 1076 | |
| 1077 | seq_puts(s, "GPIO ranges handled:\n"); |
| 1078 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1079 | mutex_lock(&pinctrl_mutex); |
| 1080 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1081 | /* Loop over the ranges */ |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1082 | list_for_each_entry(range, &pctldev->gpio_ranges, node) { |
Linus Walleij | 75d6642 | 2011-11-16 09:58:51 +0100 | [diff] [blame] | 1083 | seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n", |
| 1084 | range->id, range->name, |
| 1085 | range->base, (range->base + range->npins - 1), |
| 1086 | range->pin_base, |
| 1087 | (range->pin_base + range->npins - 1)); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1088 | } |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1089 | |
| 1090 | mutex_unlock(&pinctrl_mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1091 | |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
| 1095 | static int pinctrl_devices_show(struct seq_file *s, void *what) |
| 1096 | { |
| 1097 | struct pinctrl_dev *pctldev; |
| 1098 | |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 1099 | seq_puts(s, "name [pinmux] [pinconf]\n"); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1100 | |
| 1101 | mutex_lock(&pinctrl_mutex); |
| 1102 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1103 | list_for_each_entry(pctldev, &pinctrldev_list, node) { |
| 1104 | seq_printf(s, "%s ", pctldev->desc->name); |
| 1105 | if (pctldev->desc->pmxops) |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 1106 | seq_puts(s, "yes "); |
| 1107 | else |
| 1108 | seq_puts(s, "no "); |
| 1109 | if (pctldev->desc->confops) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1110 | seq_puts(s, "yes"); |
| 1111 | else |
| 1112 | seq_puts(s, "no"); |
| 1113 | seq_puts(s, "\n"); |
| 1114 | } |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1115 | |
| 1116 | mutex_unlock(&pinctrl_mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1117 | |
| 1118 | return 0; |
| 1119 | } |
| 1120 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 1121 | static inline const char *map_type(enum pinctrl_map_type type) |
| 1122 | { |
| 1123 | static const char * const names[] = { |
| 1124 | "INVALID", |
| 1125 | "DUMMY_STATE", |
| 1126 | "MUX_GROUP", |
| 1127 | "CONFIGS_PIN", |
| 1128 | "CONFIGS_GROUP", |
| 1129 | }; |
| 1130 | |
| 1131 | if (type >= ARRAY_SIZE(names)) |
| 1132 | return "UNKNOWN"; |
| 1133 | |
| 1134 | return names[type]; |
| 1135 | } |
| 1136 | |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1137 | static int pinctrl_maps_show(struct seq_file *s, void *what) |
| 1138 | { |
| 1139 | struct pinctrl_maps *maps_node; |
| 1140 | int i; |
| 1141 | struct pinctrl_map const *map; |
| 1142 | |
| 1143 | seq_puts(s, "Pinctrl maps:\n"); |
| 1144 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1145 | mutex_lock(&pinctrl_mutex); |
| 1146 | |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1147 | for_each_maps(maps_node, i, map) { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 1148 | seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n", |
| 1149 | map->dev_name, map->name, map_type(map->type), |
| 1150 | map->type); |
| 1151 | |
| 1152 | if (map->type != PIN_MAP_TYPE_DUMMY_STATE) |
| 1153 | seq_printf(s, "controlling device %s\n", |
| 1154 | map->ctrl_dev_name); |
| 1155 | |
| 1156 | switch (map->type) { |
| 1157 | case PIN_MAP_TYPE_MUX_GROUP: |
| 1158 | pinmux_show_map(s, map); |
| 1159 | break; |
| 1160 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 1161 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 1162 | pinconf_show_map(s, map); |
| 1163 | break; |
| 1164 | default: |
| 1165 | break; |
| 1166 | } |
| 1167 | |
| 1168 | seq_printf(s, "\n"); |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1169 | } |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1170 | |
| 1171 | mutex_unlock(&pinctrl_mutex); |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1172 | |
| 1173 | return 0; |
| 1174 | } |
| 1175 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1176 | static int pinctrl_show(struct seq_file *s, void *what) |
| 1177 | { |
| 1178 | struct pinctrl *p; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1179 | struct pinctrl_state *state; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 1180 | struct pinctrl_setting *setting; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1181 | |
| 1182 | seq_puts(s, "Requested pin control handlers their pinmux maps:\n"); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1183 | |
| 1184 | mutex_lock(&pinctrl_mutex); |
| 1185 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1186 | list_for_each_entry(p, &pinctrl_list, node) { |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1187 | seq_printf(s, "device: %s current state: %s\n", |
| 1188 | dev_name(p->dev), |
| 1189 | p->state ? p->state->name : "none"); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1190 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1191 | list_for_each_entry(state, &p->states, node) { |
| 1192 | seq_printf(s, " state: %s\n", state->name); |
| 1193 | |
| 1194 | list_for_each_entry(setting, &state->settings, node) { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 1195 | struct pinctrl_dev *pctldev = setting->pctldev; |
| 1196 | |
| 1197 | seq_printf(s, " type: %s controller %s ", |
| 1198 | map_type(setting->type), |
| 1199 | pinctrl_dev_get_name(pctldev)); |
| 1200 | |
| 1201 | switch (setting->type) { |
| 1202 | case PIN_MAP_TYPE_MUX_GROUP: |
| 1203 | pinmux_show_setting(s, setting); |
| 1204 | break; |
| 1205 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 1206 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 1207 | pinconf_show_setting(s, setting); |
| 1208 | break; |
| 1209 | default: |
| 1210 | break; |
| 1211 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1212 | } |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1213 | } |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1214 | } |
| 1215 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1216 | mutex_unlock(&pinctrl_mutex); |
| 1217 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1218 | return 0; |
| 1219 | } |
| 1220 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1221 | static int pinctrl_pins_open(struct inode *inode, struct file *file) |
| 1222 | { |
| 1223 | return single_open(file, pinctrl_pins_show, inode->i_private); |
| 1224 | } |
| 1225 | |
| 1226 | static int pinctrl_groups_open(struct inode *inode, struct file *file) |
| 1227 | { |
| 1228 | return single_open(file, pinctrl_groups_show, inode->i_private); |
| 1229 | } |
| 1230 | |
| 1231 | static int pinctrl_gpioranges_open(struct inode *inode, struct file *file) |
| 1232 | { |
| 1233 | return single_open(file, pinctrl_gpioranges_show, inode->i_private); |
| 1234 | } |
| 1235 | |
| 1236 | static int pinctrl_devices_open(struct inode *inode, struct file *file) |
| 1237 | { |
| 1238 | return single_open(file, pinctrl_devices_show, NULL); |
| 1239 | } |
| 1240 | |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1241 | static int pinctrl_maps_open(struct inode *inode, struct file *file) |
| 1242 | { |
| 1243 | return single_open(file, pinctrl_maps_show, NULL); |
| 1244 | } |
| 1245 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1246 | static int pinctrl_open(struct inode *inode, struct file *file) |
| 1247 | { |
| 1248 | return single_open(file, pinctrl_show, NULL); |
| 1249 | } |
| 1250 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1251 | static const struct file_operations pinctrl_pins_ops = { |
| 1252 | .open = pinctrl_pins_open, |
| 1253 | .read = seq_read, |
| 1254 | .llseek = seq_lseek, |
| 1255 | .release = single_release, |
| 1256 | }; |
| 1257 | |
| 1258 | static const struct file_operations pinctrl_groups_ops = { |
| 1259 | .open = pinctrl_groups_open, |
| 1260 | .read = seq_read, |
| 1261 | .llseek = seq_lseek, |
| 1262 | .release = single_release, |
| 1263 | }; |
| 1264 | |
| 1265 | static const struct file_operations pinctrl_gpioranges_ops = { |
| 1266 | .open = pinctrl_gpioranges_open, |
| 1267 | .read = seq_read, |
| 1268 | .llseek = seq_lseek, |
| 1269 | .release = single_release, |
| 1270 | }; |
| 1271 | |
| 1272 | static const struct file_operations pinctrl_devices_ops = { |
| 1273 | .open = pinctrl_devices_open, |
| 1274 | .read = seq_read, |
| 1275 | .llseek = seq_lseek, |
| 1276 | .release = single_release, |
| 1277 | }; |
| 1278 | |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1279 | static const struct file_operations pinctrl_maps_ops = { |
| 1280 | .open = pinctrl_maps_open, |
| 1281 | .read = seq_read, |
| 1282 | .llseek = seq_lseek, |
| 1283 | .release = single_release, |
| 1284 | }; |
| 1285 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1286 | static const struct file_operations pinctrl_ops = { |
| 1287 | .open = pinctrl_open, |
| 1288 | .read = seq_read, |
| 1289 | .llseek = seq_lseek, |
| 1290 | .release = single_release, |
| 1291 | }; |
| 1292 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1293 | static struct dentry *debugfs_root; |
| 1294 | |
| 1295 | static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) |
| 1296 | { |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1297 | struct dentry *device_root; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1298 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1299 | device_root = debugfs_create_dir(dev_name(pctldev->dev), |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1300 | debugfs_root); |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1301 | pctldev->device_root = device_root; |
| 1302 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1303 | if (IS_ERR(device_root) || !device_root) { |
| 1304 | pr_warn("failed to create debugfs directory for %s\n", |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1305 | dev_name(pctldev->dev)); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1306 | return; |
| 1307 | } |
| 1308 | debugfs_create_file("pins", S_IFREG | S_IRUGO, |
| 1309 | device_root, pctldev, &pinctrl_pins_ops); |
| 1310 | debugfs_create_file("pingroups", S_IFREG | S_IRUGO, |
| 1311 | device_root, pctldev, &pinctrl_groups_ops); |
| 1312 | debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO, |
| 1313 | device_root, pctldev, &pinctrl_gpioranges_ops); |
| 1314 | pinmux_init_device_debugfs(device_root, pctldev); |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 1315 | pinconf_init_device_debugfs(device_root, pctldev); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1316 | } |
| 1317 | |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1318 | static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev) |
| 1319 | { |
| 1320 | debugfs_remove_recursive(pctldev->device_root); |
| 1321 | } |
| 1322 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1323 | static void pinctrl_init_debugfs(void) |
| 1324 | { |
| 1325 | debugfs_root = debugfs_create_dir("pinctrl", NULL); |
| 1326 | if (IS_ERR(debugfs_root) || !debugfs_root) { |
| 1327 | pr_warn("failed to create debugfs directory\n"); |
| 1328 | debugfs_root = NULL; |
| 1329 | return; |
| 1330 | } |
| 1331 | |
| 1332 | debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO, |
| 1333 | debugfs_root, NULL, &pinctrl_devices_ops); |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1334 | debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO, |
| 1335 | debugfs_root, NULL, &pinctrl_maps_ops); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1336 | debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO, |
| 1337 | debugfs_root, NULL, &pinctrl_ops); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | #else /* CONFIG_DEBUG_FS */ |
| 1341 | |
| 1342 | static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) |
| 1343 | { |
| 1344 | } |
| 1345 | |
| 1346 | static void pinctrl_init_debugfs(void) |
| 1347 | { |
| 1348 | } |
| 1349 | |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1350 | static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev) |
| 1351 | { |
| 1352 | } |
| 1353 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1354 | #endif |
| 1355 | |
Stephen Warren | d26bc49 | 2012-03-16 14:54:25 -0600 | [diff] [blame] | 1356 | static int pinctrl_check_ops(struct pinctrl_dev *pctldev) |
| 1357 | { |
| 1358 | const struct pinctrl_ops *ops = pctldev->desc->pctlops; |
| 1359 | |
| 1360 | if (!ops || |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 1361 | !ops->get_groups_count || |
Stephen Warren | d26bc49 | 2012-03-16 14:54:25 -0600 | [diff] [blame] | 1362 | !ops->get_group_name || |
| 1363 | !ops->get_group_pins) |
| 1364 | return -EINVAL; |
| 1365 | |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 1366 | if (ops->dt_node_to_map && !ops->dt_free_map) |
| 1367 | return -EINVAL; |
| 1368 | |
Stephen Warren | d26bc49 | 2012-03-16 14:54:25 -0600 | [diff] [blame] | 1369 | return 0; |
| 1370 | } |
| 1371 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1372 | /** |
| 1373 | * pinctrl_register() - register a pin controller device |
| 1374 | * @pctldesc: descriptor for this pin controller |
| 1375 | * @dev: parent device for this pin controller |
| 1376 | * @driver_data: private pin controller data for this pin controller |
| 1377 | */ |
| 1378 | struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc, |
| 1379 | struct device *dev, void *driver_data) |
| 1380 | { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1381 | struct pinctrl_dev *pctldev; |
| 1382 | int ret; |
| 1383 | |
Devendra Naga | da9aecb | 2012-06-16 23:43:16 +0530 | [diff] [blame] | 1384 | if (!pctldesc) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1385 | return NULL; |
Devendra Naga | da9aecb | 2012-06-16 23:43:16 +0530 | [diff] [blame] | 1386 | if (!pctldesc->name) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1387 | return NULL; |
| 1388 | |
Stephen Warren | 02f5b98 | 2012-02-22 14:26:00 -0700 | [diff] [blame] | 1389 | pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL); |
Stephen Warren | 95dcd4a | 2012-02-22 14:25:59 -0700 | [diff] [blame] | 1390 | if (pctldev == NULL) { |
| 1391 | dev_err(dev, "failed to alloc struct pinctrl_dev\n"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1392 | return NULL; |
Stephen Warren | 95dcd4a | 2012-02-22 14:25:59 -0700 | [diff] [blame] | 1393 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1394 | |
| 1395 | /* Initialize pin control device struct */ |
| 1396 | pctldev->owner = pctldesc->owner; |
| 1397 | pctldev->desc = pctldesc; |
| 1398 | pctldev->driver_data = driver_data; |
| 1399 | INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1400 | INIT_LIST_HEAD(&pctldev->gpio_ranges); |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1401 | pctldev->dev = dev; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1402 | |
Stephen Warren | d26bc49 | 2012-03-16 14:54:25 -0600 | [diff] [blame] | 1403 | /* check core ops for sanity */ |
Devendra Naga | da9aecb | 2012-06-16 23:43:16 +0530 | [diff] [blame] | 1404 | if (pinctrl_check_ops(pctldev)) { |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 1405 | dev_err(dev, "pinctrl ops lacks necessary functions\n"); |
Stephen Warren | d26bc49 | 2012-03-16 14:54:25 -0600 | [diff] [blame] | 1406 | goto out_err; |
| 1407 | } |
| 1408 | |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 1409 | /* If we're implementing pinmuxing, check the ops for sanity */ |
| 1410 | if (pctldesc->pmxops) { |
Devendra Naga | da9aecb | 2012-06-16 23:43:16 +0530 | [diff] [blame] | 1411 | if (pinmux_check_ops(pctldev)) |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 1412 | goto out_err; |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | /* If we're implementing pinconfig, check the ops for sanity */ |
| 1416 | if (pctldesc->confops) { |
Devendra Naga | da9aecb | 2012-06-16 23:43:16 +0530 | [diff] [blame] | 1417 | if (pinconf_check_ops(pctldev)) |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 1418 | goto out_err; |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 1419 | } |
| 1420 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1421 | /* Register all the pins */ |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 1422 | dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1423 | ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins); |
| 1424 | if (ret) { |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 1425 | dev_err(dev, "error during pin registration\n"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1426 | pinctrl_free_pindescs(pctldev, pctldesc->pins, |
| 1427 | pctldesc->npins); |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1428 | goto out_err; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1429 | } |
| 1430 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1431 | mutex_lock(&pinctrl_mutex); |
| 1432 | |
Stephen Warren | 8b9c139 | 2012-02-19 23:45:42 -0700 | [diff] [blame] | 1433 | list_add_tail(&pctldev->node, &pinctrldev_list); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1434 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1435 | pctldev->p = pinctrl_get_locked(pctldev->dev); |
| 1436 | if (!IS_ERR(pctldev->p)) { |
| 1437 | struct pinctrl_state *s = |
| 1438 | pinctrl_lookup_state_locked(pctldev->p, |
| 1439 | PINCTRL_STATE_DEFAULT); |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 1440 | if (IS_ERR(s)) { |
| 1441 | dev_dbg(dev, "failed to lookup the default state\n"); |
| 1442 | } else { |
Devendra Naga | da9aecb | 2012-06-16 23:43:16 +0530 | [diff] [blame] | 1443 | if (pinctrl_select_state_locked(pctldev->p, s)) |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 1444 | dev_err(dev, |
| 1445 | "failed to select default state\n"); |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 1446 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1447 | } |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1448 | |
| 1449 | mutex_unlock(&pinctrl_mutex); |
| 1450 | |
Stephen Warren | 2304b47 | 2012-02-22 14:26:01 -0700 | [diff] [blame] | 1451 | pinctrl_init_device_debugfs(pctldev); |
| 1452 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1453 | return pctldev; |
| 1454 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1455 | out_err: |
| 1456 | kfree(pctldev); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1457 | return NULL; |
| 1458 | } |
| 1459 | EXPORT_SYMBOL_GPL(pinctrl_register); |
| 1460 | |
| 1461 | /** |
| 1462 | * pinctrl_unregister() - unregister pinmux |
| 1463 | * @pctldev: pin controller to unregister |
| 1464 | * |
| 1465 | * Called by pinmux drivers to unregister a pinmux. |
| 1466 | */ |
| 1467 | void pinctrl_unregister(struct pinctrl_dev *pctldev) |
| 1468 | { |
Dong Aisheng | 5d589b0 | 2012-05-23 21:22:40 +0800 | [diff] [blame^] | 1469 | struct pinctrl_gpio_range *range, *n; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1470 | if (pctldev == NULL) |
| 1471 | return; |
| 1472 | |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1473 | pinctrl_remove_device_debugfs(pctldev); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1474 | |
| 1475 | mutex_lock(&pinctrl_mutex); |
| 1476 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1477 | if (!IS_ERR(pctldev->p)) |
| 1478 | pinctrl_put_locked(pctldev->p, true); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1479 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1480 | /* TODO: check that no pinmuxes are still active? */ |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1481 | list_del(&pctldev->node); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1482 | /* Destroy descriptor tree */ |
| 1483 | pinctrl_free_pindescs(pctldev, pctldev->desc->pins, |
| 1484 | pctldev->desc->npins); |
Dong Aisheng | 5d589b0 | 2012-05-23 21:22:40 +0800 | [diff] [blame^] | 1485 | /* remove gpio ranges map */ |
| 1486 | list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node) |
| 1487 | list_del(&range->node); |
| 1488 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1489 | kfree(pctldev); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1490 | |
| 1491 | mutex_unlock(&pinctrl_mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1492 | } |
| 1493 | EXPORT_SYMBOL_GPL(pinctrl_unregister); |
| 1494 | |
| 1495 | static int __init pinctrl_init(void) |
| 1496 | { |
| 1497 | pr_info("initialized pinctrl subsystem\n"); |
| 1498 | pinctrl_init_debugfs(); |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
| 1502 | /* init early since many drivers really need to initialized pinmux early */ |
| 1503 | core_initcall(pinctrl_init); |