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