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 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 566 | switch (map->type) { |
| 567 | case PIN_MAP_TYPE_MUX_GROUP: |
| 568 | ret = pinmux_map_to_setting(map, setting); |
| 569 | break; |
| 570 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 571 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 572 | ret = pinconf_map_to_setting(map, setting); |
| 573 | break; |
| 574 | default: |
| 575 | ret = -EINVAL; |
| 576 | break; |
| 577 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 578 | if (ret < 0) { |
| 579 | kfree(setting); |
| 580 | return ret; |
| 581 | } |
| 582 | |
| 583 | list_add_tail(&setting->node, &state->settings); |
| 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | static struct pinctrl *find_pinctrl(struct device *dev) |
| 589 | { |
| 590 | struct pinctrl *p; |
| 591 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 592 | list_for_each_entry(p, &pinctrl_list, node) |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 593 | if (p->dev == dev) |
| 594 | return p; |
| 595 | |
| 596 | return NULL; |
| 597 | } |
| 598 | |
| 599 | static void pinctrl_put_locked(struct pinctrl *p, bool inlist); |
| 600 | |
| 601 | static struct pinctrl *create_pinctrl(struct device *dev) |
| 602 | { |
| 603 | struct pinctrl *p; |
| 604 | const char *devname; |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 605 | struct pinctrl_maps *maps_node; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 606 | int i; |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 607 | struct pinctrl_map const *map; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 608 | int ret; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 609 | |
| 610 | /* |
| 611 | * create the state cookie holder struct pinctrl for each |
| 612 | * mapping, this is what consumers will get when requesting |
| 613 | * a pin control handle with pinctrl_get() |
| 614 | */ |
Stephen Warren | 02f5b98 | 2012-02-22 14:26:00 -0700 | [diff] [blame] | 615 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
Stephen Warren | 95dcd4a | 2012-02-22 14:25:59 -0700 | [diff] [blame] | 616 | if (p == NULL) { |
| 617 | dev_err(dev, "failed to alloc struct pinctrl\n"); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 618 | return ERR_PTR(-ENOMEM); |
Stephen Warren | 95dcd4a | 2012-02-22 14:25:59 -0700 | [diff] [blame] | 619 | } |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 620 | p->dev = dev; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 621 | INIT_LIST_HEAD(&p->states); |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 622 | INIT_LIST_HEAD(&p->dt_maps); |
| 623 | |
| 624 | ret = pinctrl_dt_to_map(p); |
| 625 | if (ret < 0) { |
| 626 | kfree(p); |
| 627 | return ERR_PTR(ret); |
| 628 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 629 | |
| 630 | devname = dev_name(dev); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 631 | |
| 632 | /* Iterate over the pin control maps to locate the right ones */ |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 633 | for_each_maps(maps_node, i, map) { |
Stephen Warren | 1681f5a | 2012-02-22 14:25:58 -0700 | [diff] [blame] | 634 | /* Map must be for this device */ |
| 635 | if (strcmp(map->dev_name, devname)) |
| 636 | continue; |
| 637 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 638 | ret = add_setting(p, map); |
| 639 | if (ret < 0) { |
| 640 | pinctrl_put_locked(p, false); |
| 641 | return ERR_PTR(ret); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 645 | /* Add the pinmux to the global list */ |
Stephen Warren | 8b9c139 | 2012-02-19 23:45:42 -0700 | [diff] [blame] | 646 | list_add_tail(&p->node, &pinctrl_list); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 647 | |
| 648 | return p; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 649 | } |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 650 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 651 | static struct pinctrl *pinctrl_get_locked(struct device *dev) |
| 652 | { |
| 653 | struct pinctrl *p; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 654 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 655 | if (WARN_ON(!dev)) |
| 656 | return ERR_PTR(-EINVAL); |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 657 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 658 | p = find_pinctrl(dev); |
| 659 | if (p != NULL) |
| 660 | return ERR_PTR(-EBUSY); |
| 661 | |
Richard Genoud | d599bfb | 2012-08-10 16:53:49 +0200 | [diff] [blame] | 662 | return create_pinctrl(dev); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 663 | } |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 664 | |
| 665 | /** |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 666 | * pinctrl_get() - retrieves the pinctrl handle for a device |
| 667 | * @dev: the device to obtain the handle for |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 668 | */ |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 669 | struct pinctrl *pinctrl_get(struct device *dev) |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 670 | { |
| 671 | struct pinctrl *p; |
| 672 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 673 | mutex_lock(&pinctrl_mutex); |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 674 | p = pinctrl_get_locked(dev); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 675 | mutex_unlock(&pinctrl_mutex); |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 676 | |
| 677 | return p; |
| 678 | } |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 679 | EXPORT_SYMBOL_GPL(pinctrl_get); |
| 680 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 681 | static void pinctrl_put_locked(struct pinctrl *p, bool inlist) |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 682 | { |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 683 | struct pinctrl_state *state, *n1; |
| 684 | struct pinctrl_setting *setting, *n2; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 685 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 686 | list_for_each_entry_safe(state, n1, &p->states, node) { |
| 687 | list_for_each_entry_safe(setting, n2, &state->settings, node) { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 688 | switch (setting->type) { |
| 689 | case PIN_MAP_TYPE_MUX_GROUP: |
| 690 | if (state == p->state) |
| 691 | pinmux_disable_setting(setting); |
| 692 | pinmux_free_setting(setting); |
| 693 | break; |
| 694 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 695 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 696 | pinconf_free_setting(setting); |
| 697 | break; |
| 698 | default: |
| 699 | break; |
| 700 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 701 | list_del(&setting->node); |
| 702 | kfree(setting); |
| 703 | } |
| 704 | list_del(&state->node); |
| 705 | kfree(state); |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 706 | } |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 707 | |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 708 | pinctrl_dt_free_maps(p); |
| 709 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 710 | if (inlist) |
| 711 | list_del(&p->node); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 712 | kfree(p); |
| 713 | } |
| 714 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 715 | /** |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 716 | * pinctrl_put() - release a previously claimed pinctrl handle |
| 717 | * @p: the pinctrl handle to release |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 718 | */ |
| 719 | void pinctrl_put(struct pinctrl *p) |
| 720 | { |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 721 | mutex_lock(&pinctrl_mutex); |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 722 | pinctrl_put_locked(p, true); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 723 | mutex_unlock(&pinctrl_mutex); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 724 | } |
| 725 | EXPORT_SYMBOL_GPL(pinctrl_put); |
| 726 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 727 | static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p, |
| 728 | const char *name) |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 729 | { |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 730 | struct pinctrl_state *state; |
| 731 | |
| 732 | state = find_state(p, name); |
Dong Aisheng | 5b3aa5f | 2012-04-26 16:15:50 +0800 | [diff] [blame] | 733 | if (!state) { |
| 734 | if (pinctrl_dummy_state) { |
| 735 | /* create dummy state */ |
| 736 | dev_dbg(p->dev, "using pinctrl dummy state (%s)\n", |
| 737 | name); |
| 738 | state = create_state(p, name); |
Richard Genoud | d599bfb | 2012-08-10 16:53:49 +0200 | [diff] [blame] | 739 | } else |
| 740 | state = ERR_PTR(-ENODEV); |
Dong Aisheng | 5b3aa5f | 2012-04-26 16:15:50 +0800 | [diff] [blame] | 741 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 742 | |
| 743 | return state; |
| 744 | } |
| 745 | |
| 746 | /** |
| 747 | * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle |
| 748 | * @p: the pinctrl handle to retrieve the state from |
| 749 | * @name: the state name to retrieve |
| 750 | */ |
| 751 | struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name) |
| 752 | { |
| 753 | struct pinctrl_state *s; |
| 754 | |
| 755 | mutex_lock(&pinctrl_mutex); |
| 756 | s = pinctrl_lookup_state_locked(p, name); |
| 757 | mutex_unlock(&pinctrl_mutex); |
| 758 | |
| 759 | return s; |
| 760 | } |
| 761 | EXPORT_SYMBOL_GPL(pinctrl_lookup_state); |
| 762 | |
| 763 | static int pinctrl_select_state_locked(struct pinctrl *p, |
| 764 | struct pinctrl_state *state) |
| 765 | { |
| 766 | struct pinctrl_setting *setting, *setting2; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 767 | int ret; |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 768 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 769 | if (p->state == state) |
| 770 | return 0; |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 771 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 772 | if (p->state) { |
| 773 | /* |
| 774 | * The set of groups with a mux configuration in the old state |
| 775 | * may not be identical to the set of groups with a mux setting |
| 776 | * in the new state. While this might be unusual, it's entirely |
| 777 | * possible for the "user"-supplied mapping table to be written |
| 778 | * that way. For each group that was configured in the old state |
| 779 | * but not in the new state, this code puts that group into a |
| 780 | * safe/disabled state. |
| 781 | */ |
| 782 | list_for_each_entry(setting, &p->state->settings, node) { |
| 783 | bool found = false; |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 784 | if (setting->type != PIN_MAP_TYPE_MUX_GROUP) |
| 785 | continue; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 786 | list_for_each_entry(setting2, &state->settings, node) { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 787 | if (setting2->type != PIN_MAP_TYPE_MUX_GROUP) |
| 788 | continue; |
| 789 | if (setting2->data.mux.group == |
| 790 | setting->data.mux.group) { |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 791 | found = true; |
| 792 | break; |
| 793 | } |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 794 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 795 | if (!found) |
| 796 | pinmux_disable_setting(setting); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | p->state = state; |
| 801 | |
| 802 | /* Apply all the settings for the new state */ |
| 803 | list_for_each_entry(setting, &state->settings, node) { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 804 | switch (setting->type) { |
| 805 | case PIN_MAP_TYPE_MUX_GROUP: |
| 806 | ret = pinmux_enable_setting(setting); |
| 807 | break; |
| 808 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 809 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 810 | ret = pinconf_apply_setting(setting); |
| 811 | break; |
| 812 | default: |
| 813 | ret = -EINVAL; |
| 814 | break; |
| 815 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 816 | if (ret < 0) { |
| 817 | /* FIXME: Difficult to return to prev state */ |
| 818 | return ret; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 819 | } |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 822 | return 0; |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 825 | /** |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 826 | * pinctrl_select() - select/activate/program a pinctrl state to HW |
| 827 | * @p: the pinctrl handle for the device that requests configuratio |
| 828 | * @state: the state handle to select/activate/program |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 829 | */ |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 830 | int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state) |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 831 | { |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 832 | int ret; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 833 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 834 | mutex_lock(&pinctrl_mutex); |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 835 | ret = pinctrl_select_state_locked(p, state); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 836 | mutex_unlock(&pinctrl_mutex); |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 837 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 838 | return ret; |
| 839 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 840 | EXPORT_SYMBOL_GPL(pinctrl_select_state); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 841 | |
Stephen Warren | 6d4ca1f | 2012-04-16 10:51:00 -0600 | [diff] [blame] | 842 | static void devm_pinctrl_release(struct device *dev, void *res) |
| 843 | { |
| 844 | pinctrl_put(*(struct pinctrl **)res); |
| 845 | } |
| 846 | |
| 847 | /** |
| 848 | * struct devm_pinctrl_get() - Resource managed pinctrl_get() |
| 849 | * @dev: the device to obtain the handle for |
| 850 | * |
| 851 | * If there is a need to explicitly destroy the returned struct pinctrl, |
| 852 | * devm_pinctrl_put() should be used, rather than plain pinctrl_put(). |
| 853 | */ |
| 854 | struct pinctrl *devm_pinctrl_get(struct device *dev) |
| 855 | { |
| 856 | struct pinctrl **ptr, *p; |
| 857 | |
| 858 | ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL); |
| 859 | if (!ptr) |
| 860 | return ERR_PTR(-ENOMEM); |
| 861 | |
| 862 | p = pinctrl_get(dev); |
| 863 | if (!IS_ERR(p)) { |
| 864 | *ptr = p; |
| 865 | devres_add(dev, ptr); |
| 866 | } else { |
| 867 | devres_free(ptr); |
| 868 | } |
| 869 | |
| 870 | return p; |
| 871 | } |
| 872 | EXPORT_SYMBOL_GPL(devm_pinctrl_get); |
| 873 | |
| 874 | static int devm_pinctrl_match(struct device *dev, void *res, void *data) |
| 875 | { |
| 876 | struct pinctrl **p = res; |
| 877 | |
| 878 | return *p == data; |
| 879 | } |
| 880 | |
| 881 | /** |
| 882 | * devm_pinctrl_put() - Resource managed pinctrl_put() |
| 883 | * @p: the pinctrl handle to release |
| 884 | * |
| 885 | * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally |
| 886 | * this function will not need to be called and the resource management |
| 887 | * code will ensure that the resource is freed. |
| 888 | */ |
| 889 | void devm_pinctrl_put(struct pinctrl *p) |
| 890 | { |
| 891 | WARN_ON(devres_destroy(p->dev, devm_pinctrl_release, |
| 892 | devm_pinctrl_match, p)); |
| 893 | pinctrl_put(p); |
| 894 | } |
| 895 | EXPORT_SYMBOL_GPL(devm_pinctrl_put); |
| 896 | |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 897 | int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps, |
| 898 | bool dup, bool locked) |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 899 | { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 900 | int i, ret; |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 901 | struct pinctrl_maps *maps_node; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 902 | |
| 903 | pr_debug("add %d pinmux maps\n", num_maps); |
| 904 | |
| 905 | /* First sanity check the new mapping */ |
| 906 | for (i = 0; i < num_maps; i++) { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 907 | if (!maps[i].dev_name) { |
| 908 | pr_err("failed to register map %s (%d): no device given\n", |
| 909 | maps[i].name, i); |
| 910 | return -EINVAL; |
| 911 | } |
| 912 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 913 | if (!maps[i].name) { |
| 914 | pr_err("failed to register map %d: no map name given\n", |
Stephen Warren | 95dcd4a | 2012-02-22 14:25:59 -0700 | [diff] [blame] | 915 | i); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 916 | return -EINVAL; |
| 917 | } |
| 918 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 919 | if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE && |
| 920 | !maps[i].ctrl_dev_name) { |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 921 | pr_err("failed to register map %s (%d): no pin control device given\n", |
| 922 | maps[i].name, i); |
| 923 | return -EINVAL; |
| 924 | } |
| 925 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 926 | switch (maps[i].type) { |
| 927 | case PIN_MAP_TYPE_DUMMY_STATE: |
| 928 | break; |
| 929 | case PIN_MAP_TYPE_MUX_GROUP: |
| 930 | ret = pinmux_validate_map(&maps[i], i); |
| 931 | if (ret < 0) |
Stephen Warren | fde04f4 | 2012-04-25 10:32:16 -0600 | [diff] [blame] | 932 | return ret; |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 933 | break; |
| 934 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 935 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 936 | ret = pinconf_validate_map(&maps[i], i); |
| 937 | if (ret < 0) |
Stephen Warren | fde04f4 | 2012-04-25 10:32:16 -0600 | [diff] [blame] | 938 | return ret; |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 939 | break; |
| 940 | default: |
| 941 | pr_err("failed to register map %s (%d): invalid type given\n", |
Stephen Warren | 95dcd4a | 2012-02-22 14:25:59 -0700 | [diff] [blame] | 942 | maps[i].name, i); |
Stephen Warren | 1681f5a | 2012-02-22 14:25:58 -0700 | [diff] [blame] | 943 | return -EINVAL; |
| 944 | } |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 945 | } |
| 946 | |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 947 | maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL); |
| 948 | if (!maps_node) { |
| 949 | pr_err("failed to alloc struct pinctrl_maps\n"); |
| 950 | return -ENOMEM; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 951 | } |
| 952 | |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 953 | maps_node->num_maps = num_maps; |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 954 | if (dup) { |
| 955 | maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps, |
| 956 | GFP_KERNEL); |
| 957 | if (!maps_node->maps) { |
| 958 | pr_err("failed to duplicate mapping table\n"); |
| 959 | kfree(maps_node); |
| 960 | return -ENOMEM; |
| 961 | } |
| 962 | } else { |
| 963 | maps_node->maps = maps; |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 966 | if (!locked) |
| 967 | mutex_lock(&pinctrl_mutex); |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 968 | list_add_tail(&maps_node->node, &pinctrl_maps); |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 969 | if (!locked) |
| 970 | mutex_unlock(&pinctrl_mutex); |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 971 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 972 | return 0; |
| 973 | } |
| 974 | |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 975 | /** |
| 976 | * pinctrl_register_mappings() - register a set of pin controller mappings |
| 977 | * @maps: the pincontrol mappings table to register. This should probably be |
| 978 | * marked with __initdata so it can be discarded after boot. This |
| 979 | * function will perform a shallow copy for the mapping entries. |
| 980 | * @num_maps: the number of maps in the mapping table |
| 981 | */ |
| 982 | int pinctrl_register_mappings(struct pinctrl_map const *maps, |
| 983 | unsigned num_maps) |
| 984 | { |
| 985 | return pinctrl_register_map(maps, num_maps, true, false); |
| 986 | } |
| 987 | |
| 988 | void pinctrl_unregister_map(struct pinctrl_map const *map) |
| 989 | { |
| 990 | struct pinctrl_maps *maps_node; |
| 991 | |
| 992 | list_for_each_entry(maps_node, &pinctrl_maps, node) { |
| 993 | if (maps_node->maps == map) { |
| 994 | list_del(&maps_node->node); |
| 995 | return; |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1000 | #ifdef CONFIG_DEBUG_FS |
| 1001 | |
| 1002 | static int pinctrl_pins_show(struct seq_file *s, void *what) |
| 1003 | { |
| 1004 | struct pinctrl_dev *pctldev = s->private; |
| 1005 | const struct pinctrl_ops *ops = pctldev->desc->pctlops; |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 1006 | unsigned i, pin; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1007 | |
| 1008 | seq_printf(s, "registered pins: %d\n", pctldev->desc->npins); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1009 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1010 | mutex_lock(&pinctrl_mutex); |
| 1011 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 1012 | /* The pin number can be retrived from the pin controller descriptor */ |
| 1013 | for (i = 0; i < pctldev->desc->npins; i++) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1014 | struct pin_desc *desc; |
| 1015 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 1016 | pin = pctldev->desc->pins[i].number; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1017 | desc = pin_desc_get(pctldev, pin); |
| 1018 | /* Pin space may be sparse */ |
| 1019 | if (desc == NULL) |
| 1020 | continue; |
| 1021 | |
| 1022 | seq_printf(s, "pin %d (%s) ", pin, |
| 1023 | desc->name ? desc->name : "unnamed"); |
| 1024 | |
| 1025 | /* Driver-specific info per pin */ |
| 1026 | if (ops->pin_dbg_show) |
| 1027 | ops->pin_dbg_show(pctldev, s, pin); |
| 1028 | |
| 1029 | seq_puts(s, "\n"); |
| 1030 | } |
| 1031 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1032 | mutex_unlock(&pinctrl_mutex); |
| 1033 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1034 | return 0; |
| 1035 | } |
| 1036 | |
| 1037 | static int pinctrl_groups_show(struct seq_file *s, void *what) |
| 1038 | { |
| 1039 | struct pinctrl_dev *pctldev = s->private; |
| 1040 | const struct pinctrl_ops *ops = pctldev->desc->pctlops; |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 1041 | unsigned ngroups, selector = 0; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1042 | |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 1043 | ngroups = ops->get_groups_count(pctldev); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1044 | mutex_lock(&pinctrl_mutex); |
| 1045 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1046 | seq_puts(s, "registered pin groups:\n"); |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 1047 | while (selector < ngroups) { |
Stephen Warren | a5818a8 | 2011-10-19 16:19:25 -0600 | [diff] [blame] | 1048 | const unsigned *pins; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1049 | unsigned num_pins; |
| 1050 | const char *gname = ops->get_group_name(pctldev, selector); |
Dong Aisheng | dcb5dbc | 2012-04-17 15:00:46 +0800 | [diff] [blame] | 1051 | const char *pname; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1052 | int ret; |
| 1053 | int i; |
| 1054 | |
| 1055 | ret = ops->get_group_pins(pctldev, selector, |
| 1056 | &pins, &num_pins); |
| 1057 | if (ret) |
| 1058 | seq_printf(s, "%s [ERROR GETTING PINS]\n", |
| 1059 | gname); |
| 1060 | else { |
Dong Aisheng | dcb5dbc | 2012-04-17 15:00:46 +0800 | [diff] [blame] | 1061 | seq_printf(s, "group: %s\n", gname); |
| 1062 | for (i = 0; i < num_pins; i++) { |
| 1063 | pname = pin_get_name(pctldev, pins[i]); |
Wei Yongjun | b4dd784 | 2012-10-22 12:58:09 +0800 | [diff] [blame] | 1064 | if (WARN_ON(!pname)) { |
| 1065 | mutex_unlock(&pinctrl_mutex); |
Dong Aisheng | dcb5dbc | 2012-04-17 15:00:46 +0800 | [diff] [blame] | 1066 | return -EINVAL; |
Wei Yongjun | b4dd784 | 2012-10-22 12:58:09 +0800 | [diff] [blame] | 1067 | } |
Dong Aisheng | dcb5dbc | 2012-04-17 15:00:46 +0800 | [diff] [blame] | 1068 | seq_printf(s, "pin %d (%s)\n", pins[i], pname); |
| 1069 | } |
| 1070 | seq_puts(s, "\n"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1071 | } |
| 1072 | selector++; |
| 1073 | } |
| 1074 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1075 | mutex_unlock(&pinctrl_mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1076 | |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
| 1080 | static int pinctrl_gpioranges_show(struct seq_file *s, void *what) |
| 1081 | { |
| 1082 | struct pinctrl_dev *pctldev = s->private; |
| 1083 | struct pinctrl_gpio_range *range = NULL; |
| 1084 | |
| 1085 | seq_puts(s, "GPIO ranges handled:\n"); |
| 1086 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1087 | mutex_lock(&pinctrl_mutex); |
| 1088 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1089 | /* Loop over the ranges */ |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1090 | list_for_each_entry(range, &pctldev->gpio_ranges, node) { |
Linus Walleij | 75d6642 | 2011-11-16 09:58:51 +0100 | [diff] [blame] | 1091 | seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n", |
| 1092 | range->id, range->name, |
| 1093 | range->base, (range->base + range->npins - 1), |
| 1094 | range->pin_base, |
| 1095 | (range->pin_base + range->npins - 1)); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1096 | } |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1097 | |
| 1098 | mutex_unlock(&pinctrl_mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1099 | |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | static int pinctrl_devices_show(struct seq_file *s, void *what) |
| 1104 | { |
| 1105 | struct pinctrl_dev *pctldev; |
| 1106 | |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 1107 | seq_puts(s, "name [pinmux] [pinconf]\n"); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1108 | |
| 1109 | mutex_lock(&pinctrl_mutex); |
| 1110 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1111 | list_for_each_entry(pctldev, &pinctrldev_list, node) { |
| 1112 | seq_printf(s, "%s ", pctldev->desc->name); |
| 1113 | if (pctldev->desc->pmxops) |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 1114 | seq_puts(s, "yes "); |
| 1115 | else |
| 1116 | seq_puts(s, "no "); |
| 1117 | if (pctldev->desc->confops) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1118 | seq_puts(s, "yes"); |
| 1119 | else |
| 1120 | seq_puts(s, "no"); |
| 1121 | seq_puts(s, "\n"); |
| 1122 | } |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1123 | |
| 1124 | mutex_unlock(&pinctrl_mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1125 | |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 1129 | static inline const char *map_type(enum pinctrl_map_type type) |
| 1130 | { |
| 1131 | static const char * const names[] = { |
| 1132 | "INVALID", |
| 1133 | "DUMMY_STATE", |
| 1134 | "MUX_GROUP", |
| 1135 | "CONFIGS_PIN", |
| 1136 | "CONFIGS_GROUP", |
| 1137 | }; |
| 1138 | |
| 1139 | if (type >= ARRAY_SIZE(names)) |
| 1140 | return "UNKNOWN"; |
| 1141 | |
| 1142 | return names[type]; |
| 1143 | } |
| 1144 | |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1145 | static int pinctrl_maps_show(struct seq_file *s, void *what) |
| 1146 | { |
| 1147 | struct pinctrl_maps *maps_node; |
| 1148 | int i; |
| 1149 | struct pinctrl_map const *map; |
| 1150 | |
| 1151 | seq_puts(s, "Pinctrl maps:\n"); |
| 1152 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1153 | mutex_lock(&pinctrl_mutex); |
| 1154 | |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1155 | for_each_maps(maps_node, i, map) { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 1156 | seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n", |
| 1157 | map->dev_name, map->name, map_type(map->type), |
| 1158 | map->type); |
| 1159 | |
| 1160 | if (map->type != PIN_MAP_TYPE_DUMMY_STATE) |
| 1161 | seq_printf(s, "controlling device %s\n", |
| 1162 | map->ctrl_dev_name); |
| 1163 | |
| 1164 | switch (map->type) { |
| 1165 | case PIN_MAP_TYPE_MUX_GROUP: |
| 1166 | pinmux_show_map(s, map); |
| 1167 | break; |
| 1168 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 1169 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 1170 | pinconf_show_map(s, map); |
| 1171 | break; |
| 1172 | default: |
| 1173 | break; |
| 1174 | } |
| 1175 | |
| 1176 | seq_printf(s, "\n"); |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1177 | } |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1178 | |
| 1179 | mutex_unlock(&pinctrl_mutex); |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1180 | |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1184 | static int pinctrl_show(struct seq_file *s, void *what) |
| 1185 | { |
| 1186 | struct pinctrl *p; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1187 | struct pinctrl_state *state; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 1188 | struct pinctrl_setting *setting; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1189 | |
| 1190 | seq_puts(s, "Requested pin control handlers their pinmux maps:\n"); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1191 | |
| 1192 | mutex_lock(&pinctrl_mutex); |
| 1193 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1194 | list_for_each_entry(p, &pinctrl_list, node) { |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1195 | seq_printf(s, "device: %s current state: %s\n", |
| 1196 | dev_name(p->dev), |
| 1197 | p->state ? p->state->name : "none"); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1198 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1199 | list_for_each_entry(state, &p->states, node) { |
| 1200 | seq_printf(s, " state: %s\n", state->name); |
| 1201 | |
| 1202 | list_for_each_entry(setting, &state->settings, node) { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 1203 | struct pinctrl_dev *pctldev = setting->pctldev; |
| 1204 | |
| 1205 | seq_printf(s, " type: %s controller %s ", |
| 1206 | map_type(setting->type), |
| 1207 | pinctrl_dev_get_name(pctldev)); |
| 1208 | |
| 1209 | switch (setting->type) { |
| 1210 | case PIN_MAP_TYPE_MUX_GROUP: |
| 1211 | pinmux_show_setting(s, setting); |
| 1212 | break; |
| 1213 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 1214 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 1215 | pinconf_show_setting(s, setting); |
| 1216 | break; |
| 1217 | default: |
| 1218 | break; |
| 1219 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1220 | } |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1221 | } |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1222 | } |
| 1223 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1224 | mutex_unlock(&pinctrl_mutex); |
| 1225 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1226 | return 0; |
| 1227 | } |
| 1228 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1229 | static int pinctrl_pins_open(struct inode *inode, struct file *file) |
| 1230 | { |
| 1231 | return single_open(file, pinctrl_pins_show, inode->i_private); |
| 1232 | } |
| 1233 | |
| 1234 | static int pinctrl_groups_open(struct inode *inode, struct file *file) |
| 1235 | { |
| 1236 | return single_open(file, pinctrl_groups_show, inode->i_private); |
| 1237 | } |
| 1238 | |
| 1239 | static int pinctrl_gpioranges_open(struct inode *inode, struct file *file) |
| 1240 | { |
| 1241 | return single_open(file, pinctrl_gpioranges_show, inode->i_private); |
| 1242 | } |
| 1243 | |
| 1244 | static int pinctrl_devices_open(struct inode *inode, struct file *file) |
| 1245 | { |
| 1246 | return single_open(file, pinctrl_devices_show, NULL); |
| 1247 | } |
| 1248 | |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1249 | static int pinctrl_maps_open(struct inode *inode, struct file *file) |
| 1250 | { |
| 1251 | return single_open(file, pinctrl_maps_show, NULL); |
| 1252 | } |
| 1253 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1254 | static int pinctrl_open(struct inode *inode, struct file *file) |
| 1255 | { |
| 1256 | return single_open(file, pinctrl_show, NULL); |
| 1257 | } |
| 1258 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1259 | static const struct file_operations pinctrl_pins_ops = { |
| 1260 | .open = pinctrl_pins_open, |
| 1261 | .read = seq_read, |
| 1262 | .llseek = seq_lseek, |
| 1263 | .release = single_release, |
| 1264 | }; |
| 1265 | |
| 1266 | static const struct file_operations pinctrl_groups_ops = { |
| 1267 | .open = pinctrl_groups_open, |
| 1268 | .read = seq_read, |
| 1269 | .llseek = seq_lseek, |
| 1270 | .release = single_release, |
| 1271 | }; |
| 1272 | |
| 1273 | static const struct file_operations pinctrl_gpioranges_ops = { |
| 1274 | .open = pinctrl_gpioranges_open, |
| 1275 | .read = seq_read, |
| 1276 | .llseek = seq_lseek, |
| 1277 | .release = single_release, |
| 1278 | }; |
| 1279 | |
| 1280 | static const struct file_operations pinctrl_devices_ops = { |
| 1281 | .open = pinctrl_devices_open, |
| 1282 | .read = seq_read, |
| 1283 | .llseek = seq_lseek, |
| 1284 | .release = single_release, |
| 1285 | }; |
| 1286 | |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1287 | static const struct file_operations pinctrl_maps_ops = { |
| 1288 | .open = pinctrl_maps_open, |
| 1289 | .read = seq_read, |
| 1290 | .llseek = seq_lseek, |
| 1291 | .release = single_release, |
| 1292 | }; |
| 1293 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1294 | static const struct file_operations pinctrl_ops = { |
| 1295 | .open = pinctrl_open, |
| 1296 | .read = seq_read, |
| 1297 | .llseek = seq_lseek, |
| 1298 | .release = single_release, |
| 1299 | }; |
| 1300 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1301 | static struct dentry *debugfs_root; |
| 1302 | |
| 1303 | static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) |
| 1304 | { |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1305 | struct dentry *device_root; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1306 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1307 | device_root = debugfs_create_dir(dev_name(pctldev->dev), |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1308 | debugfs_root); |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1309 | pctldev->device_root = device_root; |
| 1310 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1311 | if (IS_ERR(device_root) || !device_root) { |
| 1312 | pr_warn("failed to create debugfs directory for %s\n", |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1313 | dev_name(pctldev->dev)); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1314 | return; |
| 1315 | } |
| 1316 | debugfs_create_file("pins", S_IFREG | S_IRUGO, |
| 1317 | device_root, pctldev, &pinctrl_pins_ops); |
| 1318 | debugfs_create_file("pingroups", S_IFREG | S_IRUGO, |
| 1319 | device_root, pctldev, &pinctrl_groups_ops); |
| 1320 | debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO, |
| 1321 | device_root, pctldev, &pinctrl_gpioranges_ops); |
| 1322 | pinmux_init_device_debugfs(device_root, pctldev); |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 1323 | pinconf_init_device_debugfs(device_root, pctldev); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1324 | } |
| 1325 | |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1326 | static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev) |
| 1327 | { |
| 1328 | debugfs_remove_recursive(pctldev->device_root); |
| 1329 | } |
| 1330 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1331 | static void pinctrl_init_debugfs(void) |
| 1332 | { |
| 1333 | debugfs_root = debugfs_create_dir("pinctrl", NULL); |
| 1334 | if (IS_ERR(debugfs_root) || !debugfs_root) { |
| 1335 | pr_warn("failed to create debugfs directory\n"); |
| 1336 | debugfs_root = NULL; |
| 1337 | return; |
| 1338 | } |
| 1339 | |
| 1340 | debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO, |
| 1341 | debugfs_root, NULL, &pinctrl_devices_ops); |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1342 | debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO, |
| 1343 | debugfs_root, NULL, &pinctrl_maps_ops); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1344 | debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO, |
| 1345 | debugfs_root, NULL, &pinctrl_ops); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | #else /* CONFIG_DEBUG_FS */ |
| 1349 | |
| 1350 | static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) |
| 1351 | { |
| 1352 | } |
| 1353 | |
| 1354 | static void pinctrl_init_debugfs(void) |
| 1355 | { |
| 1356 | } |
| 1357 | |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1358 | static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev) |
| 1359 | { |
| 1360 | } |
| 1361 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1362 | #endif |
| 1363 | |
Stephen Warren | d26bc49 | 2012-03-16 14:54:25 -0600 | [diff] [blame] | 1364 | static int pinctrl_check_ops(struct pinctrl_dev *pctldev) |
| 1365 | { |
| 1366 | const struct pinctrl_ops *ops = pctldev->desc->pctlops; |
| 1367 | |
| 1368 | if (!ops || |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 1369 | !ops->get_groups_count || |
Stephen Warren | d26bc49 | 2012-03-16 14:54:25 -0600 | [diff] [blame] | 1370 | !ops->get_group_name || |
| 1371 | !ops->get_group_pins) |
| 1372 | return -EINVAL; |
| 1373 | |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 1374 | if (ops->dt_node_to_map && !ops->dt_free_map) |
| 1375 | return -EINVAL; |
| 1376 | |
Stephen Warren | d26bc49 | 2012-03-16 14:54:25 -0600 | [diff] [blame] | 1377 | return 0; |
| 1378 | } |
| 1379 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1380 | /** |
| 1381 | * pinctrl_register() - register a pin controller device |
| 1382 | * @pctldesc: descriptor for this pin controller |
| 1383 | * @dev: parent device for this pin controller |
| 1384 | * @driver_data: private pin controller data for this pin controller |
| 1385 | */ |
| 1386 | struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc, |
| 1387 | struct device *dev, void *driver_data) |
| 1388 | { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1389 | struct pinctrl_dev *pctldev; |
| 1390 | int ret; |
| 1391 | |
Devendra Naga | da9aecb | 2012-06-16 23:43:16 +0530 | [diff] [blame] | 1392 | if (!pctldesc) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1393 | return NULL; |
Devendra Naga | da9aecb | 2012-06-16 23:43:16 +0530 | [diff] [blame] | 1394 | if (!pctldesc->name) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1395 | return NULL; |
| 1396 | |
Stephen Warren | 02f5b98 | 2012-02-22 14:26:00 -0700 | [diff] [blame] | 1397 | pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL); |
Stephen Warren | 95dcd4a | 2012-02-22 14:25:59 -0700 | [diff] [blame] | 1398 | if (pctldev == NULL) { |
| 1399 | dev_err(dev, "failed to alloc struct pinctrl_dev\n"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1400 | return NULL; |
Stephen Warren | 95dcd4a | 2012-02-22 14:25:59 -0700 | [diff] [blame] | 1401 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1402 | |
| 1403 | /* Initialize pin control device struct */ |
| 1404 | pctldev->owner = pctldesc->owner; |
| 1405 | pctldev->desc = pctldesc; |
| 1406 | pctldev->driver_data = driver_data; |
| 1407 | INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1408 | INIT_LIST_HEAD(&pctldev->gpio_ranges); |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1409 | pctldev->dev = dev; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1410 | |
Stephen Warren | d26bc49 | 2012-03-16 14:54:25 -0600 | [diff] [blame] | 1411 | /* check core ops for sanity */ |
Devendra Naga | da9aecb | 2012-06-16 23:43:16 +0530 | [diff] [blame] | 1412 | if (pinctrl_check_ops(pctldev)) { |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 1413 | dev_err(dev, "pinctrl ops lacks necessary functions\n"); |
Stephen Warren | d26bc49 | 2012-03-16 14:54:25 -0600 | [diff] [blame] | 1414 | goto out_err; |
| 1415 | } |
| 1416 | |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 1417 | /* If we're implementing pinmuxing, check the ops for sanity */ |
| 1418 | if (pctldesc->pmxops) { |
Devendra Naga | da9aecb | 2012-06-16 23:43:16 +0530 | [diff] [blame] | 1419 | if (pinmux_check_ops(pctldev)) |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 1420 | goto out_err; |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | /* If we're implementing pinconfig, check the ops for sanity */ |
| 1424 | if (pctldesc->confops) { |
Devendra Naga | da9aecb | 2012-06-16 23:43:16 +0530 | [diff] [blame] | 1425 | if (pinconf_check_ops(pctldev)) |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 1426 | goto out_err; |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 1427 | } |
| 1428 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1429 | /* Register all the pins */ |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 1430 | dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1431 | ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins); |
| 1432 | if (ret) { |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 1433 | dev_err(dev, "error during pin registration\n"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1434 | pinctrl_free_pindescs(pctldev, pctldesc->pins, |
| 1435 | pctldesc->npins); |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1436 | goto out_err; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1437 | } |
| 1438 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1439 | mutex_lock(&pinctrl_mutex); |
| 1440 | |
Stephen Warren | 8b9c139 | 2012-02-19 23:45:42 -0700 | [diff] [blame] | 1441 | list_add_tail(&pctldev->node, &pinctrldev_list); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1442 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1443 | pctldev->p = pinctrl_get_locked(pctldev->dev); |
| 1444 | if (!IS_ERR(pctldev->p)) { |
| 1445 | struct pinctrl_state *s = |
| 1446 | pinctrl_lookup_state_locked(pctldev->p, |
| 1447 | PINCTRL_STATE_DEFAULT); |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 1448 | if (IS_ERR(s)) { |
| 1449 | dev_dbg(dev, "failed to lookup the default state\n"); |
| 1450 | } else { |
Devendra Naga | da9aecb | 2012-06-16 23:43:16 +0530 | [diff] [blame] | 1451 | if (pinctrl_select_state_locked(pctldev->p, s)) |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 1452 | dev_err(dev, |
| 1453 | "failed to select default state\n"); |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 1454 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1455 | } |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1456 | |
| 1457 | mutex_unlock(&pinctrl_mutex); |
| 1458 | |
Stephen Warren | 2304b47 | 2012-02-22 14:26:01 -0700 | [diff] [blame] | 1459 | pinctrl_init_device_debugfs(pctldev); |
| 1460 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1461 | return pctldev; |
| 1462 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1463 | out_err: |
| 1464 | kfree(pctldev); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1465 | return NULL; |
| 1466 | } |
| 1467 | EXPORT_SYMBOL_GPL(pinctrl_register); |
| 1468 | |
| 1469 | /** |
| 1470 | * pinctrl_unregister() - unregister pinmux |
| 1471 | * @pctldev: pin controller to unregister |
| 1472 | * |
| 1473 | * Called by pinmux drivers to unregister a pinmux. |
| 1474 | */ |
| 1475 | void pinctrl_unregister(struct pinctrl_dev *pctldev) |
| 1476 | { |
Dong Aisheng | 5d589b0 | 2012-05-23 21:22:40 +0800 | [diff] [blame] | 1477 | struct pinctrl_gpio_range *range, *n; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1478 | if (pctldev == NULL) |
| 1479 | return; |
| 1480 | |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1481 | pinctrl_remove_device_debugfs(pctldev); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1482 | |
| 1483 | mutex_lock(&pinctrl_mutex); |
| 1484 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1485 | if (!IS_ERR(pctldev->p)) |
| 1486 | pinctrl_put_locked(pctldev->p, true); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1487 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1488 | /* TODO: check that no pinmuxes are still active? */ |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1489 | list_del(&pctldev->node); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1490 | /* Destroy descriptor tree */ |
| 1491 | pinctrl_free_pindescs(pctldev, pctldev->desc->pins, |
| 1492 | pctldev->desc->npins); |
Dong Aisheng | 5d589b0 | 2012-05-23 21:22:40 +0800 | [diff] [blame] | 1493 | /* remove gpio ranges map */ |
| 1494 | list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node) |
| 1495 | list_del(&range->node); |
| 1496 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1497 | kfree(pctldev); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1498 | |
| 1499 | mutex_unlock(&pinctrl_mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1500 | } |
| 1501 | EXPORT_SYMBOL_GPL(pinctrl_unregister); |
| 1502 | |
| 1503 | static int __init pinctrl_init(void) |
| 1504 | { |
| 1505 | pr_info("initialized pinctrl subsystem\n"); |
| 1506 | pinctrl_init_debugfs(); |
| 1507 | return 0; |
| 1508 | } |
| 1509 | |
| 1510 | /* init early since many drivers really need to initialized pinmux early */ |
| 1511 | core_initcall(pinctrl_init); |