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