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