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