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