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, |
Masahiro Yamada | cd8f61f | 2016-05-25 14:09:31 +0900 | [diff] [blame] | 228 | const struct pinctrl_pin_desc *pin) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 229 | { |
| 230 | struct pin_desc *pindesc; |
| 231 | |
Masahiro Yamada | cd8f61f | 2016-05-25 14:09:31 +0900 | [diff] [blame] | 232 | pindesc = pin_desc_get(pctldev, pin->number); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 233 | if (pindesc != NULL) { |
Masahiro Yamada | cd8f61f | 2016-05-25 14:09:31 +0900 | [diff] [blame] | 234 | dev_err(pctldev->dev, "pin %d already registered\n", |
| 235 | pin->number); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 236 | return -EINVAL; |
| 237 | } |
| 238 | |
| 239 | pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL); |
Bjorn Andersson | 2104d12 | 2017-01-05 08:07:55 -0800 | [diff] [blame] | 240 | if (!pindesc) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 241 | return -ENOMEM; |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 242 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 243 | /* Set owner */ |
| 244 | pindesc->pctldev = pctldev; |
| 245 | |
Stephen Warren | 9af1e44 | 2011-10-19 16:19:27 -0600 | [diff] [blame] | 246 | /* Copy basic pin info */ |
Masahiro Yamada | cd8f61f | 2016-05-25 14:09:31 +0900 | [diff] [blame] | 247 | if (pin->name) { |
| 248 | pindesc->name = pin->name; |
Linus Walleij | ca53c5f | 2011-12-14 20:33:37 +0100 | [diff] [blame] | 249 | } else { |
Masahiro Yamada | cd8f61f | 2016-05-25 14:09:31 +0900 | [diff] [blame] | 250 | pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number); |
Sachin Kamat | eb26cc9 | 2012-09-25 12:41:40 +0530 | [diff] [blame] | 251 | if (pindesc->name == NULL) { |
| 252 | kfree(pindesc); |
Linus Walleij | ca53c5f | 2011-12-14 20:33:37 +0100 | [diff] [blame] | 253 | return -ENOMEM; |
Sachin Kamat | eb26cc9 | 2012-09-25 12:41:40 +0530 | [diff] [blame] | 254 | } |
Linus Walleij | ca53c5f | 2011-12-14 20:33:37 +0100 | [diff] [blame] | 255 | pindesc->dynamic_name = true; |
| 256 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 257 | |
Masahiro Yamada | cd8f61f | 2016-05-25 14:09:31 +0900 | [diff] [blame] | 258 | pindesc->drv_data = pin->drv_data; |
| 259 | |
| 260 | radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 261 | pr_debug("registered pin %d (%s) on %s\n", |
Masahiro Yamada | cd8f61f | 2016-05-25 14:09:31 +0900 | [diff] [blame] | 262 | pin->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++) { |
Masahiro Yamada | cd8f61f | 2016-05-25 14:09:31 +0900 | [diff] [blame] | 274 | ret = pinctrl_register_one_pin(pctldev, &pins[i]); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 275 | if (ret) |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | /** |
Christian Ruppert | c8587ee | 2013-06-13 14:55:31 +0200 | [diff] [blame] | 283 | * gpio_to_pin() - GPIO range GPIO number to pin number translation |
| 284 | * @range: GPIO range used for the translation |
| 285 | * @gpio: gpio pin to translate to a pin number |
| 286 | * |
| 287 | * Finds the pin number for a given GPIO using the specified GPIO range |
| 288 | * as a base for translation. The distinction between linear GPIO ranges |
| 289 | * and pin list based GPIO ranges is managed correctly by this function. |
| 290 | * |
| 291 | * This function assumes the gpio is part of the specified GPIO range, use |
| 292 | * only after making sure this is the case (e.g. by calling it on the |
| 293 | * result of successful pinctrl_get_device_gpio_range calls)! |
| 294 | */ |
| 295 | static inline int gpio_to_pin(struct pinctrl_gpio_range *range, |
| 296 | unsigned int gpio) |
| 297 | { |
| 298 | unsigned int offset = gpio - range->base; |
| 299 | if (range->pins) |
| 300 | return range->pins[offset]; |
| 301 | else |
| 302 | return range->pin_base + offset; |
| 303 | } |
| 304 | |
| 305 | /** |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 306 | * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range |
| 307 | * @pctldev: pin controller device to check |
| 308 | * @gpio: gpio pin to check taken from the global GPIO pin space |
| 309 | * |
| 310 | * Tries to match a GPIO pin number to the ranges handled by a certain pin |
| 311 | * controller, return the range or NULL |
| 312 | */ |
| 313 | static struct pinctrl_gpio_range * |
| 314 | pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio) |
| 315 | { |
| 316 | struct pinctrl_gpio_range *range = NULL; |
| 317 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 318 | mutex_lock(&pctldev->mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 319 | /* Loop over the ranges */ |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 320 | list_for_each_entry(range, &pctldev->gpio_ranges, node) { |
| 321 | /* Check if we're in the valid range */ |
| 322 | if (gpio >= range->base && |
| 323 | gpio < range->base + range->npins) { |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 324 | mutex_unlock(&pctldev->mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 325 | return range; |
| 326 | } |
| 327 | } |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 328 | mutex_unlock(&pctldev->mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 329 | return NULL; |
| 330 | } |
| 331 | |
| 332 | /** |
Haojian Zhuang | 51e13c2 | 2013-02-17 19:42:50 +0800 | [diff] [blame] | 333 | * pinctrl_ready_for_gpio_range() - check if other GPIO pins of |
| 334 | * the same GPIO chip are in range |
| 335 | * @gpio: gpio pin to check taken from the global GPIO pin space |
| 336 | * |
| 337 | * This function is complement of pinctrl_match_gpio_range(). If the return |
| 338 | * value of pinctrl_match_gpio_range() is NULL, this function could be used |
| 339 | * to check whether pinctrl device is ready or not. Maybe some GPIO pins |
| 340 | * of the same GPIO chip don't have back-end pinctrl interface. |
| 341 | * If the return value is true, it means that pinctrl device is ready & the |
| 342 | * certain GPIO pin doesn't have back-end pinctrl device. If the return value |
| 343 | * is false, it means that pinctrl device may not be ready. |
| 344 | */ |
Haojian Zhuang | 2afe822 | 2013-03-28 07:34:19 +0800 | [diff] [blame] | 345 | #ifdef CONFIG_GPIOLIB |
Haojian Zhuang | 51e13c2 | 2013-02-17 19:42:50 +0800 | [diff] [blame] | 346 | static bool pinctrl_ready_for_gpio_range(unsigned gpio) |
| 347 | { |
| 348 | struct pinctrl_dev *pctldev; |
| 349 | struct pinctrl_gpio_range *range = NULL; |
| 350 | struct gpio_chip *chip = gpio_to_chip(gpio); |
| 351 | |
Tony Lindgren | 942cde7 | 2015-09-03 10:34:30 -0700 | [diff] [blame] | 352 | if (WARN(!chip, "no gpio_chip for gpio%i?", gpio)) |
| 353 | return false; |
| 354 | |
Linus Walleij | 44d5f7b | 2013-05-16 09:17:04 +0200 | [diff] [blame] | 355 | mutex_lock(&pinctrldev_list_mutex); |
| 356 | |
Haojian Zhuang | 51e13c2 | 2013-02-17 19:42:50 +0800 | [diff] [blame] | 357 | /* Loop over the pin controllers */ |
| 358 | list_for_each_entry(pctldev, &pinctrldev_list, node) { |
| 359 | /* Loop over the ranges */ |
Axel Lin | 5ffbe2e | 2013-08-18 20:40:29 +0800 | [diff] [blame] | 360 | mutex_lock(&pctldev->mutex); |
Haojian Zhuang | 51e13c2 | 2013-02-17 19:42:50 +0800 | [diff] [blame] | 361 | list_for_each_entry(range, &pctldev->gpio_ranges, node) { |
| 362 | /* Check if any gpio range overlapped with gpio chip */ |
| 363 | if (range->base + range->npins - 1 < chip->base || |
| 364 | range->base > chip->base + chip->ngpio - 1) |
| 365 | continue; |
Axel Lin | 5ffbe2e | 2013-08-18 20:40:29 +0800 | [diff] [blame] | 366 | mutex_unlock(&pctldev->mutex); |
Linus Walleij | 44d5f7b | 2013-05-16 09:17:04 +0200 | [diff] [blame] | 367 | mutex_unlock(&pinctrldev_list_mutex); |
Haojian Zhuang | 51e13c2 | 2013-02-17 19:42:50 +0800 | [diff] [blame] | 368 | return true; |
| 369 | } |
Axel Lin | 5ffbe2e | 2013-08-18 20:40:29 +0800 | [diff] [blame] | 370 | mutex_unlock(&pctldev->mutex); |
Haojian Zhuang | 51e13c2 | 2013-02-17 19:42:50 +0800 | [diff] [blame] | 371 | } |
Linus Walleij | 44d5f7b | 2013-05-16 09:17:04 +0200 | [diff] [blame] | 372 | |
| 373 | mutex_unlock(&pinctrldev_list_mutex); |
| 374 | |
Haojian Zhuang | 51e13c2 | 2013-02-17 19:42:50 +0800 | [diff] [blame] | 375 | return false; |
| 376 | } |
Haojian Zhuang | 2afe822 | 2013-03-28 07:34:19 +0800 | [diff] [blame] | 377 | #else |
| 378 | static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; } |
| 379 | #endif |
Haojian Zhuang | 51e13c2 | 2013-02-17 19:42:50 +0800 | [diff] [blame] | 380 | |
| 381 | /** |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 382 | * pinctrl_get_device_gpio_range() - find device for GPIO range |
| 383 | * @gpio: the pin to locate the pin controller for |
| 384 | * @outdev: the pin control device if found |
| 385 | * @outrange: the GPIO range if found |
| 386 | * |
| 387 | * Find the pin controller handling a certain GPIO pin from the pinspace of |
| 388 | * the GPIO subsystem, return the device and the matching GPIO range. Returns |
Dong Aisheng | 4650b7c | 2012-04-25 19:38:13 +0800 | [diff] [blame] | 389 | * -EPROBE_DEFER if the GPIO range could not be found in any device since it |
| 390 | * may still have not been registered. |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 391 | */ |
Stephen Warren | 4ecce45d | 2012-02-19 23:45:47 -0700 | [diff] [blame] | 392 | static int pinctrl_get_device_gpio_range(unsigned gpio, |
| 393 | struct pinctrl_dev **outdev, |
| 394 | struct pinctrl_gpio_range **outrange) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 395 | { |
| 396 | struct pinctrl_dev *pctldev = NULL; |
| 397 | |
Axel Lin | f005902 | 2013-08-18 20:34:22 +0800 | [diff] [blame] | 398 | mutex_lock(&pinctrldev_list_mutex); |
| 399 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 400 | /* Loop over the pin controllers */ |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 401 | list_for_each_entry(pctldev, &pinctrldev_list, node) { |
| 402 | struct pinctrl_gpio_range *range; |
| 403 | |
| 404 | range = pinctrl_match_gpio_range(pctldev, gpio); |
| 405 | if (range != NULL) { |
| 406 | *outdev = pctldev; |
| 407 | *outrange = range; |
Axel Lin | f005902 | 2013-08-18 20:34:22 +0800 | [diff] [blame] | 408 | mutex_unlock(&pinctrldev_list_mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 412 | |
Axel Lin | f005902 | 2013-08-18 20:34:22 +0800 | [diff] [blame] | 413 | mutex_unlock(&pinctrldev_list_mutex); |
| 414 | |
Dong Aisheng | 4650b7c | 2012-04-25 19:38:13 +0800 | [diff] [blame] | 415 | return -EPROBE_DEFER; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /** |
| 419 | * pinctrl_add_gpio_range() - register a GPIO range for a controller |
| 420 | * @pctldev: pin controller device to add the range to |
| 421 | * @range: the GPIO range to add |
| 422 | * |
| 423 | * This adds a range of GPIOs to be handled by a certain pin controller. Call |
| 424 | * this to register handled ranges after registering your pin controller. |
| 425 | */ |
| 426 | void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev, |
| 427 | struct pinctrl_gpio_range *range) |
| 428 | { |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 429 | mutex_lock(&pctldev->mutex); |
Stephen Warren | 8b9c139 | 2012-02-19 23:45:42 -0700 | [diff] [blame] | 430 | list_add_tail(&range->node, &pctldev->gpio_ranges); |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 431 | mutex_unlock(&pctldev->mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 432 | } |
Stephen Warren | 4ecce45d | 2012-02-19 23:45:47 -0700 | [diff] [blame] | 433 | EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 434 | |
Dong Aisheng | 3e5e00b | 2012-05-23 21:22:41 +0800 | [diff] [blame] | 435 | void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev, |
| 436 | struct pinctrl_gpio_range *ranges, |
| 437 | unsigned nranges) |
| 438 | { |
| 439 | int i; |
| 440 | |
| 441 | for (i = 0; i < nranges; i++) |
| 442 | pinctrl_add_gpio_range(pctldev, &ranges[i]); |
| 443 | } |
| 444 | EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges); |
| 445 | |
Linus Walleij | 192c369 | 2012-11-20 14:03:37 +0100 | [diff] [blame] | 446 | struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname, |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 447 | struct pinctrl_gpio_range *range) |
| 448 | { |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 449 | struct pinctrl_dev *pctldev; |
| 450 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 451 | pctldev = get_pinctrl_dev_from_devname(devname); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 452 | |
Linus Walleij | dfa9751 | 2012-11-20 14:54:18 +0100 | [diff] [blame] | 453 | /* |
| 454 | * If we can't find this device, let's assume that is because |
| 455 | * it has not probed yet, so the driver trying to register this |
| 456 | * range need to defer probing. |
| 457 | */ |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 458 | if (!pctldev) { |
Linus Walleij | dfa9751 | 2012-11-20 14:54:18 +0100 | [diff] [blame] | 459 | return ERR_PTR(-EPROBE_DEFER); |
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 | pinctrl_add_gpio_range(pctldev, range); |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 462 | |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 463 | return pctldev; |
| 464 | } |
Linus Walleij | 192c369 | 2012-11-20 14:03:37 +0100 | [diff] [blame] | 465 | EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 466 | |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 467 | int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group, |
| 468 | const unsigned **pins, unsigned *num_pins) |
| 469 | { |
| 470 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
| 471 | int gs; |
| 472 | |
Antoine Ténart | e5b3b2d | 2014-04-10 15:07:50 +0200 | [diff] [blame] | 473 | if (!pctlops->get_group_pins) |
| 474 | return -EINVAL; |
| 475 | |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 476 | gs = pinctrl_get_group_selector(pctldev, pin_group); |
| 477 | if (gs < 0) |
| 478 | return gs; |
| 479 | |
| 480 | return pctlops->get_group_pins(pctldev, gs, pins, num_pins); |
| 481 | } |
| 482 | EXPORT_SYMBOL_GPL(pinctrl_get_group_pins); |
| 483 | |
Joachim Eastwood | b18537c | 2016-02-25 22:44:37 +0100 | [diff] [blame] | 484 | struct pinctrl_gpio_range * |
| 485 | pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev, |
| 486 | unsigned int pin) |
| 487 | { |
| 488 | struct pinctrl_gpio_range *range; |
| 489 | |
| 490 | /* Loop over the ranges */ |
| 491 | list_for_each_entry(range, &pctldev->gpio_ranges, node) { |
| 492 | /* Check if we're in the valid range */ |
| 493 | if (range->pins) { |
| 494 | int a; |
| 495 | for (a = 0; a < range->npins; a++) { |
| 496 | if (range->pins[a] == pin) |
| 497 | return range; |
| 498 | } |
| 499 | } else if (pin >= range->pin_base && |
| 500 | pin < range->pin_base + range->npins) |
| 501 | return range; |
| 502 | } |
| 503 | |
| 504 | return NULL; |
| 505 | } |
| 506 | EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock); |
| 507 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 508 | /** |
Linus Walleij | 9afbefb | 2012-11-20 14:25:07 +0100 | [diff] [blame] | 509 | * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin |
| 510 | * @pctldev: the pin controller device to look in |
| 511 | * @pin: a controller-local number to find the range for |
| 512 | */ |
| 513 | struct pinctrl_gpio_range * |
| 514 | pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev, |
| 515 | unsigned int pin) |
| 516 | { |
Wei Yongjun | c8f50e8 | 2013-06-18 12:24:58 +0800 | [diff] [blame] | 517 | struct pinctrl_gpio_range *range; |
Linus Walleij | 9afbefb | 2012-11-20 14:25:07 +0100 | [diff] [blame] | 518 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 519 | mutex_lock(&pctldev->mutex); |
Joachim Eastwood | b18537c | 2016-02-25 22:44:37 +0100 | [diff] [blame] | 520 | range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin); |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 521 | mutex_unlock(&pctldev->mutex); |
Joachim Eastwood | b18537c | 2016-02-25 22:44:37 +0100 | [diff] [blame] | 522 | |
Wei Yongjun | c8f50e8 | 2013-06-18 12:24:58 +0800 | [diff] [blame] | 523 | return range; |
Linus Walleij | 9afbefb | 2012-11-20 14:25:07 +0100 | [diff] [blame] | 524 | } |
| 525 | EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin); |
| 526 | |
| 527 | /** |
Charles Keepax | 50842cb | 2017-02-13 10:11:03 +0000 | [diff] [blame] | 528 | * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller |
Viresh Kumar | 7e10ee6 | 2012-10-27 15:21:35 +0530 | [diff] [blame] | 529 | * @pctldev: pin controller device to remove the range from |
| 530 | * @range: the GPIO range to remove |
| 531 | */ |
| 532 | void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev, |
| 533 | struct pinctrl_gpio_range *range) |
| 534 | { |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 535 | mutex_lock(&pctldev->mutex); |
Viresh Kumar | 7e10ee6 | 2012-10-27 15:21:35 +0530 | [diff] [blame] | 536 | list_del(&range->node); |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 537 | mutex_unlock(&pctldev->mutex); |
Viresh Kumar | 7e10ee6 | 2012-10-27 15:21:35 +0530 | [diff] [blame] | 538 | } |
| 539 | EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range); |
| 540 | |
Linus Walleij | c033a71 | 2016-12-30 15:04:43 +0100 | [diff] [blame] | 541 | #ifdef CONFIG_GENERIC_PINCTRL_GROUPS |
Tony Lindgren | c7059c5 | 2016-12-27 09:20:00 -0800 | [diff] [blame] | 542 | |
| 543 | /** |
| 544 | * pinctrl_generic_get_group_count() - returns the number of pin groups |
| 545 | * @pctldev: pin controller device |
| 546 | */ |
| 547 | int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev) |
| 548 | { |
| 549 | return pctldev->num_groups; |
| 550 | } |
| 551 | EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count); |
| 552 | |
| 553 | /** |
| 554 | * pinctrl_generic_get_group_name() - returns the name of a pin group |
| 555 | * @pctldev: pin controller device |
| 556 | * @selector: group number |
| 557 | */ |
| 558 | const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev, |
| 559 | unsigned int selector) |
| 560 | { |
| 561 | struct group_desc *group; |
| 562 | |
| 563 | group = radix_tree_lookup(&pctldev->pin_group_tree, |
| 564 | selector); |
| 565 | if (!group) |
| 566 | return NULL; |
| 567 | |
| 568 | return group->name; |
| 569 | } |
| 570 | EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name); |
| 571 | |
| 572 | /** |
| 573 | * pinctrl_generic_get_group_pins() - gets the pin group pins |
| 574 | * @pctldev: pin controller device |
| 575 | * @selector: group number |
| 576 | * @pins: pins in the group |
| 577 | * @num_pins: number of pins in the group |
| 578 | */ |
| 579 | int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev, |
| 580 | unsigned int selector, |
| 581 | const unsigned int **pins, |
| 582 | unsigned int *num_pins) |
| 583 | { |
| 584 | struct group_desc *group; |
| 585 | |
| 586 | group = radix_tree_lookup(&pctldev->pin_group_tree, |
| 587 | selector); |
| 588 | if (!group) { |
| 589 | dev_err(pctldev->dev, "%s could not find pingroup%i\n", |
| 590 | __func__, selector); |
| 591 | return -EINVAL; |
| 592 | } |
| 593 | |
| 594 | *pins = group->pins; |
| 595 | *num_pins = group->num_pins; |
| 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins); |
| 600 | |
| 601 | /** |
| 602 | * pinctrl_generic_get_group() - returns a pin group based on the number |
| 603 | * @pctldev: pin controller device |
| 604 | * @gselector: group number |
| 605 | */ |
| 606 | struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev, |
| 607 | unsigned int selector) |
| 608 | { |
| 609 | struct group_desc *group; |
| 610 | |
| 611 | group = radix_tree_lookup(&pctldev->pin_group_tree, |
| 612 | selector); |
| 613 | if (!group) |
| 614 | return NULL; |
| 615 | |
| 616 | return group; |
| 617 | } |
| 618 | EXPORT_SYMBOL_GPL(pinctrl_generic_get_group); |
| 619 | |
| 620 | /** |
| 621 | * pinctrl_generic_add_group() - adds a new pin group |
| 622 | * @pctldev: pin controller device |
| 623 | * @name: name of the pin group |
| 624 | * @pins: pins in the pin group |
| 625 | * @num_pins: number of pins in the pin group |
| 626 | * @data: pin controller driver specific data |
| 627 | * |
| 628 | * Note that the caller must take care of locking. |
| 629 | */ |
| 630 | int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name, |
| 631 | int *pins, int num_pins, void *data) |
| 632 | { |
| 633 | struct group_desc *group; |
| 634 | |
| 635 | group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL); |
| 636 | if (!group) |
| 637 | return -ENOMEM; |
| 638 | |
| 639 | group->name = name; |
| 640 | group->pins = pins; |
| 641 | group->num_pins = num_pins; |
| 642 | group->data = data; |
| 643 | |
| 644 | radix_tree_insert(&pctldev->pin_group_tree, pctldev->num_groups, |
| 645 | group); |
| 646 | |
| 647 | pctldev->num_groups++; |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | EXPORT_SYMBOL_GPL(pinctrl_generic_add_group); |
| 652 | |
| 653 | /** |
| 654 | * pinctrl_generic_remove_group() - removes a numbered pin group |
| 655 | * @pctldev: pin controller device |
| 656 | * @selector: group number |
| 657 | * |
| 658 | * Note that the caller must take care of locking. |
| 659 | */ |
| 660 | int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev, |
| 661 | unsigned int selector) |
| 662 | { |
| 663 | struct group_desc *group; |
| 664 | |
| 665 | group = radix_tree_lookup(&pctldev->pin_group_tree, |
| 666 | selector); |
| 667 | if (!group) |
| 668 | return -ENOENT; |
| 669 | |
| 670 | radix_tree_delete(&pctldev->pin_group_tree, selector); |
| 671 | devm_kfree(pctldev->dev, group); |
| 672 | |
| 673 | pctldev->num_groups--; |
| 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group); |
| 678 | |
| 679 | /** |
| 680 | * pinctrl_generic_free_groups() - removes all pin groups |
| 681 | * @pctldev: pin controller device |
| 682 | * |
Tony Lindgren | 664b7c4 | 2017-05-12 08:47:57 -0700 | [diff] [blame] | 683 | * Note that the caller must take care of locking. The pinctrl groups |
| 684 | * are allocated with devm_kzalloc() so no need to free them here. |
Tony Lindgren | c7059c5 | 2016-12-27 09:20:00 -0800 | [diff] [blame] | 685 | */ |
| 686 | static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev) |
| 687 | { |
| 688 | struct radix_tree_iter iter; |
Tony Lindgren | c7059c5 | 2016-12-27 09:20:00 -0800 | [diff] [blame] | 689 | void **slot; |
Tony Lindgren | c7059c5 | 2016-12-27 09:20:00 -0800 | [diff] [blame] | 690 | |
| 691 | radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0) |
Tony Lindgren | 664b7c4 | 2017-05-12 08:47:57 -0700 | [diff] [blame] | 692 | radix_tree_delete(&pctldev->pin_group_tree, iter.index); |
Tony Lindgren | c7059c5 | 2016-12-27 09:20:00 -0800 | [diff] [blame] | 693 | |
| 694 | pctldev->num_groups = 0; |
| 695 | } |
| 696 | |
| 697 | #else |
| 698 | static inline void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev) |
| 699 | { |
| 700 | } |
Linus Walleij | c033a71 | 2016-12-30 15:04:43 +0100 | [diff] [blame] | 701 | #endif /* CONFIG_GENERIC_PINCTRL_GROUPS */ |
Tony Lindgren | c7059c5 | 2016-12-27 09:20:00 -0800 | [diff] [blame] | 702 | |
Viresh Kumar | 7e10ee6 | 2012-10-27 15:21:35 +0530 | [diff] [blame] | 703 | /** |
Linus Walleij | 7afde8b | 2011-10-19 17:07:16 +0200 | [diff] [blame] | 704 | * pinctrl_get_group_selector() - returns the group selector for a group |
| 705 | * @pctldev: the pin controller handling the group |
| 706 | * @pin_group: the pin group to look up |
| 707 | */ |
| 708 | int pinctrl_get_group_selector(struct pinctrl_dev *pctldev, |
| 709 | const char *pin_group) |
| 710 | { |
| 711 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 712 | unsigned ngroups = pctlops->get_groups_count(pctldev); |
Linus Walleij | 7afde8b | 2011-10-19 17:07:16 +0200 | [diff] [blame] | 713 | unsigned group_selector = 0; |
| 714 | |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 715 | while (group_selector < ngroups) { |
Linus Walleij | 7afde8b | 2011-10-19 17:07:16 +0200 | [diff] [blame] | 716 | const char *gname = pctlops->get_group_name(pctldev, |
| 717 | group_selector); |
| 718 | if (!strcmp(gname, pin_group)) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 719 | dev_dbg(pctldev->dev, |
Linus Walleij | 7afde8b | 2011-10-19 17:07:16 +0200 | [diff] [blame] | 720 | "found group selector %u for %s\n", |
| 721 | group_selector, |
| 722 | pin_group); |
| 723 | return group_selector; |
| 724 | } |
| 725 | |
| 726 | group_selector++; |
| 727 | } |
| 728 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 729 | dev_err(pctldev->dev, "does not have pin group %s\n", |
Linus Walleij | 7afde8b | 2011-10-19 17:07:16 +0200 | [diff] [blame] | 730 | pin_group); |
| 731 | |
| 732 | return -EINVAL; |
| 733 | } |
| 734 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 735 | /** |
Geert Uytterhoeven | b217e43 | 2015-05-04 19:46:57 +0200 | [diff] [blame] | 736 | * pinctrl_request_gpio() - request a single pin to be used as GPIO |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 737 | * @gpio: the GPIO pin number from the GPIO subsystem number space |
| 738 | * |
| 739 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, |
| 740 | * as part of their gpio_request() semantics, platforms and individual drivers |
| 741 | * shall *NOT* request GPIO pins to be muxed in. |
| 742 | */ |
| 743 | int pinctrl_request_gpio(unsigned gpio) |
| 744 | { |
| 745 | struct pinctrl_dev *pctldev; |
| 746 | struct pinctrl_gpio_range *range; |
| 747 | int ret; |
| 748 | int pin; |
| 749 | |
| 750 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 751 | if (ret) { |
Haojian Zhuang | 51e13c2 | 2013-02-17 19:42:50 +0800 | [diff] [blame] | 752 | if (pinctrl_ready_for_gpio_range(gpio)) |
| 753 | ret = 0; |
Dong Aisheng | 4650b7c | 2012-04-25 19:38:13 +0800 | [diff] [blame] | 754 | return ret; |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 755 | } |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 756 | |
Axel Lin | 9b77ace4 | 2013-08-19 10:07:46 +0800 | [diff] [blame] | 757 | mutex_lock(&pctldev->mutex); |
| 758 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 759 | /* Convert to the pin controllers number space */ |
Christian Ruppert | c8587ee | 2013-06-13 14:55:31 +0200 | [diff] [blame] | 760 | pin = gpio_to_pin(range, gpio); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 761 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 762 | ret = pinmux_request_gpio(pctldev, range, pin, gpio); |
| 763 | |
Axel Lin | 9b77ace4 | 2013-08-19 10:07:46 +0800 | [diff] [blame] | 764 | mutex_unlock(&pctldev->mutex); |
| 765 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 766 | return ret; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 767 | } |
| 768 | EXPORT_SYMBOL_GPL(pinctrl_request_gpio); |
| 769 | |
| 770 | /** |
| 771 | * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO |
| 772 | * @gpio: the GPIO pin number from the GPIO subsystem number space |
| 773 | * |
| 774 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, |
| 775 | * as part of their gpio_free() semantics, platforms and individual drivers |
| 776 | * shall *NOT* request GPIO pins to be muxed out. |
| 777 | */ |
| 778 | void pinctrl_free_gpio(unsigned gpio) |
| 779 | { |
| 780 | struct pinctrl_dev *pctldev; |
| 781 | struct pinctrl_gpio_range *range; |
| 782 | int ret; |
| 783 | int pin; |
| 784 | |
| 785 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 786 | if (ret) { |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 787 | return; |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 788 | } |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 789 | mutex_lock(&pctldev->mutex); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 790 | |
| 791 | /* Convert to the pin controllers number space */ |
Christian Ruppert | c8587ee | 2013-06-13 14:55:31 +0200 | [diff] [blame] | 792 | pin = gpio_to_pin(range, gpio); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 793 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 794 | pinmux_free_gpio(pctldev, pin, range); |
| 795 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 796 | mutex_unlock(&pctldev->mutex); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 797 | } |
| 798 | EXPORT_SYMBOL_GPL(pinctrl_free_gpio); |
| 799 | |
| 800 | static int pinctrl_gpio_direction(unsigned gpio, bool input) |
| 801 | { |
| 802 | struct pinctrl_dev *pctldev; |
| 803 | struct pinctrl_gpio_range *range; |
| 804 | int ret; |
| 805 | int pin; |
| 806 | |
| 807 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 808 | if (ret) { |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 809 | return ret; |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | mutex_lock(&pctldev->mutex); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 813 | |
| 814 | /* Convert to the pin controllers number space */ |
Christian Ruppert | c8587ee | 2013-06-13 14:55:31 +0200 | [diff] [blame] | 815 | pin = gpio_to_pin(range, gpio); |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 816 | ret = pinmux_gpio_direction(pctldev, range, pin, input); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 817 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 818 | mutex_unlock(&pctldev->mutex); |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 819 | |
| 820 | return ret; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /** |
| 824 | * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode |
| 825 | * @gpio: the GPIO pin number from the GPIO subsystem number space |
| 826 | * |
| 827 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, |
| 828 | * as part of their gpio_direction_input() semantics, platforms and individual |
| 829 | * drivers shall *NOT* touch pin control GPIO calls. |
| 830 | */ |
| 831 | int pinctrl_gpio_direction_input(unsigned gpio) |
| 832 | { |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 833 | return pinctrl_gpio_direction(gpio, true); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 834 | } |
| 835 | EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input); |
| 836 | |
| 837 | /** |
| 838 | * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode |
| 839 | * @gpio: the GPIO pin number from the GPIO subsystem number space |
| 840 | * |
| 841 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, |
| 842 | * as part of their gpio_direction_output() semantics, platforms and individual |
| 843 | * drivers shall *NOT* touch pin control GPIO calls. |
| 844 | */ |
| 845 | int pinctrl_gpio_direction_output(unsigned gpio) |
| 846 | { |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 847 | return pinctrl_gpio_direction(gpio, false); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 848 | } |
| 849 | EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output); |
| 850 | |
Mika Westerberg | 15381bc | 2017-01-23 15:34:33 +0300 | [diff] [blame] | 851 | /** |
| 852 | * pinctrl_gpio_set_config() - Apply config to given GPIO pin |
| 853 | * @gpio: the GPIO pin number from the GPIO subsystem number space |
| 854 | * @config: the configuration to apply to the GPIO |
| 855 | * |
| 856 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, if |
| 857 | * they need to call the underlying pin controller to change GPIO config |
| 858 | * (for example set debounce time). |
| 859 | */ |
| 860 | int pinctrl_gpio_set_config(unsigned gpio, unsigned long config) |
| 861 | { |
| 862 | unsigned long configs[] = { config }; |
| 863 | struct pinctrl_gpio_range *range; |
| 864 | struct pinctrl_dev *pctldev; |
| 865 | int ret, pin; |
| 866 | |
| 867 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); |
| 868 | if (ret) |
| 869 | return ret; |
| 870 | |
| 871 | mutex_lock(&pctldev->mutex); |
| 872 | pin = gpio_to_pin(range, gpio); |
| 873 | ret = pinconf_set_config(pctldev, pin, configs, ARRAY_SIZE(configs)); |
| 874 | mutex_unlock(&pctldev->mutex); |
| 875 | |
| 876 | return ret; |
| 877 | } |
| 878 | EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config); |
| 879 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 880 | static struct pinctrl_state *find_state(struct pinctrl *p, |
| 881 | const char *name) |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 882 | { |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 883 | struct pinctrl_state *state; |
| 884 | |
| 885 | list_for_each_entry(state, &p->states, node) |
| 886 | if (!strcmp(state->name, name)) |
| 887 | return state; |
| 888 | |
| 889 | return NULL; |
| 890 | } |
| 891 | |
| 892 | static struct pinctrl_state *create_state(struct pinctrl *p, |
| 893 | const char *name) |
| 894 | { |
| 895 | struct pinctrl_state *state; |
| 896 | |
| 897 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
Bjorn Andersson | 2104d12 | 2017-01-05 08:07:55 -0800 | [diff] [blame] | 898 | if (!state) |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 899 | return ERR_PTR(-ENOMEM); |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 900 | |
| 901 | state->name = name; |
| 902 | INIT_LIST_HEAD(&state->settings); |
| 903 | |
| 904 | list_add_tail(&state->node, &p->states); |
| 905 | |
| 906 | return state; |
| 907 | } |
| 908 | |
Tony Lindgren | 99e4f67 | 2016-12-27 09:19:59 -0800 | [diff] [blame] | 909 | static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev, |
| 910 | struct pinctrl_map const *map) |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 911 | { |
| 912 | struct pinctrl_state *state; |
| 913 | struct pinctrl_setting *setting; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 914 | int ret; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 915 | |
| 916 | state = find_state(p, map->name); |
| 917 | if (!state) |
| 918 | state = create_state(p, map->name); |
| 919 | if (IS_ERR(state)) |
| 920 | return PTR_ERR(state); |
| 921 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 922 | if (map->type == PIN_MAP_TYPE_DUMMY_STATE) |
| 923 | return 0; |
| 924 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 925 | setting = kzalloc(sizeof(*setting), GFP_KERNEL); |
Bjorn Andersson | 2104d12 | 2017-01-05 08:07:55 -0800 | [diff] [blame] | 926 | if (!setting) |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 927 | return -ENOMEM; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 928 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 929 | setting->type = map->type; |
| 930 | |
Tony Lindgren | 99e4f67 | 2016-12-27 09:19:59 -0800 | [diff] [blame] | 931 | if (pctldev) |
| 932 | setting->pctldev = pctldev; |
| 933 | else |
| 934 | setting->pctldev = |
| 935 | get_pinctrl_dev_from_devname(map->ctrl_dev_name); |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 936 | if (setting->pctldev == NULL) { |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 937 | kfree(setting); |
Linus Walleij | 8921649 | 2012-12-11 14:14:32 +0100 | [diff] [blame] | 938 | /* Do not defer probing of hogs (circular loop) */ |
| 939 | if (!strcmp(map->ctrl_dev_name, map->dev_name)) |
| 940 | return -ENODEV; |
Linus Walleij | c05127c | 2012-04-10 10:00:38 +0200 | [diff] [blame] | 941 | /* |
| 942 | * OK let us guess that the driver is not there yet, and |
| 943 | * let's defer obtaining this pinctrl handle to later... |
| 944 | */ |
Linus Walleij | 8921649 | 2012-12-11 14:14:32 +0100 | [diff] [blame] | 945 | dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe", |
| 946 | map->ctrl_dev_name); |
Linus Walleij | c05127c | 2012-04-10 10:00:38 +0200 | [diff] [blame] | 947 | return -EPROBE_DEFER; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Linus Walleij | 1a78958 | 2012-10-17 20:51:54 +0200 | [diff] [blame] | 950 | setting->dev_name = map->dev_name; |
| 951 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 952 | switch (map->type) { |
| 953 | case PIN_MAP_TYPE_MUX_GROUP: |
| 954 | ret = pinmux_map_to_setting(map, setting); |
| 955 | break; |
| 956 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 957 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 958 | ret = pinconf_map_to_setting(map, setting); |
| 959 | break; |
| 960 | default: |
| 961 | ret = -EINVAL; |
| 962 | break; |
| 963 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 964 | if (ret < 0) { |
| 965 | kfree(setting); |
| 966 | return ret; |
| 967 | } |
| 968 | |
| 969 | list_add_tail(&setting->node, &state->settings); |
| 970 | |
| 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | static struct pinctrl *find_pinctrl(struct device *dev) |
| 975 | { |
| 976 | struct pinctrl *p; |
| 977 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 978 | mutex_lock(&pinctrl_list_mutex); |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 979 | list_for_each_entry(p, &pinctrl_list, node) |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 980 | if (p->dev == dev) { |
| 981 | mutex_unlock(&pinctrl_list_mutex); |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 982 | return p; |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 983 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 984 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 985 | mutex_unlock(&pinctrl_list_mutex); |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 986 | return NULL; |
| 987 | } |
| 988 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 989 | static void pinctrl_free(struct pinctrl *p, bool inlist); |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 990 | |
Tony Lindgren | 99e4f67 | 2016-12-27 09:19:59 -0800 | [diff] [blame] | 991 | static struct pinctrl *create_pinctrl(struct device *dev, |
| 992 | struct pinctrl_dev *pctldev) |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 993 | { |
| 994 | struct pinctrl *p; |
| 995 | const char *devname; |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 996 | struct pinctrl_maps *maps_node; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 997 | int i; |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 998 | struct pinctrl_map const *map; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 999 | int ret; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1000 | |
| 1001 | /* |
| 1002 | * create the state cookie holder struct pinctrl for each |
| 1003 | * mapping, this is what consumers will get when requesting |
| 1004 | * a pin control handle with pinctrl_get() |
| 1005 | */ |
Stephen Warren | 02f5b98 | 2012-02-22 14:26:00 -0700 | [diff] [blame] | 1006 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
Bjorn Andersson | 2104d12 | 2017-01-05 08:07:55 -0800 | [diff] [blame] | 1007 | if (!p) |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1008 | return ERR_PTR(-ENOMEM); |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 1009 | p->dev = dev; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1010 | INIT_LIST_HEAD(&p->states); |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 1011 | INIT_LIST_HEAD(&p->dt_maps); |
| 1012 | |
Tony Lindgren | 99e4f67 | 2016-12-27 09:19:59 -0800 | [diff] [blame] | 1013 | ret = pinctrl_dt_to_map(p, pctldev); |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 1014 | if (ret < 0) { |
| 1015 | kfree(p); |
| 1016 | return ERR_PTR(ret); |
| 1017 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1018 | |
| 1019 | devname = dev_name(dev); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1020 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1021 | mutex_lock(&pinctrl_maps_mutex); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1022 | /* Iterate over the pin control maps to locate the right ones */ |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 1023 | for_each_maps(maps_node, i, map) { |
Stephen Warren | 1681f5a | 2012-02-22 14:25:58 -0700 | [diff] [blame] | 1024 | /* Map must be for this device */ |
| 1025 | if (strcmp(map->dev_name, devname)) |
| 1026 | continue; |
| 1027 | |
Tony Lindgren | 99e4f67 | 2016-12-27 09:19:59 -0800 | [diff] [blame] | 1028 | ret = add_setting(p, pctldev, map); |
Linus Walleij | 8921649 | 2012-12-11 14:14:32 +0100 | [diff] [blame] | 1029 | /* |
| 1030 | * At this point the adding of a setting may: |
| 1031 | * |
| 1032 | * - Defer, if the pinctrl device is not yet available |
| 1033 | * - Fail, if the pinctrl device is not yet available, |
| 1034 | * AND the setting is a hog. We cannot defer that, since |
| 1035 | * the hog will kick in immediately after the device |
| 1036 | * is registered. |
| 1037 | * |
| 1038 | * If the error returned was not -EPROBE_DEFER then we |
| 1039 | * accumulate the errors to see if we end up with |
| 1040 | * an -EPROBE_DEFER later, as that is the worst case. |
| 1041 | */ |
| 1042 | if (ret == -EPROBE_DEFER) { |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1043 | pinctrl_free(p, false); |
| 1044 | mutex_unlock(&pinctrl_maps_mutex); |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1045 | return ERR_PTR(ret); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1046 | } |
| 1047 | } |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1048 | mutex_unlock(&pinctrl_maps_mutex); |
| 1049 | |
Linus Walleij | 8921649 | 2012-12-11 14:14:32 +0100 | [diff] [blame] | 1050 | if (ret < 0) { |
Andy Shevchenko | 3ec440e | 2017-02-28 16:59:56 +0200 | [diff] [blame] | 1051 | /* If some other error than deferral occurred, return here */ |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1052 | pinctrl_free(p, false); |
Linus Walleij | 8921649 | 2012-12-11 14:14:32 +0100 | [diff] [blame] | 1053 | return ERR_PTR(ret); |
| 1054 | } |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1055 | |
Linus Walleij | ab78029 | 2013-01-22 10:56:14 -0700 | [diff] [blame] | 1056 | kref_init(&p->users); |
| 1057 | |
Linus Walleij | b0666ba | 2012-12-11 13:12:35 +0100 | [diff] [blame] | 1058 | /* Add the pinctrl handle to the global list */ |
Stanislaw Gruszka | 7b320cb | 2014-02-04 09:07:09 +0100 | [diff] [blame] | 1059 | mutex_lock(&pinctrl_list_mutex); |
Stephen Warren | 8b9c139 | 2012-02-19 23:45:42 -0700 | [diff] [blame] | 1060 | list_add_tail(&p->node, &pinctrl_list); |
Stanislaw Gruszka | 7b320cb | 2014-02-04 09:07:09 +0100 | [diff] [blame] | 1061 | mutex_unlock(&pinctrl_list_mutex); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1062 | |
| 1063 | return p; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1064 | } |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 1065 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1066 | /** |
| 1067 | * pinctrl_get() - retrieves the pinctrl handle for a device |
| 1068 | * @dev: the device to obtain the handle for |
| 1069 | */ |
| 1070 | struct pinctrl *pinctrl_get(struct device *dev) |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1071 | { |
| 1072 | struct pinctrl *p; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 1073 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1074 | if (WARN_ON(!dev)) |
| 1075 | return ERR_PTR(-EINVAL); |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 1076 | |
Linus Walleij | ab78029 | 2013-01-22 10:56:14 -0700 | [diff] [blame] | 1077 | /* |
| 1078 | * See if somebody else (such as the device core) has already |
| 1079 | * obtained a handle to the pinctrl for this device. In that case, |
| 1080 | * return another pointer to it. |
| 1081 | */ |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1082 | p = find_pinctrl(dev); |
Linus Walleij | ab78029 | 2013-01-22 10:56:14 -0700 | [diff] [blame] | 1083 | if (p != NULL) { |
| 1084 | dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n"); |
| 1085 | kref_get(&p->users); |
| 1086 | return p; |
| 1087 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1088 | |
Tony Lindgren | 99e4f67 | 2016-12-27 09:19:59 -0800 | [diff] [blame] | 1089 | return create_pinctrl(dev, NULL); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1090 | } |
| 1091 | EXPORT_SYMBOL_GPL(pinctrl_get); |
| 1092 | |
Richard Genoud | d3cee830 | 2013-03-25 15:47:21 +0100 | [diff] [blame] | 1093 | static void pinctrl_free_setting(bool disable_setting, |
| 1094 | struct pinctrl_setting *setting) |
| 1095 | { |
| 1096 | switch (setting->type) { |
| 1097 | case PIN_MAP_TYPE_MUX_GROUP: |
| 1098 | if (disable_setting) |
| 1099 | pinmux_disable_setting(setting); |
| 1100 | pinmux_free_setting(setting); |
| 1101 | break; |
| 1102 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 1103 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 1104 | pinconf_free_setting(setting); |
| 1105 | break; |
| 1106 | default: |
| 1107 | break; |
| 1108 | } |
| 1109 | } |
| 1110 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1111 | static void pinctrl_free(struct pinctrl *p, bool inlist) |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1112 | { |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1113 | struct pinctrl_state *state, *n1; |
| 1114 | struct pinctrl_setting *setting, *n2; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 1115 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1116 | mutex_lock(&pinctrl_list_mutex); |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1117 | list_for_each_entry_safe(state, n1, &p->states, node) { |
| 1118 | list_for_each_entry_safe(setting, n2, &state->settings, node) { |
Richard Genoud | d3cee830 | 2013-03-25 15:47:21 +0100 | [diff] [blame] | 1119 | pinctrl_free_setting(state == p->state, setting); |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1120 | list_del(&setting->node); |
| 1121 | kfree(setting); |
| 1122 | } |
| 1123 | list_del(&state->node); |
| 1124 | kfree(state); |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 1125 | } |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1126 | |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 1127 | pinctrl_dt_free_maps(p); |
| 1128 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1129 | if (inlist) |
| 1130 | list_del(&p->node); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1131 | kfree(p); |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1132 | mutex_unlock(&pinctrl_list_mutex); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1135 | /** |
Linus Walleij | ab78029 | 2013-01-22 10:56:14 -0700 | [diff] [blame] | 1136 | * pinctrl_release() - release the pinctrl handle |
| 1137 | * @kref: the kref in the pinctrl being released |
| 1138 | */ |
Sachin Kamat | 2917e83 | 2013-01-24 15:01:00 +0530 | [diff] [blame] | 1139 | static void pinctrl_release(struct kref *kref) |
Linus Walleij | ab78029 | 2013-01-22 10:56:14 -0700 | [diff] [blame] | 1140 | { |
| 1141 | struct pinctrl *p = container_of(kref, struct pinctrl, users); |
| 1142 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1143 | pinctrl_free(p, true); |
Linus Walleij | ab78029 | 2013-01-22 10:56:14 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | /** |
| 1147 | * pinctrl_put() - decrease use count on a previously claimed pinctrl handle |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1148 | * @p: the pinctrl handle to release |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1149 | */ |
| 1150 | void pinctrl_put(struct pinctrl *p) |
| 1151 | { |
Linus Walleij | ab78029 | 2013-01-22 10:56:14 -0700 | [diff] [blame] | 1152 | kref_put(&p->users, pinctrl_release); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1153 | } |
| 1154 | EXPORT_SYMBOL_GPL(pinctrl_put); |
| 1155 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1156 | /** |
| 1157 | * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle |
| 1158 | * @p: the pinctrl handle to retrieve the state from |
| 1159 | * @name: the state name to retrieve |
| 1160 | */ |
| 1161 | struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, |
| 1162 | const char *name) |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1163 | { |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1164 | struct pinctrl_state *state; |
| 1165 | |
| 1166 | state = find_state(p, name); |
Dong Aisheng | 5b3aa5f | 2012-04-26 16:15:50 +0800 | [diff] [blame] | 1167 | if (!state) { |
| 1168 | if (pinctrl_dummy_state) { |
| 1169 | /* create dummy state */ |
| 1170 | dev_dbg(p->dev, "using pinctrl dummy state (%s)\n", |
| 1171 | name); |
| 1172 | state = create_state(p, name); |
Richard Genoud | d599bfb | 2012-08-10 16:53:49 +0200 | [diff] [blame] | 1173 | } else |
| 1174 | state = ERR_PTR(-ENODEV); |
Dong Aisheng | 5b3aa5f | 2012-04-26 16:15:50 +0800 | [diff] [blame] | 1175 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1176 | |
| 1177 | return state; |
| 1178 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1179 | EXPORT_SYMBOL_GPL(pinctrl_lookup_state); |
| 1180 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1181 | /** |
| 1182 | * pinctrl_select_state() - select/activate/program a pinctrl state to HW |
| 1183 | * @p: the pinctrl handle for the device that requests configuration |
| 1184 | * @state: the state handle to select/activate/program |
| 1185 | */ |
| 1186 | int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state) |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1187 | { |
| 1188 | struct pinctrl_setting *setting, *setting2; |
Richard Genoud | 50cf7c8 | 2013-03-25 15:47:23 +0100 | [diff] [blame] | 1189 | struct pinctrl_state *old_state = p->state; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 1190 | int ret; |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1191 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1192 | if (p->state == state) |
| 1193 | return 0; |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1194 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1195 | if (p->state) { |
| 1196 | /* |
Fan Wu | 2243a87 | 2014-06-09 09:37:56 +0800 | [diff] [blame] | 1197 | * For each pinmux setting in the old state, forget SW's record |
| 1198 | * of mux owner for that pingroup. Any pingroups which are |
| 1199 | * still owned by the new state will be re-acquired by the call |
| 1200 | * to pinmux_enable_setting() in the loop below. |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1201 | */ |
| 1202 | list_for_each_entry(setting, &p->state->settings, node) { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 1203 | if (setting->type != PIN_MAP_TYPE_MUX_GROUP) |
| 1204 | continue; |
Fan Wu | 2243a87 | 2014-06-09 09:37:56 +0800 | [diff] [blame] | 1205 | pinmux_disable_setting(setting); |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1206 | } |
| 1207 | } |
| 1208 | |
Richard Genoud | 3102a76 | 2013-03-25 15:47:22 +0100 | [diff] [blame] | 1209 | p->state = NULL; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1210 | |
| 1211 | /* Apply all the settings for the new state */ |
| 1212 | list_for_each_entry(setting, &state->settings, node) { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 1213 | switch (setting->type) { |
| 1214 | case PIN_MAP_TYPE_MUX_GROUP: |
| 1215 | ret = pinmux_enable_setting(setting); |
| 1216 | break; |
| 1217 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 1218 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 1219 | ret = pinconf_apply_setting(setting); |
| 1220 | break; |
| 1221 | default: |
| 1222 | ret = -EINVAL; |
| 1223 | break; |
| 1224 | } |
Richard Genoud | 3102a76 | 2013-03-25 15:47:22 +0100 | [diff] [blame] | 1225 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1226 | if (ret < 0) { |
Richard Genoud | 3102a76 | 2013-03-25 15:47:22 +0100 | [diff] [blame] | 1227 | goto unapply_new_state; |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1228 | } |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
Richard Genoud | 3102a76 | 2013-03-25 15:47:22 +0100 | [diff] [blame] | 1231 | p->state = state; |
| 1232 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 1233 | return 0; |
Richard Genoud | 3102a76 | 2013-03-25 15:47:22 +0100 | [diff] [blame] | 1234 | |
| 1235 | unapply_new_state: |
Richard Genoud | da58751 | 2013-03-28 12:55:46 +0100 | [diff] [blame] | 1236 | dev_err(p->dev, "Error applying setting, reverse things back\n"); |
Richard Genoud | 3102a76 | 2013-03-25 15:47:22 +0100 | [diff] [blame] | 1237 | |
Richard Genoud | 3102a76 | 2013-03-25 15:47:22 +0100 | [diff] [blame] | 1238 | list_for_each_entry(setting2, &state->settings, node) { |
| 1239 | if (&setting2->node == &setting->node) |
| 1240 | break; |
Richard Genoud | af60617 | 2013-03-29 10:03:26 +0100 | [diff] [blame] | 1241 | /* |
| 1242 | * All we can do here is pinmux_disable_setting. |
| 1243 | * That means that some pins are muxed differently now |
| 1244 | * than they were before applying the setting (We can't |
| 1245 | * "unmux a pin"!), but it's not a big deal since the pins |
| 1246 | * are free to be muxed by another apply_setting. |
| 1247 | */ |
| 1248 | if (setting2->type == PIN_MAP_TYPE_MUX_GROUP) |
| 1249 | pinmux_disable_setting(setting2); |
Richard Genoud | 3102a76 | 2013-03-25 15:47:22 +0100 | [diff] [blame] | 1250 | } |
Richard Genoud | 8009d5f | 2013-03-28 12:55:47 +0100 | [diff] [blame] | 1251 | |
Richard Genoud | 385d942 | 2013-03-29 10:03:27 +0100 | [diff] [blame] | 1252 | /* There's no infinite recursive loop here because p->state is NULL */ |
| 1253 | if (old_state) |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1254 | pinctrl_select_state(p, old_state); |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1255 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1256 | return ret; |
| 1257 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1258 | EXPORT_SYMBOL_GPL(pinctrl_select_state); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1259 | |
Stephen Warren | 6d4ca1f | 2012-04-16 10:51:00 -0600 | [diff] [blame] | 1260 | static void devm_pinctrl_release(struct device *dev, void *res) |
| 1261 | { |
| 1262 | pinctrl_put(*(struct pinctrl **)res); |
| 1263 | } |
| 1264 | |
| 1265 | /** |
| 1266 | * struct devm_pinctrl_get() - Resource managed pinctrl_get() |
| 1267 | * @dev: the device to obtain the handle for |
| 1268 | * |
| 1269 | * If there is a need to explicitly destroy the returned struct pinctrl, |
| 1270 | * devm_pinctrl_put() should be used, rather than plain pinctrl_put(). |
| 1271 | */ |
| 1272 | struct pinctrl *devm_pinctrl_get(struct device *dev) |
| 1273 | { |
| 1274 | struct pinctrl **ptr, *p; |
| 1275 | |
| 1276 | ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL); |
| 1277 | if (!ptr) |
| 1278 | return ERR_PTR(-ENOMEM); |
| 1279 | |
| 1280 | p = pinctrl_get(dev); |
| 1281 | if (!IS_ERR(p)) { |
| 1282 | *ptr = p; |
| 1283 | devres_add(dev, ptr); |
| 1284 | } else { |
| 1285 | devres_free(ptr); |
| 1286 | } |
| 1287 | |
| 1288 | return p; |
| 1289 | } |
| 1290 | EXPORT_SYMBOL_GPL(devm_pinctrl_get); |
| 1291 | |
| 1292 | static int devm_pinctrl_match(struct device *dev, void *res, void *data) |
| 1293 | { |
| 1294 | struct pinctrl **p = res; |
| 1295 | |
| 1296 | return *p == data; |
| 1297 | } |
| 1298 | |
| 1299 | /** |
| 1300 | * devm_pinctrl_put() - Resource managed pinctrl_put() |
| 1301 | * @p: the pinctrl handle to release |
| 1302 | * |
| 1303 | * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally |
| 1304 | * this function will not need to be called and the resource management |
| 1305 | * code will ensure that the resource is freed. |
| 1306 | */ |
| 1307 | void devm_pinctrl_put(struct pinctrl *p) |
| 1308 | { |
Jingoo Han | a72149e | 2013-02-26 11:34:07 +0900 | [diff] [blame] | 1309 | WARN_ON(devres_release(p->dev, devm_pinctrl_release, |
Stephen Warren | 6d4ca1f | 2012-04-16 10:51:00 -0600 | [diff] [blame] | 1310 | devm_pinctrl_match, p)); |
Stephen Warren | 6d4ca1f | 2012-04-16 10:51:00 -0600 | [diff] [blame] | 1311 | } |
| 1312 | EXPORT_SYMBOL_GPL(devm_pinctrl_put); |
| 1313 | |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 1314 | int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps, |
Doug Anderson | c5272a2 | 2015-05-01 09:01:27 -0700 | [diff] [blame] | 1315 | bool dup) |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1316 | { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 1317 | int i, ret; |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 1318 | struct pinctrl_maps *maps_node; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1319 | |
Masahiro Yamada | 7e9236f | 2015-05-28 21:52:59 +0900 | [diff] [blame] | 1320 | pr_debug("add %u pinctrl maps\n", num_maps); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1321 | |
| 1322 | /* First sanity check the new mapping */ |
| 1323 | for (i = 0; i < num_maps; i++) { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 1324 | if (!maps[i].dev_name) { |
| 1325 | pr_err("failed to register map %s (%d): no device given\n", |
| 1326 | maps[i].name, i); |
| 1327 | return -EINVAL; |
| 1328 | } |
| 1329 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1330 | if (!maps[i].name) { |
| 1331 | pr_err("failed to register map %d: no map name given\n", |
Stephen Warren | 95dcd4a | 2012-02-22 14:25:59 -0700 | [diff] [blame] | 1332 | i); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1333 | return -EINVAL; |
| 1334 | } |
| 1335 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 1336 | if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE && |
| 1337 | !maps[i].ctrl_dev_name) { |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1338 | pr_err("failed to register map %s (%d): no pin control device given\n", |
| 1339 | maps[i].name, i); |
| 1340 | return -EINVAL; |
| 1341 | } |
| 1342 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 1343 | switch (maps[i].type) { |
| 1344 | case PIN_MAP_TYPE_DUMMY_STATE: |
| 1345 | break; |
| 1346 | case PIN_MAP_TYPE_MUX_GROUP: |
| 1347 | ret = pinmux_validate_map(&maps[i], i); |
| 1348 | if (ret < 0) |
Stephen Warren | fde04f4 | 2012-04-25 10:32:16 -0600 | [diff] [blame] | 1349 | return ret; |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 1350 | break; |
| 1351 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 1352 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 1353 | ret = pinconf_validate_map(&maps[i], i); |
| 1354 | if (ret < 0) |
Stephen Warren | fde04f4 | 2012-04-25 10:32:16 -0600 | [diff] [blame] | 1355 | return ret; |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 1356 | break; |
| 1357 | default: |
| 1358 | pr_err("failed to register map %s (%d): invalid type given\n", |
Stephen Warren | 95dcd4a | 2012-02-22 14:25:59 -0700 | [diff] [blame] | 1359 | maps[i].name, i); |
Stephen Warren | 1681f5a | 2012-02-22 14:25:58 -0700 | [diff] [blame] | 1360 | return -EINVAL; |
| 1361 | } |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1362 | } |
| 1363 | |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 1364 | maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL); |
Bjorn Andersson | 2104d12 | 2017-01-05 08:07:55 -0800 | [diff] [blame] | 1365 | if (!maps_node) |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 1366 | return -ENOMEM; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1367 | |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 1368 | maps_node->num_maps = num_maps; |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 1369 | if (dup) { |
| 1370 | maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps, |
| 1371 | GFP_KERNEL); |
| 1372 | if (!maps_node->maps) { |
| 1373 | pr_err("failed to duplicate mapping table\n"); |
| 1374 | kfree(maps_node); |
| 1375 | return -ENOMEM; |
| 1376 | } |
| 1377 | } else { |
| 1378 | maps_node->maps = maps; |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
Doug Anderson | c5272a2 | 2015-05-01 09:01:27 -0700 | [diff] [blame] | 1381 | mutex_lock(&pinctrl_maps_mutex); |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 1382 | list_add_tail(&maps_node->node, &pinctrl_maps); |
Doug Anderson | c5272a2 | 2015-05-01 09:01:27 -0700 | [diff] [blame] | 1383 | mutex_unlock(&pinctrl_maps_mutex); |
Stephen Warren | b2b3e66 | 2012-02-19 23:45:43 -0700 | [diff] [blame] | 1384 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1385 | return 0; |
| 1386 | } |
| 1387 | |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 1388 | /** |
| 1389 | * pinctrl_register_mappings() - register a set of pin controller mappings |
| 1390 | * @maps: the pincontrol mappings table to register. This should probably be |
| 1391 | * marked with __initdata so it can be discarded after boot. This |
| 1392 | * function will perform a shallow copy for the mapping entries. |
| 1393 | * @num_maps: the number of maps in the mapping table |
| 1394 | */ |
| 1395 | int pinctrl_register_mappings(struct pinctrl_map const *maps, |
| 1396 | unsigned num_maps) |
| 1397 | { |
Doug Anderson | c5272a2 | 2015-05-01 09:01:27 -0700 | [diff] [blame] | 1398 | return pinctrl_register_map(maps, num_maps, true); |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | void pinctrl_unregister_map(struct pinctrl_map const *map) |
| 1402 | { |
| 1403 | struct pinctrl_maps *maps_node; |
| 1404 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1405 | mutex_lock(&pinctrl_maps_mutex); |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 1406 | list_for_each_entry(maps_node, &pinctrl_maps, node) { |
| 1407 | if (maps_node->maps == map) { |
| 1408 | list_del(&maps_node->node); |
Linus Walleij | db6c2c6 | 2013-07-23 01:43:20 +0200 | [diff] [blame] | 1409 | kfree(maps_node); |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1410 | mutex_unlock(&pinctrl_maps_mutex); |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 1411 | return; |
| 1412 | } |
| 1413 | } |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1414 | mutex_unlock(&pinctrl_maps_mutex); |
Stephen Warren | 57291ce | 2012-03-23 10:29:46 -0600 | [diff] [blame] | 1415 | } |
| 1416 | |
Julien Delacou | 840a47b | 2012-12-10 14:47:33 +0100 | [diff] [blame] | 1417 | /** |
| 1418 | * pinctrl_force_sleep() - turn a given controller device into sleep state |
| 1419 | * @pctldev: pin controller device |
| 1420 | */ |
| 1421 | int pinctrl_force_sleep(struct pinctrl_dev *pctldev) |
| 1422 | { |
| 1423 | if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep)) |
| 1424 | return pinctrl_select_state(pctldev->p, pctldev->hog_sleep); |
| 1425 | return 0; |
| 1426 | } |
| 1427 | EXPORT_SYMBOL_GPL(pinctrl_force_sleep); |
| 1428 | |
| 1429 | /** |
| 1430 | * pinctrl_force_default() - turn a given controller device into default state |
| 1431 | * @pctldev: pin controller device |
| 1432 | */ |
| 1433 | int pinctrl_force_default(struct pinctrl_dev *pctldev) |
| 1434 | { |
| 1435 | if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default)) |
| 1436 | return pinctrl_select_state(pctldev->p, pctldev->hog_default); |
| 1437 | return 0; |
| 1438 | } |
| 1439 | EXPORT_SYMBOL_GPL(pinctrl_force_default); |
| 1440 | |
Douglas Anderson | ef0eebc | 2015-10-20 21:15:06 -0700 | [diff] [blame] | 1441 | /** |
| 1442 | * pinctrl_init_done() - tell pinctrl probe is done |
| 1443 | * |
| 1444 | * We'll use this time to switch the pins from "init" to "default" unless the |
| 1445 | * driver selected some other state. |
| 1446 | * |
| 1447 | * @dev: device to that's done probing |
| 1448 | */ |
| 1449 | int pinctrl_init_done(struct device *dev) |
| 1450 | { |
| 1451 | struct dev_pin_info *pins = dev->pins; |
| 1452 | int ret; |
| 1453 | |
| 1454 | if (!pins) |
| 1455 | return 0; |
| 1456 | |
| 1457 | if (IS_ERR(pins->init_state)) |
| 1458 | return 0; /* No such state */ |
| 1459 | |
| 1460 | if (pins->p->state != pins->init_state) |
| 1461 | return 0; /* Not at init anyway */ |
| 1462 | |
| 1463 | if (IS_ERR(pins->default_state)) |
| 1464 | return 0; /* No default state */ |
| 1465 | |
| 1466 | ret = pinctrl_select_state(pins->p, pins->default_state); |
| 1467 | if (ret) |
| 1468 | dev_err(dev, "failed to activate default pinctrl state\n"); |
| 1469 | |
| 1470 | return ret; |
| 1471 | } |
| 1472 | |
Linus Walleij | 14005ee | 2013-06-05 15:30:33 +0200 | [diff] [blame] | 1473 | #ifdef CONFIG_PM |
| 1474 | |
| 1475 | /** |
Tony Lindgren | f333349 | 2013-07-18 08:15:04 -0700 | [diff] [blame] | 1476 | * pinctrl_pm_select_state() - select pinctrl state for PM |
| 1477 | * @dev: device to select default state for |
| 1478 | * @state: state to set |
| 1479 | */ |
| 1480 | static int pinctrl_pm_select_state(struct device *dev, |
| 1481 | struct pinctrl_state *state) |
| 1482 | { |
| 1483 | struct dev_pin_info *pins = dev->pins; |
| 1484 | int ret; |
| 1485 | |
| 1486 | if (IS_ERR(state)) |
| 1487 | return 0; /* No such state */ |
| 1488 | ret = pinctrl_select_state(pins->p, state); |
| 1489 | if (ret) |
| 1490 | dev_err(dev, "failed to activate pinctrl state %s\n", |
| 1491 | state->name); |
| 1492 | return ret; |
| 1493 | } |
| 1494 | |
| 1495 | /** |
Linus Walleij | 14005ee | 2013-06-05 15:30:33 +0200 | [diff] [blame] | 1496 | * pinctrl_pm_select_default_state() - select default pinctrl state for PM |
| 1497 | * @dev: device to select default state for |
| 1498 | */ |
| 1499 | int pinctrl_pm_select_default_state(struct device *dev) |
| 1500 | { |
Tony Lindgren | f333349 | 2013-07-18 08:15:04 -0700 | [diff] [blame] | 1501 | if (!dev->pins) |
Linus Walleij | 14005ee | 2013-06-05 15:30:33 +0200 | [diff] [blame] | 1502 | return 0; |
Tony Lindgren | f333349 | 2013-07-18 08:15:04 -0700 | [diff] [blame] | 1503 | |
| 1504 | return pinctrl_pm_select_state(dev, dev->pins->default_state); |
Linus Walleij | 14005ee | 2013-06-05 15:30:33 +0200 | [diff] [blame] | 1505 | } |
Arnd Bergmann | f472dea | 2013-06-17 17:12:28 +0200 | [diff] [blame] | 1506 | EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state); |
Linus Walleij | 14005ee | 2013-06-05 15:30:33 +0200 | [diff] [blame] | 1507 | |
| 1508 | /** |
| 1509 | * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM |
| 1510 | * @dev: device to select sleep state for |
| 1511 | */ |
| 1512 | int pinctrl_pm_select_sleep_state(struct device *dev) |
| 1513 | { |
Tony Lindgren | f333349 | 2013-07-18 08:15:04 -0700 | [diff] [blame] | 1514 | if (!dev->pins) |
Linus Walleij | 14005ee | 2013-06-05 15:30:33 +0200 | [diff] [blame] | 1515 | return 0; |
Tony Lindgren | f333349 | 2013-07-18 08:15:04 -0700 | [diff] [blame] | 1516 | |
| 1517 | return pinctrl_pm_select_state(dev, dev->pins->sleep_state); |
Linus Walleij | 14005ee | 2013-06-05 15:30:33 +0200 | [diff] [blame] | 1518 | } |
Arnd Bergmann | f472dea | 2013-06-17 17:12:28 +0200 | [diff] [blame] | 1519 | EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state); |
Linus Walleij | 14005ee | 2013-06-05 15:30:33 +0200 | [diff] [blame] | 1520 | |
| 1521 | /** |
| 1522 | * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM |
| 1523 | * @dev: device to select idle state for |
| 1524 | */ |
| 1525 | int pinctrl_pm_select_idle_state(struct device *dev) |
| 1526 | { |
Tony Lindgren | f333349 | 2013-07-18 08:15:04 -0700 | [diff] [blame] | 1527 | if (!dev->pins) |
Linus Walleij | 14005ee | 2013-06-05 15:30:33 +0200 | [diff] [blame] | 1528 | return 0; |
Tony Lindgren | f333349 | 2013-07-18 08:15:04 -0700 | [diff] [blame] | 1529 | |
| 1530 | return pinctrl_pm_select_state(dev, dev->pins->idle_state); |
Linus Walleij | 14005ee | 2013-06-05 15:30:33 +0200 | [diff] [blame] | 1531 | } |
Arnd Bergmann | f472dea | 2013-06-17 17:12:28 +0200 | [diff] [blame] | 1532 | EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state); |
Linus Walleij | 14005ee | 2013-06-05 15:30:33 +0200 | [diff] [blame] | 1533 | #endif |
| 1534 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1535 | #ifdef CONFIG_DEBUG_FS |
| 1536 | |
| 1537 | static int pinctrl_pins_show(struct seq_file *s, void *what) |
| 1538 | { |
| 1539 | struct pinctrl_dev *pctldev = s->private; |
| 1540 | const struct pinctrl_ops *ops = pctldev->desc->pctlops; |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 1541 | unsigned i, pin; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1542 | |
| 1543 | seq_printf(s, "registered pins: %d\n", pctldev->desc->npins); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1544 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1545 | mutex_lock(&pctldev->mutex); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1546 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 1547 | /* The pin number can be retrived from the pin controller descriptor */ |
| 1548 | for (i = 0; i < pctldev->desc->npins; i++) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1549 | struct pin_desc *desc; |
| 1550 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 1551 | pin = pctldev->desc->pins[i].number; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1552 | desc = pin_desc_get(pctldev, pin); |
| 1553 | /* Pin space may be sparse */ |
| 1554 | if (desc == NULL) |
| 1555 | continue; |
| 1556 | |
Masahiro Yamada | cf9d994 | 2016-05-24 14:26:26 +0900 | [diff] [blame] | 1557 | seq_printf(s, "pin %d (%s) ", pin, desc->name); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1558 | |
| 1559 | /* Driver-specific info per pin */ |
| 1560 | if (ops->pin_dbg_show) |
| 1561 | ops->pin_dbg_show(pctldev, s, pin); |
| 1562 | |
| 1563 | seq_puts(s, "\n"); |
| 1564 | } |
| 1565 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1566 | mutex_unlock(&pctldev->mutex); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1567 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1568 | return 0; |
| 1569 | } |
| 1570 | |
| 1571 | static int pinctrl_groups_show(struct seq_file *s, void *what) |
| 1572 | { |
| 1573 | struct pinctrl_dev *pctldev = s->private; |
| 1574 | const struct pinctrl_ops *ops = pctldev->desc->pctlops; |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 1575 | unsigned ngroups, selector = 0; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1576 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1577 | mutex_lock(&pctldev->mutex); |
| 1578 | |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 1579 | ngroups = ops->get_groups_count(pctldev); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1580 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1581 | seq_puts(s, "registered pin groups:\n"); |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 1582 | while (selector < ngroups) { |
Antoine Ténart | e5b3b2d | 2014-04-10 15:07:50 +0200 | [diff] [blame] | 1583 | const unsigned *pins = NULL; |
| 1584 | unsigned num_pins = 0; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1585 | const char *gname = ops->get_group_name(pctldev, selector); |
Dong Aisheng | dcb5dbc | 2012-04-17 15:00:46 +0800 | [diff] [blame] | 1586 | const char *pname; |
Antoine Ténart | e5b3b2d | 2014-04-10 15:07:50 +0200 | [diff] [blame] | 1587 | int ret = 0; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1588 | int i; |
| 1589 | |
Antoine Ténart | e5b3b2d | 2014-04-10 15:07:50 +0200 | [diff] [blame] | 1590 | if (ops->get_group_pins) |
| 1591 | ret = ops->get_group_pins(pctldev, selector, |
| 1592 | &pins, &num_pins); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1593 | if (ret) |
| 1594 | seq_printf(s, "%s [ERROR GETTING PINS]\n", |
| 1595 | gname); |
| 1596 | else { |
Dong Aisheng | dcb5dbc | 2012-04-17 15:00:46 +0800 | [diff] [blame] | 1597 | seq_printf(s, "group: %s\n", gname); |
| 1598 | for (i = 0; i < num_pins; i++) { |
| 1599 | pname = pin_get_name(pctldev, pins[i]); |
Wei Yongjun | b4dd784 | 2012-10-22 12:58:09 +0800 | [diff] [blame] | 1600 | if (WARN_ON(!pname)) { |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1601 | mutex_unlock(&pctldev->mutex); |
Dong Aisheng | dcb5dbc | 2012-04-17 15:00:46 +0800 | [diff] [blame] | 1602 | return -EINVAL; |
Wei Yongjun | b4dd784 | 2012-10-22 12:58:09 +0800 | [diff] [blame] | 1603 | } |
Dong Aisheng | dcb5dbc | 2012-04-17 15:00:46 +0800 | [diff] [blame] | 1604 | seq_printf(s, "pin %d (%s)\n", pins[i], pname); |
| 1605 | } |
| 1606 | seq_puts(s, "\n"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1607 | } |
| 1608 | selector++; |
| 1609 | } |
| 1610 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1611 | mutex_unlock(&pctldev->mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1612 | |
| 1613 | return 0; |
| 1614 | } |
| 1615 | |
| 1616 | static int pinctrl_gpioranges_show(struct seq_file *s, void *what) |
| 1617 | { |
| 1618 | struct pinctrl_dev *pctldev = s->private; |
| 1619 | struct pinctrl_gpio_range *range = NULL; |
| 1620 | |
| 1621 | seq_puts(s, "GPIO ranges handled:\n"); |
| 1622 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1623 | mutex_lock(&pctldev->mutex); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1624 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1625 | /* Loop over the ranges */ |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1626 | list_for_each_entry(range, &pctldev->gpio_ranges, node) { |
Christian Ruppert | c8587ee | 2013-06-13 14:55:31 +0200 | [diff] [blame] | 1627 | if (range->pins) { |
| 1628 | int a; |
| 1629 | seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {", |
| 1630 | range->id, range->name, |
| 1631 | range->base, (range->base + range->npins - 1)); |
| 1632 | for (a = 0; a < range->npins - 1; a++) |
| 1633 | seq_printf(s, "%u, ", range->pins[a]); |
| 1634 | seq_printf(s, "%u}\n", range->pins[a]); |
| 1635 | } |
| 1636 | else |
| 1637 | seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n", |
| 1638 | range->id, range->name, |
| 1639 | range->base, (range->base + range->npins - 1), |
| 1640 | range->pin_base, |
| 1641 | (range->pin_base + range->npins - 1)); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1642 | } |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1643 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1644 | mutex_unlock(&pctldev->mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1645 | |
| 1646 | return 0; |
| 1647 | } |
| 1648 | |
| 1649 | static int pinctrl_devices_show(struct seq_file *s, void *what) |
| 1650 | { |
| 1651 | struct pinctrl_dev *pctldev; |
| 1652 | |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 1653 | seq_puts(s, "name [pinmux] [pinconf]\n"); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1654 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1655 | mutex_lock(&pinctrldev_list_mutex); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1656 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1657 | list_for_each_entry(pctldev, &pinctrldev_list, node) { |
| 1658 | seq_printf(s, "%s ", pctldev->desc->name); |
| 1659 | if (pctldev->desc->pmxops) |
Linus Walleij | ae6b4d8 | 2011-10-19 18:14:33 +0200 | [diff] [blame] | 1660 | seq_puts(s, "yes "); |
| 1661 | else |
| 1662 | seq_puts(s, "no "); |
| 1663 | if (pctldev->desc->confops) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1664 | seq_puts(s, "yes"); |
| 1665 | else |
| 1666 | seq_puts(s, "no"); |
| 1667 | seq_puts(s, "\n"); |
| 1668 | } |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1669 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1670 | mutex_unlock(&pinctrldev_list_mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1671 | |
| 1672 | return 0; |
| 1673 | } |
| 1674 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 1675 | static inline const char *map_type(enum pinctrl_map_type type) |
| 1676 | { |
| 1677 | static const char * const names[] = { |
| 1678 | "INVALID", |
| 1679 | "DUMMY_STATE", |
| 1680 | "MUX_GROUP", |
| 1681 | "CONFIGS_PIN", |
| 1682 | "CONFIGS_GROUP", |
| 1683 | }; |
| 1684 | |
| 1685 | if (type >= ARRAY_SIZE(names)) |
| 1686 | return "UNKNOWN"; |
| 1687 | |
| 1688 | return names[type]; |
| 1689 | } |
| 1690 | |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1691 | static int pinctrl_maps_show(struct seq_file *s, void *what) |
| 1692 | { |
| 1693 | struct pinctrl_maps *maps_node; |
| 1694 | int i; |
| 1695 | struct pinctrl_map const *map; |
| 1696 | |
| 1697 | seq_puts(s, "Pinctrl maps:\n"); |
| 1698 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1699 | mutex_lock(&pinctrl_maps_mutex); |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1700 | for_each_maps(maps_node, i, map) { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 1701 | seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n", |
| 1702 | map->dev_name, map->name, map_type(map->type), |
| 1703 | map->type); |
| 1704 | |
| 1705 | if (map->type != PIN_MAP_TYPE_DUMMY_STATE) |
| 1706 | seq_printf(s, "controlling device %s\n", |
| 1707 | map->ctrl_dev_name); |
| 1708 | |
| 1709 | switch (map->type) { |
| 1710 | case PIN_MAP_TYPE_MUX_GROUP: |
| 1711 | pinmux_show_map(s, map); |
| 1712 | break; |
| 1713 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 1714 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 1715 | pinconf_show_map(s, map); |
| 1716 | break; |
| 1717 | default: |
| 1718 | break; |
| 1719 | } |
| 1720 | |
| 1721 | seq_printf(s, "\n"); |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1722 | } |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1723 | mutex_unlock(&pinctrl_maps_mutex); |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1724 | |
| 1725 | return 0; |
| 1726 | } |
| 1727 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1728 | static int pinctrl_show(struct seq_file *s, void *what) |
| 1729 | { |
| 1730 | struct pinctrl *p; |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1731 | struct pinctrl_state *state; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 1732 | struct pinctrl_setting *setting; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1733 | |
| 1734 | seq_puts(s, "Requested pin control handlers their pinmux maps:\n"); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1735 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1736 | mutex_lock(&pinctrl_list_mutex); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1737 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1738 | list_for_each_entry(p, &pinctrl_list, node) { |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1739 | seq_printf(s, "device: %s current state: %s\n", |
| 1740 | dev_name(p->dev), |
| 1741 | p->state ? p->state->name : "none"); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1742 | |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1743 | list_for_each_entry(state, &p->states, node) { |
| 1744 | seq_printf(s, " state: %s\n", state->name); |
| 1745 | |
| 1746 | list_for_each_entry(setting, &state->settings, node) { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 1747 | struct pinctrl_dev *pctldev = setting->pctldev; |
| 1748 | |
| 1749 | seq_printf(s, " type: %s controller %s ", |
| 1750 | map_type(setting->type), |
| 1751 | pinctrl_dev_get_name(pctldev)); |
| 1752 | |
| 1753 | switch (setting->type) { |
| 1754 | case PIN_MAP_TYPE_MUX_GROUP: |
| 1755 | pinmux_show_setting(s, setting); |
| 1756 | break; |
| 1757 | case PIN_MAP_TYPE_CONFIGS_PIN: |
| 1758 | case PIN_MAP_TYPE_CONFIGS_GROUP: |
| 1759 | pinconf_show_setting(s, setting); |
| 1760 | break; |
| 1761 | default: |
| 1762 | break; |
| 1763 | } |
Stephen Warren | 6e5e959 | 2012-03-02 13:05:47 -0700 | [diff] [blame] | 1764 | } |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1765 | } |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1766 | } |
| 1767 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1768 | mutex_unlock(&pinctrl_list_mutex); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 1769 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1770 | return 0; |
| 1771 | } |
| 1772 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1773 | static int pinctrl_pins_open(struct inode *inode, struct file *file) |
| 1774 | { |
| 1775 | return single_open(file, pinctrl_pins_show, inode->i_private); |
| 1776 | } |
| 1777 | |
| 1778 | static int pinctrl_groups_open(struct inode *inode, struct file *file) |
| 1779 | { |
| 1780 | return single_open(file, pinctrl_groups_show, inode->i_private); |
| 1781 | } |
| 1782 | |
| 1783 | static int pinctrl_gpioranges_open(struct inode *inode, struct file *file) |
| 1784 | { |
| 1785 | return single_open(file, pinctrl_gpioranges_show, inode->i_private); |
| 1786 | } |
| 1787 | |
| 1788 | static int pinctrl_devices_open(struct inode *inode, struct file *file) |
| 1789 | { |
| 1790 | return single_open(file, pinctrl_devices_show, NULL); |
| 1791 | } |
| 1792 | |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1793 | static int pinctrl_maps_open(struct inode *inode, struct file *file) |
| 1794 | { |
| 1795 | return single_open(file, pinctrl_maps_show, NULL); |
| 1796 | } |
| 1797 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1798 | static int pinctrl_open(struct inode *inode, struct file *file) |
| 1799 | { |
| 1800 | return single_open(file, pinctrl_show, NULL); |
| 1801 | } |
| 1802 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1803 | static const struct file_operations pinctrl_pins_ops = { |
| 1804 | .open = pinctrl_pins_open, |
| 1805 | .read = seq_read, |
| 1806 | .llseek = seq_lseek, |
| 1807 | .release = single_release, |
| 1808 | }; |
| 1809 | |
| 1810 | static const struct file_operations pinctrl_groups_ops = { |
| 1811 | .open = pinctrl_groups_open, |
| 1812 | .read = seq_read, |
| 1813 | .llseek = seq_lseek, |
| 1814 | .release = single_release, |
| 1815 | }; |
| 1816 | |
| 1817 | static const struct file_operations pinctrl_gpioranges_ops = { |
| 1818 | .open = pinctrl_gpioranges_open, |
| 1819 | .read = seq_read, |
| 1820 | .llseek = seq_lseek, |
| 1821 | .release = single_release, |
| 1822 | }; |
| 1823 | |
| 1824 | static const struct file_operations pinctrl_devices_ops = { |
| 1825 | .open = pinctrl_devices_open, |
| 1826 | .read = seq_read, |
| 1827 | .llseek = seq_lseek, |
| 1828 | .release = single_release, |
| 1829 | }; |
| 1830 | |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1831 | static const struct file_operations pinctrl_maps_ops = { |
| 1832 | .open = pinctrl_maps_open, |
| 1833 | .read = seq_read, |
| 1834 | .llseek = seq_lseek, |
| 1835 | .release = single_release, |
| 1836 | }; |
| 1837 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1838 | static const struct file_operations pinctrl_ops = { |
| 1839 | .open = pinctrl_open, |
| 1840 | .read = seq_read, |
| 1841 | .llseek = seq_lseek, |
| 1842 | .release = single_release, |
| 1843 | }; |
| 1844 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1845 | static struct dentry *debugfs_root; |
| 1846 | |
| 1847 | static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) |
| 1848 | { |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1849 | struct dentry *device_root; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1850 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1851 | device_root = debugfs_create_dir(dev_name(pctldev->dev), |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1852 | debugfs_root); |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1853 | pctldev->device_root = device_root; |
| 1854 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1855 | if (IS_ERR(device_root) || !device_root) { |
| 1856 | pr_warn("failed to create debugfs directory for %s\n", |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1857 | dev_name(pctldev->dev)); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1858 | return; |
| 1859 | } |
| 1860 | debugfs_create_file("pins", S_IFREG | S_IRUGO, |
| 1861 | device_root, pctldev, &pinctrl_pins_ops); |
| 1862 | debugfs_create_file("pingroups", S_IFREG | S_IRUGO, |
| 1863 | device_root, pctldev, &pinctrl_groups_ops); |
| 1864 | debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO, |
| 1865 | device_root, pctldev, &pinctrl_gpioranges_ops); |
Florian Vaussard | e7f2a44 | 2014-02-05 07:51:22 +0100 | [diff] [blame] | 1866 | if (pctldev->desc->pmxops) |
| 1867 | pinmux_init_device_debugfs(device_root, pctldev); |
| 1868 | if (pctldev->desc->confops) |
| 1869 | pinconf_init_device_debugfs(device_root, pctldev); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1870 | } |
| 1871 | |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1872 | static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev) |
| 1873 | { |
| 1874 | debugfs_remove_recursive(pctldev->device_root); |
| 1875 | } |
| 1876 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1877 | static void pinctrl_init_debugfs(void) |
| 1878 | { |
| 1879 | debugfs_root = debugfs_create_dir("pinctrl", NULL); |
| 1880 | if (IS_ERR(debugfs_root) || !debugfs_root) { |
| 1881 | pr_warn("failed to create debugfs directory\n"); |
| 1882 | debugfs_root = NULL; |
| 1883 | return; |
| 1884 | } |
| 1885 | |
| 1886 | debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO, |
| 1887 | debugfs_root, NULL, &pinctrl_devices_ops); |
Stephen Warren | 3eedb43 | 2012-02-23 17:04:40 -0700 | [diff] [blame] | 1888 | debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO, |
| 1889 | debugfs_root, NULL, &pinctrl_maps_ops); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 1890 | debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO, |
| 1891 | debugfs_root, NULL, &pinctrl_ops); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | #else /* CONFIG_DEBUG_FS */ |
| 1895 | |
| 1896 | static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) |
| 1897 | { |
| 1898 | } |
| 1899 | |
| 1900 | static void pinctrl_init_debugfs(void) |
| 1901 | { |
| 1902 | } |
| 1903 | |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 1904 | static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev) |
| 1905 | { |
| 1906 | } |
| 1907 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1908 | #endif |
| 1909 | |
Stephen Warren | d26bc49 | 2012-03-16 14:54:25 -0600 | [diff] [blame] | 1910 | static int pinctrl_check_ops(struct pinctrl_dev *pctldev) |
| 1911 | { |
| 1912 | const struct pinctrl_ops *ops = pctldev->desc->pctlops; |
| 1913 | |
| 1914 | if (!ops || |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 1915 | !ops->get_groups_count || |
Antoine Ténart | e5b3b2d | 2014-04-10 15:07:50 +0200 | [diff] [blame] | 1916 | !ops->get_group_name) |
Stephen Warren | d26bc49 | 2012-03-16 14:54:25 -0600 | [diff] [blame] | 1917 | return -EINVAL; |
| 1918 | |
| 1919 | return 0; |
| 1920 | } |
| 1921 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1922 | /** |
Tony Lindgren | 950b0d9 | 2017-01-11 14:13:34 -0800 | [diff] [blame] | 1923 | * pinctrl_init_controller() - init a pin controller device |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1924 | * @pctldesc: descriptor for this pin controller |
| 1925 | * @dev: parent device for this pin controller |
| 1926 | * @driver_data: private pin controller data for this pin controller |
| 1927 | */ |
Andy Shevchenko | 0ca4921 | 2017-03-27 14:37:09 +0300 | [diff] [blame] | 1928 | static struct pinctrl_dev * |
| 1929 | pinctrl_init_controller(struct pinctrl_desc *pctldesc, struct device *dev, |
| 1930 | void *driver_data) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1931 | { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1932 | struct pinctrl_dev *pctldev; |
| 1933 | int ret; |
| 1934 | |
Devendra Naga | da9aecb | 2012-06-16 23:43:16 +0530 | [diff] [blame] | 1935 | if (!pctldesc) |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 1936 | return ERR_PTR(-EINVAL); |
Devendra Naga | da9aecb | 2012-06-16 23:43:16 +0530 | [diff] [blame] | 1937 | if (!pctldesc->name) |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 1938 | return ERR_PTR(-EINVAL); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1939 | |
Stephen Warren | 02f5b98 | 2012-02-22 14:26:00 -0700 | [diff] [blame] | 1940 | pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL); |
Bjorn Andersson | 2104d12 | 2017-01-05 08:07:55 -0800 | [diff] [blame] | 1941 | if (!pctldev) |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 1942 | return ERR_PTR(-ENOMEM); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1943 | |
| 1944 | /* Initialize pin control device struct */ |
| 1945 | pctldev->owner = pctldesc->owner; |
| 1946 | pctldev->desc = pctldesc; |
| 1947 | pctldev->driver_data = driver_data; |
| 1948 | INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL); |
Linus Walleij | c033a71 | 2016-12-30 15:04:43 +0100 | [diff] [blame] | 1949 | #ifdef CONFIG_GENERIC_PINCTRL_GROUPS |
Tony Lindgren | c7059c5 | 2016-12-27 09:20:00 -0800 | [diff] [blame] | 1950 | INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL); |
Linus Walleij | c033a71 | 2016-12-30 15:04:43 +0100 | [diff] [blame] | 1951 | #endif |
Tony Lindgren | a76edc8 | 2016-12-27 09:20:01 -0800 | [diff] [blame] | 1952 | #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS |
| 1953 | INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL); |
| 1954 | #endif |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1955 | INIT_LIST_HEAD(&pctldev->gpio_ranges); |
Thierry Reding | 46daed6 | 2017-01-12 17:03:34 +0100 | [diff] [blame] | 1956 | INIT_LIST_HEAD(&pctldev->node); |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1957 | pctldev->dev = dev; |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1958 | mutex_init(&pctldev->mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1959 | |
Stephen Warren | d26bc49 | 2012-03-16 14:54:25 -0600 | [diff] [blame] | 1960 | /* check core ops for sanity */ |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 1961 | ret = pinctrl_check_ops(pctldev); |
| 1962 | if (ret) { |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 1963 | dev_err(dev, "pinctrl ops lacks necessary functions\n"); |
Stephen Warren | d26bc49 | 2012-03-16 14:54:25 -0600 | [diff] [blame] | 1964 | goto out_err; |
| 1965 | } |
| 1966 | |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 1967 | /* If we're implementing pinmuxing, check the ops for sanity */ |
| 1968 | if (pctldesc->pmxops) { |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 1969 | ret = pinmux_check_ops(pctldev); |
| 1970 | if (ret) |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 1971 | goto out_err; |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | /* If we're implementing pinconfig, check the ops for sanity */ |
| 1975 | if (pctldesc->confops) { |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 1976 | ret = pinconf_check_ops(pctldev); |
| 1977 | if (ret) |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 1978 | goto out_err; |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 1979 | } |
| 1980 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1981 | /* Register all the pins */ |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 1982 | dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1983 | ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins); |
| 1984 | if (ret) { |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 1985 | dev_err(dev, "error during pin registration\n"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1986 | pinctrl_free_pindescs(pctldev, pctldesc->pins, |
| 1987 | pctldesc->npins); |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1988 | goto out_err; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1989 | } |
| 1990 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1991 | return pctldev; |
| 1992 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1993 | out_err: |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 1994 | mutex_destroy(&pctldev->mutex); |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 1995 | kfree(pctldev); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 1996 | return ERR_PTR(ret); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1997 | } |
Tony Lindgren | 950b0d9 | 2017-01-11 14:13:34 -0800 | [diff] [blame] | 1998 | |
Tony Lindgren | 6118714 | 2017-03-30 09:16:39 -0700 | [diff] [blame] | 1999 | static int pinctrl_claim_hogs(struct pinctrl_dev *pctldev) |
Tony Lindgren | 950b0d9 | 2017-01-11 14:13:34 -0800 | [diff] [blame] | 2000 | { |
| 2001 | pctldev->p = create_pinctrl(pctldev->dev, pctldev); |
Tony Lindgren | 6118714 | 2017-03-30 09:16:39 -0700 | [diff] [blame] | 2002 | if (PTR_ERR(pctldev->p) == -ENODEV) { |
| 2003 | dev_dbg(pctldev->dev, "no hogs found\n"); |
Tony Lindgren | 950b0d9 | 2017-01-11 14:13:34 -0800 | [diff] [blame] | 2004 | |
Tony Lindgren | 6118714 | 2017-03-30 09:16:39 -0700 | [diff] [blame] | 2005 | return 0; |
| 2006 | } |
| 2007 | |
| 2008 | if (IS_ERR(pctldev->p)) { |
| 2009 | dev_err(pctldev->dev, "error claiming hogs: %li\n", |
| 2010 | PTR_ERR(pctldev->p)); |
| 2011 | |
| 2012 | return PTR_ERR(pctldev->p); |
| 2013 | } |
| 2014 | |
| 2015 | kref_get(&pctldev->p->users); |
| 2016 | pctldev->hog_default = |
| 2017 | pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT); |
| 2018 | if (IS_ERR(pctldev->hog_default)) { |
| 2019 | dev_dbg(pctldev->dev, |
| 2020 | "failed to lookup the default state\n"); |
| 2021 | } else { |
| 2022 | if (pinctrl_select_state(pctldev->p, |
| 2023 | pctldev->hog_default)) |
| 2024 | dev_err(pctldev->dev, |
| 2025 | "failed to select default state\n"); |
| 2026 | } |
| 2027 | |
| 2028 | pctldev->hog_sleep = |
| 2029 | pinctrl_lookup_state(pctldev->p, |
| 2030 | PINCTRL_STATE_SLEEP); |
| 2031 | if (IS_ERR(pctldev->hog_sleep)) |
| 2032 | dev_dbg(pctldev->dev, |
| 2033 | "failed to lookup the sleep state\n"); |
| 2034 | |
| 2035 | return 0; |
| 2036 | } |
| 2037 | |
| 2038 | int pinctrl_enable(struct pinctrl_dev *pctldev) |
| 2039 | { |
| 2040 | int error; |
| 2041 | |
| 2042 | error = pinctrl_claim_hogs(pctldev); |
| 2043 | if (error) { |
| 2044 | dev_err(pctldev->dev, "could not claim hogs: %i\n", |
| 2045 | error); |
| 2046 | mutex_destroy(&pctldev->mutex); |
| 2047 | kfree(pctldev); |
| 2048 | |
| 2049 | return error; |
Tony Lindgren | 950b0d9 | 2017-01-11 14:13:34 -0800 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | mutex_lock(&pinctrldev_list_mutex); |
| 2053 | list_add_tail(&pctldev->node, &pinctrldev_list); |
| 2054 | mutex_unlock(&pinctrldev_list_mutex); |
| 2055 | |
| 2056 | pinctrl_init_device_debugfs(pctldev); |
| 2057 | |
| 2058 | return 0; |
| 2059 | } |
Tony Lindgren | 6118714 | 2017-03-30 09:16:39 -0700 | [diff] [blame] | 2060 | EXPORT_SYMBOL_GPL(pinctrl_enable); |
Tony Lindgren | 950b0d9 | 2017-01-11 14:13:34 -0800 | [diff] [blame] | 2061 | |
| 2062 | /** |
| 2063 | * pinctrl_register() - register a pin controller device |
| 2064 | * @pctldesc: descriptor for this pin controller |
| 2065 | * @dev: parent device for this pin controller |
| 2066 | * @driver_data: private pin controller data for this pin controller |
| 2067 | * |
| 2068 | * Note that pinctrl_register() is known to have problems as the pin |
| 2069 | * controller driver functions are called before the driver has a |
| 2070 | * struct pinctrl_dev handle. To avoid issues later on, please use the |
| 2071 | * new pinctrl_register_and_init() below instead. |
| 2072 | */ |
| 2073 | struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc, |
| 2074 | struct device *dev, void *driver_data) |
| 2075 | { |
| 2076 | struct pinctrl_dev *pctldev; |
| 2077 | int error; |
| 2078 | |
| 2079 | pctldev = pinctrl_init_controller(pctldesc, dev, driver_data); |
| 2080 | if (IS_ERR(pctldev)) |
| 2081 | return pctldev; |
| 2082 | |
Tony Lindgren | 6118714 | 2017-03-30 09:16:39 -0700 | [diff] [blame] | 2083 | error = pinctrl_enable(pctldev); |
| 2084 | if (error) |
Tony Lindgren | 950b0d9 | 2017-01-11 14:13:34 -0800 | [diff] [blame] | 2085 | return ERR_PTR(error); |
Tony Lindgren | 950b0d9 | 2017-01-11 14:13:34 -0800 | [diff] [blame] | 2086 | |
| 2087 | return pctldev; |
| 2088 | |
| 2089 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 2090 | EXPORT_SYMBOL_GPL(pinctrl_register); |
| 2091 | |
Tony Lindgren | 6118714 | 2017-03-30 09:16:39 -0700 | [diff] [blame] | 2092 | /** |
| 2093 | * pinctrl_register_and_init() - register and init pin controller device |
| 2094 | * @pctldesc: descriptor for this pin controller |
| 2095 | * @dev: parent device for this pin controller |
| 2096 | * @driver_data: private pin controller data for this pin controller |
| 2097 | * @pctldev: pin controller device |
| 2098 | * |
| 2099 | * Note that pinctrl_enable() still needs to be manually called after |
| 2100 | * this once the driver is ready. |
| 2101 | */ |
Tony Lindgren | 950b0d9 | 2017-01-11 14:13:34 -0800 | [diff] [blame] | 2102 | int pinctrl_register_and_init(struct pinctrl_desc *pctldesc, |
| 2103 | struct device *dev, void *driver_data, |
| 2104 | struct pinctrl_dev **pctldev) |
| 2105 | { |
| 2106 | struct pinctrl_dev *p; |
Tony Lindgren | 950b0d9 | 2017-01-11 14:13:34 -0800 | [diff] [blame] | 2107 | |
| 2108 | p = pinctrl_init_controller(pctldesc, dev, driver_data); |
| 2109 | if (IS_ERR(p)) |
| 2110 | return PTR_ERR(p); |
| 2111 | |
| 2112 | /* |
| 2113 | * We have pinctrl_start() call functions in the pin controller |
| 2114 | * driver with create_pinctrl() for at least dt_node_to_map(). So |
| 2115 | * let's make sure pctldev is properly initialized for the |
| 2116 | * pin controller driver before we do anything. |
| 2117 | */ |
| 2118 | *pctldev = p; |
| 2119 | |
Tony Lindgren | 950b0d9 | 2017-01-11 14:13:34 -0800 | [diff] [blame] | 2120 | return 0; |
| 2121 | } |
| 2122 | EXPORT_SYMBOL_GPL(pinctrl_register_and_init); |
| 2123 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 2124 | /** |
| 2125 | * pinctrl_unregister() - unregister pinmux |
| 2126 | * @pctldev: pin controller to unregister |
| 2127 | * |
| 2128 | * Called by pinmux drivers to unregister a pinmux. |
| 2129 | */ |
| 2130 | void pinctrl_unregister(struct pinctrl_dev *pctldev) |
| 2131 | { |
Dong Aisheng | 5d589b0 | 2012-05-23 21:22:40 +0800 | [diff] [blame] | 2132 | struct pinctrl_gpio_range *range, *n; |
Jon Hunter | 3429fb3 | 2017-01-05 15:52:55 +0000 | [diff] [blame] | 2133 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 2134 | if (pctldev == NULL) |
| 2135 | return; |
| 2136 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 2137 | mutex_lock(&pctldev->mutex); |
Tony Lindgren | 0215716 | 2012-01-20 08:17:22 -0800 | [diff] [blame] | 2138 | pinctrl_remove_device_debugfs(pctldev); |
Jim Lin | db93fac | 2015-01-08 20:25:05 +0800 | [diff] [blame] | 2139 | mutex_unlock(&pctldev->mutex); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 2140 | |
Jon Hunter | 3429fb3 | 2017-01-05 15:52:55 +0000 | [diff] [blame] | 2141 | if (!IS_ERR_OR_NULL(pctldev->p)) |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 2142 | pinctrl_put(pctldev->p); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 2143 | |
Jim Lin | db93fac | 2015-01-08 20:25:05 +0800 | [diff] [blame] | 2144 | mutex_lock(&pinctrldev_list_mutex); |
| 2145 | mutex_lock(&pctldev->mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 2146 | /* TODO: check that no pinmuxes are still active? */ |
Thierry Reding | 46daed6 | 2017-01-12 17:03:34 +0100 | [diff] [blame] | 2147 | list_del(&pctldev->node); |
Tony Lindgren | a76edc8 | 2016-12-27 09:20:01 -0800 | [diff] [blame] | 2148 | pinmux_generic_free_functions(pctldev); |
Tony Lindgren | c7059c5 | 2016-12-27 09:20:00 -0800 | [diff] [blame] | 2149 | pinctrl_generic_free_groups(pctldev); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 2150 | /* Destroy descriptor tree */ |
| 2151 | pinctrl_free_pindescs(pctldev, pctldev->desc->pins, |
| 2152 | pctldev->desc->npins); |
Dong Aisheng | 5d589b0 | 2012-05-23 21:22:40 +0800 | [diff] [blame] | 2153 | /* remove gpio ranges map */ |
| 2154 | list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node) |
| 2155 | list_del(&range->node); |
| 2156 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 2157 | mutex_unlock(&pctldev->mutex); |
| 2158 | mutex_destroy(&pctldev->mutex); |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 2159 | kfree(pctldev); |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 2160 | mutex_unlock(&pinctrldev_list_mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 2161 | } |
| 2162 | EXPORT_SYMBOL_GPL(pinctrl_unregister); |
| 2163 | |
Laxman Dewangan | 80e0f8d | 2016-02-24 14:12:59 +0530 | [diff] [blame] | 2164 | static void devm_pinctrl_dev_release(struct device *dev, void *res) |
| 2165 | { |
| 2166 | struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res; |
| 2167 | |
| 2168 | pinctrl_unregister(pctldev); |
| 2169 | } |
| 2170 | |
| 2171 | static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data) |
| 2172 | { |
| 2173 | struct pctldev **r = res; |
| 2174 | |
Laxman Dewangan | 3024f92 | 2016-02-24 14:44:07 +0530 | [diff] [blame] | 2175 | if (WARN_ON(!r || !*r)) |
Laxman Dewangan | 80e0f8d | 2016-02-24 14:12:59 +0530 | [diff] [blame] | 2176 | return 0; |
| 2177 | |
| 2178 | return *r == data; |
| 2179 | } |
| 2180 | |
| 2181 | /** |
| 2182 | * devm_pinctrl_register() - Resource managed version of pinctrl_register(). |
| 2183 | * @dev: parent device for this pin controller |
| 2184 | * @pctldesc: descriptor for this pin controller |
| 2185 | * @driver_data: private pin controller data for this pin controller |
| 2186 | * |
| 2187 | * Returns an error pointer if pincontrol register failed. Otherwise |
| 2188 | * it returns valid pinctrl handle. |
| 2189 | * |
| 2190 | * The pinctrl device will be automatically released when the device is unbound. |
| 2191 | */ |
| 2192 | struct pinctrl_dev *devm_pinctrl_register(struct device *dev, |
| 2193 | struct pinctrl_desc *pctldesc, |
| 2194 | void *driver_data) |
| 2195 | { |
| 2196 | struct pinctrl_dev **ptr, *pctldev; |
| 2197 | |
| 2198 | ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL); |
| 2199 | if (!ptr) |
| 2200 | return ERR_PTR(-ENOMEM); |
| 2201 | |
| 2202 | pctldev = pinctrl_register(pctldesc, dev, driver_data); |
| 2203 | if (IS_ERR(pctldev)) { |
| 2204 | devres_free(ptr); |
| 2205 | return pctldev; |
| 2206 | } |
| 2207 | |
| 2208 | *ptr = pctldev; |
| 2209 | devres_add(dev, ptr); |
| 2210 | |
| 2211 | return pctldev; |
| 2212 | } |
| 2213 | EXPORT_SYMBOL_GPL(devm_pinctrl_register); |
| 2214 | |
| 2215 | /** |
Tony Lindgren | 950b0d9 | 2017-01-11 14:13:34 -0800 | [diff] [blame] | 2216 | * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init |
| 2217 | * @dev: parent device for this pin controller |
| 2218 | * @pctldesc: descriptor for this pin controller |
| 2219 | * @driver_data: private pin controller data for this pin controller |
| 2220 | * |
| 2221 | * Returns an error pointer if pincontrol register failed. Otherwise |
| 2222 | * it returns valid pinctrl handle. |
| 2223 | * |
| 2224 | * The pinctrl device will be automatically released when the device is unbound. |
| 2225 | */ |
| 2226 | int devm_pinctrl_register_and_init(struct device *dev, |
| 2227 | struct pinctrl_desc *pctldesc, |
| 2228 | void *driver_data, |
| 2229 | struct pinctrl_dev **pctldev) |
| 2230 | { |
| 2231 | struct pinctrl_dev **ptr; |
| 2232 | int error; |
| 2233 | |
| 2234 | ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL); |
| 2235 | if (!ptr) |
| 2236 | return -ENOMEM; |
| 2237 | |
| 2238 | error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev); |
| 2239 | if (error) { |
| 2240 | devres_free(ptr); |
| 2241 | return error; |
| 2242 | } |
| 2243 | |
| 2244 | *ptr = *pctldev; |
| 2245 | devres_add(dev, ptr); |
| 2246 | |
| 2247 | return 0; |
| 2248 | } |
| 2249 | EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init); |
| 2250 | |
| 2251 | /** |
Laxman Dewangan | 80e0f8d | 2016-02-24 14:12:59 +0530 | [diff] [blame] | 2252 | * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister(). |
| 2253 | * @dev: device for which which resource was allocated |
| 2254 | * @pctldev: the pinctrl device to unregister. |
| 2255 | */ |
| 2256 | void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev) |
| 2257 | { |
| 2258 | WARN_ON(devres_release(dev, devm_pinctrl_dev_release, |
| 2259 | devm_pinctrl_dev_match, pctldev)); |
| 2260 | } |
| 2261 | EXPORT_SYMBOL_GPL(devm_pinctrl_unregister); |
| 2262 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 2263 | static int __init pinctrl_init(void) |
| 2264 | { |
| 2265 | pr_info("initialized pinctrl subsystem\n"); |
| 2266 | pinctrl_init_debugfs(); |
| 2267 | return 0; |
| 2268 | } |
| 2269 | |
| 2270 | /* init early since many drivers really need to initialized pinmux early */ |
| 2271 | core_initcall(pinctrl_init); |