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