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