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