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