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 muxing portions of the pin control subsystem |
| 4 | * |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +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 | 7ecdb16 | 2012-03-02 13:05:45 -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) "pinmux core: " fmt |
| 14 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/radix-tree.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/list.h> |
Linus Walleij | 97607d1 | 2011-11-29 12:52:39 +0100 | [diff] [blame] | 23 | #include <linux/string.h> |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 24 | #include <linux/debugfs.h> |
| 25 | #include <linux/seq_file.h> |
| 26 | #include <linux/pinctrl/machine.h> |
| 27 | #include <linux/pinctrl/pinmux.h> |
| 28 | #include "core.h" |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 29 | #include "pinmux.h" |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 30 | |
Stephen Warren | 03665e0 | 2012-02-19 23:45:45 -0700 | [diff] [blame] | 31 | int pinmux_check_ops(struct pinctrl_dev *pctldev) |
| 32 | { |
| 33 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
Dong Aisheng | a1d31f7 | 2012-04-06 20:18:09 +0800 | [diff] [blame] | 34 | unsigned nfuncs; |
Stephen Warren | 03665e0 | 2012-02-19 23:45:45 -0700 | [diff] [blame] | 35 | unsigned selector = 0; |
| 36 | |
| 37 | /* Check that we implement required operations */ |
Dong Aisheng | a1d31f7 | 2012-04-06 20:18:09 +0800 | [diff] [blame] | 38 | if (!ops || |
| 39 | !ops->get_functions_count || |
Stephen Warren | 03665e0 | 2012-02-19 23:45:45 -0700 | [diff] [blame] | 40 | !ops->get_function_name || |
| 41 | !ops->get_function_groups || |
Linus Walleij | 03e9f0c | 2014-09-03 13:02:56 +0200 | [diff] [blame] | 42 | !ops->set_mux) { |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 43 | dev_err(pctldev->dev, "pinmux ops lacks necessary functions\n"); |
Stephen Warren | 03665e0 | 2012-02-19 23:45:45 -0700 | [diff] [blame] | 44 | return -EINVAL; |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 45 | } |
Stephen Warren | 03665e0 | 2012-02-19 23:45:45 -0700 | [diff] [blame] | 46 | /* Check that all functions registered have names */ |
Dong Aisheng | a1d31f7 | 2012-04-06 20:18:09 +0800 | [diff] [blame] | 47 | nfuncs = ops->get_functions_count(pctldev); |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 48 | while (selector < nfuncs) { |
Stephen Warren | 03665e0 | 2012-02-19 23:45:45 -0700 | [diff] [blame] | 49 | const char *fname = ops->get_function_name(pctldev, |
| 50 | selector); |
| 51 | if (!fname) { |
Dong Aisheng | a1d31f7 | 2012-04-06 20:18:09 +0800 | [diff] [blame] | 52 | dev_err(pctldev->dev, "pinmux ops has no name for function%u\n", |
Stephen Warren | 03665e0 | 2012-02-19 23:45:45 -0700 | [diff] [blame] | 53 | selector); |
| 54 | return -EINVAL; |
| 55 | } |
| 56 | selector++; |
| 57 | } |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Masahiro Yamada | 3f713b7 | 2017-08-04 11:22:31 +0900 | [diff] [blame] | 62 | int pinmux_validate_map(const struct pinctrl_map *map, int i) |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 63 | { |
| 64 | if (!map->data.mux.function) { |
| 65 | pr_err("failed to register map %s (%d): no function given\n", |
| 66 | map->name, i); |
| 67 | return -EINVAL; |
| 68 | } |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 73 | /** |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 74 | * pin_request() - request a single pin to be muxed in, typically for GPIO |
| 75 | * @pin: the pin number in the global pin space |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 76 | * @owner: a representation of the owner of this pin; typically the device |
| 77 | * name that controls its mux function, or the requested GPIO name |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 78 | * @gpio_range: the range matching the GPIO pin if this is a request for a |
| 79 | * single GPIO pin |
| 80 | */ |
| 81 | static int pin_request(struct pinctrl_dev *pctldev, |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 82 | int pin, const char *owner, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 83 | struct pinctrl_gpio_range *gpio_range) |
| 84 | { |
| 85 | struct pin_desc *desc; |
| 86 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 87 | int status = -EINVAL; |
| 88 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 89 | desc = pin_desc_get(pctldev, pin); |
| 90 | if (desc == NULL) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 91 | dev_err(pctldev->dev, |
Stephen Warren | d470531 | 2012-05-01 11:14:15 -0600 | [diff] [blame] | 92 | "pin %d is not registered so it cannot be requested\n", |
| 93 | pin); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 94 | goto out; |
| 95 | } |
| 96 | |
Dong Aisheng | d0bd8df | 2012-04-17 15:00:45 +0800 | [diff] [blame] | 97 | dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n", |
| 98 | pin, desc->name, owner); |
| 99 | |
Vladimir Zapolskiy | b1eb8fa | 2016-12-25 02:59:28 +0200 | [diff] [blame] | 100 | if ((!gpio_range || ops->strict) && |
| 101 | desc->mux_usecount && strcmp(desc->mux_owner, owner)) { |
| 102 | dev_err(pctldev->dev, |
| 103 | "pin %s already requested by %s; cannot claim for %s\n", |
| 104 | desc->name, desc->mux_owner, owner); |
| 105 | goto out; |
| 106 | } |
Stephen Warren | 652162d | 2012-03-05 17:22:15 -0700 | [diff] [blame] | 107 | |
Vladimir Zapolskiy | b1eb8fa | 2016-12-25 02:59:28 +0200 | [diff] [blame] | 108 | if ((gpio_range || ops->strict) && desc->gpio_owner) { |
| 109 | dev_err(pctldev->dev, |
| 110 | "pin %s already requested by %s; cannot claim for %s\n", |
| 111 | desc->name, desc->gpio_owner, owner); |
| 112 | goto out; |
| 113 | } |
| 114 | |
| 115 | if (gpio_range) { |
Stephen Warren | 652162d | 2012-03-05 17:22:15 -0700 | [diff] [blame] | 116 | desc->gpio_owner = owner; |
| 117 | } else { |
Stephen Warren | 652162d | 2012-03-05 17:22:15 -0700 | [diff] [blame] | 118 | desc->mux_usecount++; |
| 119 | if (desc->mux_usecount > 1) |
| 120 | return 0; |
| 121 | |
| 122 | desc->mux_owner = owner; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 123 | } |
Stephen Warren | 0e3db173 | 2012-03-02 13:05:46 -0700 | [diff] [blame] | 124 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 125 | /* Let each pin increase references to this module */ |
| 126 | if (!try_module_get(pctldev->owner)) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 127 | dev_err(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 128 | "could not increase module refcount for pin %d\n", |
| 129 | pin); |
| 130 | status = -EINVAL; |
| 131 | goto out_free_pin; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * If there is no kind of request function for the pin we just assume |
| 136 | * we got it by default and proceed. |
| 137 | */ |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 138 | if (gpio_range && ops->gpio_request_enable) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 139 | /* This requests and enables a single GPIO pin */ |
| 140 | status = ops->gpio_request_enable(pctldev, gpio_range, pin); |
| 141 | else if (ops->request) |
| 142 | status = ops->request(pctldev, pin); |
| 143 | else |
| 144 | status = 0; |
| 145 | |
Stephen Warren | 0e3db173 | 2012-03-02 13:05:46 -0700 | [diff] [blame] | 146 | if (status) { |
Stephen Warren | d470531 | 2012-05-01 11:14:15 -0600 | [diff] [blame] | 147 | dev_err(pctldev->dev, "request() failed for pin %d\n", pin); |
Stephen Warren | 0e3db173 | 2012-03-02 13:05:46 -0700 | [diff] [blame] | 148 | module_put(pctldev->owner); |
| 149 | } |
| 150 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 151 | out_free_pin: |
Stephen Warren | 0e3db173 | 2012-03-02 13:05:46 -0700 | [diff] [blame] | 152 | if (status) { |
Stephen Warren | 652162d | 2012-03-05 17:22:15 -0700 | [diff] [blame] | 153 | if (gpio_range) { |
| 154 | desc->gpio_owner = NULL; |
| 155 | } else { |
| 156 | desc->mux_usecount--; |
| 157 | if (!desc->mux_usecount) |
| 158 | desc->mux_owner = NULL; |
| 159 | } |
Stephen Warren | 0e3db173 | 2012-03-02 13:05:46 -0700 | [diff] [blame] | 160 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 161 | out: |
| 162 | if (status) |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 163 | dev_err(pctldev->dev, "pin-%d (%s) status %d\n", |
Stephen Warren | d470531 | 2012-05-01 11:14:15 -0600 | [diff] [blame] | 164 | pin, owner, status); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 165 | |
| 166 | return status; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * pin_free() - release a single muxed in pin so something else can be muxed |
| 171 | * @pctldev: pin controller device handling this pin |
| 172 | * @pin: the pin to free |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 173 | * @gpio_range: the range matching the GPIO pin if this is a request for a |
| 174 | * single GPIO pin |
Linus Walleij | 336cdba0 | 2011-11-10 09:27:41 +0100 | [diff] [blame] | 175 | * |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 176 | * This function returns a pointer to the previous owner. This is used |
| 177 | * for callers that dynamically allocate an owner name so it can be freed |
Linus Walleij | 336cdba0 | 2011-11-10 09:27:41 +0100 | [diff] [blame] | 178 | * once the pin is free. This is done for GPIO request functions. |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 179 | */ |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 180 | static const char *pin_free(struct pinctrl_dev *pctldev, int pin, |
| 181 | struct pinctrl_gpio_range *gpio_range) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 182 | { |
| 183 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 184 | struct pin_desc *desc; |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 185 | const char *owner; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 186 | |
| 187 | desc = pin_desc_get(pctldev, pin); |
| 188 | if (desc == NULL) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 189 | dev_err(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 190 | "pin is not registered so it cannot be freed\n"); |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 191 | return NULL; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Stephen Warren | 652162d | 2012-03-05 17:22:15 -0700 | [diff] [blame] | 194 | if (!gpio_range) { |
Richard Genoud | 740924a | 2013-03-21 12:21:47 +0100 | [diff] [blame] | 195 | /* |
| 196 | * A pin should not be freed more times than allocated. |
| 197 | */ |
| 198 | if (WARN_ON(!desc->mux_usecount)) |
| 199 | return NULL; |
Stephen Warren | 652162d | 2012-03-05 17:22:15 -0700 | [diff] [blame] | 200 | desc->mux_usecount--; |
| 201 | if (desc->mux_usecount) |
| 202 | return NULL; |
| 203 | } |
Stephen Warren | 0e3db173 | 2012-03-02 13:05:46 -0700 | [diff] [blame] | 204 | |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 205 | /* |
| 206 | * If there is no kind of request function for the pin we just assume |
| 207 | * we got it by default and proceed. |
| 208 | */ |
| 209 | if (gpio_range && ops->gpio_disable_free) |
| 210 | ops->gpio_disable_free(pctldev, gpio_range, pin); |
| 211 | else if (ops->free) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 212 | ops->free(pctldev, pin); |
| 213 | |
Stephen Warren | 652162d | 2012-03-05 17:22:15 -0700 | [diff] [blame] | 214 | if (gpio_range) { |
| 215 | owner = desc->gpio_owner; |
| 216 | desc->gpio_owner = NULL; |
| 217 | } else { |
| 218 | owner = desc->mux_owner; |
| 219 | desc->mux_owner = NULL; |
| 220 | desc->mux_setting = NULL; |
| 221 | } |
| 222 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 223 | module_put(pctldev->owner); |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 224 | |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 225 | return owner; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /** |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 229 | * pinmux_request_gpio() - request pinmuxing for a GPIO pin |
| 230 | * @pctldev: pin controller device affected |
| 231 | * @pin: the pin to mux in for GPIO |
| 232 | * @range: the applicable GPIO range |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 233 | */ |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 234 | int pinmux_request_gpio(struct pinctrl_dev *pctldev, |
| 235 | struct pinctrl_gpio_range *range, |
| 236 | unsigned pin, unsigned gpio) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 237 | { |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 238 | const char *owner; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 239 | int ret; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 240 | |
| 241 | /* Conjure some name stating what chip and pin this is taken by */ |
Thomas Petazzoni | 23a895a | 2012-09-13 21:48:14 +0200 | [diff] [blame] | 242 | owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio); |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 243 | if (!owner) |
Masahiro Yamada | 1fb1f05 | 2016-05-25 20:14:13 +0900 | [diff] [blame] | 244 | return -ENOMEM; |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 245 | |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 246 | ret = pin_request(pctldev, pin, owner, range); |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 247 | if (ret < 0) |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 248 | kfree(owner); |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 249 | |
| 250 | return ret; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 251 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 252 | |
| 253 | /** |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 254 | * pinmux_free_gpio() - release a pin from GPIO muxing |
| 255 | * @pctldev: the pin controller device for the pin |
| 256 | * @pin: the affected currently GPIO-muxed in pin |
| 257 | * @range: applicable GPIO range |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 258 | */ |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 259 | void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin, |
| 260 | struct pinctrl_gpio_range *range) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 261 | { |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 262 | const char *owner; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 263 | |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 264 | owner = pin_free(pctldev, pin, range); |
| 265 | kfree(owner); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 266 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 267 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 268 | /** |
| 269 | * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin |
| 270 | * @pctldev: the pin controller handling this pin |
| 271 | * @range: applicable GPIO range |
| 272 | * @pin: the affected GPIO pin in this controller |
| 273 | * @input: true if we set the pin as input, false for output |
| 274 | */ |
| 275 | int pinmux_gpio_direction(struct pinctrl_dev *pctldev, |
| 276 | struct pinctrl_gpio_range *range, |
| 277 | unsigned pin, bool input) |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 278 | { |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 279 | const struct pinmux_ops *ops; |
| 280 | int ret; |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 281 | |
| 282 | ops = pctldev->desc->pmxops; |
| 283 | |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 284 | if (ops->gpio_set_direction) |
| 285 | ret = ops->gpio_set_direction(pctldev, range, pin, input); |
| 286 | else |
| 287 | ret = 0; |
| 288 | |
| 289 | return ret; |
| 290 | } |
| 291 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 292 | static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev, |
| 293 | const char *function) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 294 | { |
| 295 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 296 | unsigned nfuncs = ops->get_functions_count(pctldev); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 297 | unsigned selector = 0; |
| 298 | |
| 299 | /* See if this pctldev has this function */ |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 300 | while (selector < nfuncs) { |
Masahiro Yamada | 163dc9f | 2015-08-01 13:22:38 +0900 | [diff] [blame] | 301 | const char *fname = ops->get_function_name(pctldev, selector); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 302 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 303 | if (!strcmp(function, fname)) |
| 304 | return selector; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 305 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 306 | selector++; |
| 307 | } |
| 308 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 309 | return -EINVAL; |
| 310 | } |
| 311 | |
Masahiro Yamada | 3f713b7 | 2017-08-04 11:22:31 +0900 | [diff] [blame] | 312 | int pinmux_map_to_setting(const struct pinctrl_map *map, |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 313 | struct pinctrl_setting *setting) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 314 | { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 315 | struct pinctrl_dev *pctldev = setting->pctldev; |
| 316 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 317 | char const * const *groups; |
| 318 | unsigned num_groups; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 319 | int ret; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 320 | const char *group; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 321 | |
Dong Aisheng | ad8bb72 | 2012-04-10 12:41:34 +0800 | [diff] [blame] | 322 | if (!pmxops) { |
| 323 | dev_err(pctldev->dev, "does not support mux function\n"); |
| 324 | return -EINVAL; |
| 325 | } |
| 326 | |
John Crispin | 15f70e1 | 2012-04-23 19:01:58 +0200 | [diff] [blame] | 327 | ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function); |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 328 | if (ret < 0) { |
| 329 | dev_err(pctldev->dev, "invalid function %s in map table\n", |
| 330 | map->data.mux.function); |
John Crispin | 15f70e1 | 2012-04-23 19:01:58 +0200 | [diff] [blame] | 331 | return ret; |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 332 | } |
John Crispin | 15f70e1 | 2012-04-23 19:01:58 +0200 | [diff] [blame] | 333 | setting->data.mux.func = ret; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 334 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 335 | ret = pmxops->get_function_groups(pctldev, setting->data.mux.func, |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 336 | &groups, &num_groups); |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 337 | if (ret < 0) { |
| 338 | dev_err(pctldev->dev, "can't query groups for function %s\n", |
| 339 | map->data.mux.function); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 340 | return ret; |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 341 | } |
| 342 | if (!num_groups) { |
| 343 | dev_err(pctldev->dev, |
| 344 | "function %s can't be selected on any group\n", |
| 345 | map->data.mux.function); |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 346 | return -EINVAL; |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 347 | } |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 348 | if (map->data.mux.group) { |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 349 | group = map->data.mux.group; |
Andy Shevchenko | dff4359 | 2016-03-17 14:22:20 -0700 | [diff] [blame] | 350 | ret = match_string(groups, num_groups, group); |
| 351 | if (ret < 0) { |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 352 | dev_err(pctldev->dev, |
| 353 | "invalid group \"%s\" for function \"%s\"\n", |
| 354 | group, map->data.mux.function); |
Andy Shevchenko | dff4359 | 2016-03-17 14:22:20 -0700 | [diff] [blame] | 355 | return ret; |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 356 | } |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 357 | } else { |
| 358 | group = groups[0]; |
| 359 | } |
| 360 | |
John Crispin | 15f70e1 | 2012-04-23 19:01:58 +0200 | [diff] [blame] | 361 | ret = pinctrl_get_group_selector(pctldev, group); |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 362 | if (ret < 0) { |
| 363 | dev_err(pctldev->dev, "invalid group %s in map table\n", |
| 364 | map->data.mux.group); |
John Crispin | 15f70e1 | 2012-04-23 19:01:58 +0200 | [diff] [blame] | 365 | return ret; |
John Crispin | ad6e110 | 2012-04-26 16:47:11 +0200 | [diff] [blame] | 366 | } |
John Crispin | 15f70e1 | 2012-04-23 19:01:58 +0200 | [diff] [blame] | 367 | setting->data.mux.group = ret; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 368 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 369 | return 0; |
| 370 | } |
| 371 | |
Masahiro Yamada | 3f713b7 | 2017-08-04 11:22:31 +0900 | [diff] [blame] | 372 | void pinmux_free_setting(const struct pinctrl_setting *setting) |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 373 | { |
Linus Walleij | 1a78958 | 2012-10-17 20:51:54 +0200 | [diff] [blame] | 374 | /* This function is currently unused */ |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 375 | } |
| 376 | |
Masahiro Yamada | 3f713b7 | 2017-08-04 11:22:31 +0900 | [diff] [blame] | 377 | int pinmux_enable_setting(const struct pinctrl_setting *setting) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 378 | { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 379 | struct pinctrl_dev *pctldev = setting->pctldev; |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 380 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 381 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
Antoine Ténart | e5b3b2d | 2014-04-10 15:07:50 +0200 | [diff] [blame] | 382 | int ret = 0; |
| 383 | const unsigned *pins = NULL; |
| 384 | unsigned num_pins = 0; |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 385 | int i; |
| 386 | struct pin_desc *desc; |
| 387 | |
Antoine Ténart | e5b3b2d | 2014-04-10 15:07:50 +0200 | [diff] [blame] | 388 | if (pctlops->get_group_pins) |
| 389 | ret = pctlops->get_group_pins(pctldev, setting->data.mux.group, |
| 390 | &pins, &num_pins); |
| 391 | |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 392 | if (ret) { |
Linus Walleij | 1c8e794 | 2013-08-14 18:23:33 +0200 | [diff] [blame] | 393 | const char *gname; |
| 394 | |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 395 | /* errors only affect debug data, so just warn */ |
Linus Walleij | 1c8e794 | 2013-08-14 18:23:33 +0200 | [diff] [blame] | 396 | gname = pctlops->get_group_name(pctldev, |
| 397 | setting->data.mux.group); |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 398 | dev_warn(pctldev->dev, |
Linus Walleij | 1c8e794 | 2013-08-14 18:23:33 +0200 | [diff] [blame] | 399 | "could not get pins for group %s\n", |
| 400 | gname); |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 401 | num_pins = 0; |
| 402 | } |
| 403 | |
Linus Walleij | 1a78958 | 2012-10-17 20:51:54 +0200 | [diff] [blame] | 404 | /* Try to allocate all pins in this group, one by one */ |
| 405 | for (i = 0; i < num_pins; i++) { |
| 406 | ret = pin_request(pctldev, pins[i], setting->dev_name, NULL); |
| 407 | if (ret) { |
Linus Walleij | 1c8e794 | 2013-08-14 18:23:33 +0200 | [diff] [blame] | 408 | const char *gname; |
| 409 | const char *pname; |
| 410 | |
| 411 | desc = pin_desc_get(pctldev, pins[i]); |
| 412 | pname = desc ? desc->name : "non-existing"; |
| 413 | gname = pctlops->get_group_name(pctldev, |
| 414 | setting->data.mux.group); |
Linus Walleij | 1a78958 | 2012-10-17 20:51:54 +0200 | [diff] [blame] | 415 | dev_err(pctldev->dev, |
Linus Walleij | 1c8e794 | 2013-08-14 18:23:33 +0200 | [diff] [blame] | 416 | "could not request pin %d (%s) from group %s " |
| 417 | " on device %s\n", |
| 418 | pins[i], pname, gname, |
| 419 | pinctrl_dev_get_name(pctldev)); |
Axel Lin | e38d457 | 2012-11-10 21:53:20 +0800 | [diff] [blame] | 420 | goto err_pin_request; |
Linus Walleij | 1a78958 | 2012-10-17 20:51:54 +0200 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | /* Now that we have acquired the pins, encode the mux setting */ |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 425 | for (i = 0; i < num_pins; i++) { |
| 426 | desc = pin_desc_get(pctldev, pins[i]); |
| 427 | if (desc == NULL) { |
| 428 | dev_warn(pctldev->dev, |
| 429 | "could not get pin desc for pin %d\n", |
| 430 | pins[i]); |
| 431 | continue; |
| 432 | } |
| 433 | desc->mux_setting = &(setting->data.mux); |
| 434 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 435 | |
Linus Walleij | 03e9f0c | 2014-09-03 13:02:56 +0200 | [diff] [blame] | 436 | ret = ops->set_mux(pctldev, setting->data.mux.func, |
| 437 | setting->data.mux.group); |
Axel Lin | e38d457 | 2012-11-10 21:53:20 +0800 | [diff] [blame] | 438 | |
| 439 | if (ret) |
Linus Walleij | 03e9f0c | 2014-09-03 13:02:56 +0200 | [diff] [blame] | 440 | goto err_set_mux; |
Axel Lin | e38d457 | 2012-11-10 21:53:20 +0800 | [diff] [blame] | 441 | |
| 442 | return 0; |
| 443 | |
Linus Walleij | 03e9f0c | 2014-09-03 13:02:56 +0200 | [diff] [blame] | 444 | err_set_mux: |
Axel Lin | e38d457 | 2012-11-10 21:53:20 +0800 | [diff] [blame] | 445 | for (i = 0; i < num_pins; i++) { |
| 446 | desc = pin_desc_get(pctldev, pins[i]); |
| 447 | if (desc) |
| 448 | desc->mux_setting = NULL; |
| 449 | } |
| 450 | err_pin_request: |
| 451 | /* On error release all taken pins */ |
| 452 | while (--i >= 0) |
| 453 | pin_free(pctldev, pins[i], NULL); |
| 454 | |
| 455 | return ret; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 456 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 457 | |
Masahiro Yamada | 3f713b7 | 2017-08-04 11:22:31 +0900 | [diff] [blame] | 458 | void pinmux_disable_setting(const struct pinctrl_setting *setting) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 459 | { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 460 | struct pinctrl_dev *pctldev = setting->pctldev; |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 461 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
Antoine Ténart | e5b3b2d | 2014-04-10 15:07:50 +0200 | [diff] [blame] | 462 | int ret = 0; |
| 463 | const unsigned *pins = NULL; |
| 464 | unsigned num_pins = 0; |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 465 | int i; |
| 466 | struct pin_desc *desc; |
| 467 | |
Antoine Ténart | e5b3b2d | 2014-04-10 15:07:50 +0200 | [diff] [blame] | 468 | if (pctlops->get_group_pins) |
| 469 | ret = pctlops->get_group_pins(pctldev, setting->data.mux.group, |
| 470 | &pins, &num_pins); |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 471 | if (ret) { |
Linus Walleij | 1c8e794 | 2013-08-14 18:23:33 +0200 | [diff] [blame] | 472 | const char *gname; |
| 473 | |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 474 | /* errors only affect debug data, so just warn */ |
Linus Walleij | 1c8e794 | 2013-08-14 18:23:33 +0200 | [diff] [blame] | 475 | gname = pctlops->get_group_name(pctldev, |
| 476 | setting->data.mux.group); |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 477 | dev_warn(pctldev->dev, |
Linus Walleij | 1c8e794 | 2013-08-14 18:23:33 +0200 | [diff] [blame] | 478 | "could not get pins for group %s\n", |
| 479 | gname); |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 480 | num_pins = 0; |
| 481 | } |
| 482 | |
Linus Walleij | 1a78958 | 2012-10-17 20:51:54 +0200 | [diff] [blame] | 483 | /* Flag the descs that no setting is active */ |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 484 | for (i = 0; i < num_pins; i++) { |
| 485 | desc = pin_desc_get(pctldev, pins[i]); |
| 486 | if (desc == NULL) { |
| 487 | dev_warn(pctldev->dev, |
| 488 | "could not get pin desc for pin %d\n", |
| 489 | pins[i]); |
| 490 | continue; |
| 491 | } |
Sonic Zhang | 744f0a9 | 2013-08-14 13:26:43 +0800 | [diff] [blame] | 492 | if (desc->mux_setting == &(setting->data.mux)) { |
Sonic Zhang | 744f0a9 | 2013-08-14 13:26:43 +0800 | [diff] [blame] | 493 | pin_free(pctldev, pins[i], NULL); |
Linus Walleij | 1c8e794 | 2013-08-14 18:23:33 +0200 | [diff] [blame] | 494 | } else { |
| 495 | const char *gname; |
Linus Walleij | 1c8e794 | 2013-08-14 18:23:33 +0200 | [diff] [blame] | 496 | |
Linus Walleij | 1c8e794 | 2013-08-14 18:23:33 +0200 | [diff] [blame] | 497 | gname = pctlops->get_group_name(pctldev, |
| 498 | setting->data.mux.group); |
| 499 | dev_warn(pctldev->dev, |
| 500 | "not freeing pin %d (%s) as part of " |
| 501 | "deactivating group %s - it is already " |
| 502 | "used for some other setting", |
Michael Opdenacker | 808e657 | 2013-10-29 04:50:45 +0100 | [diff] [blame] | 503 | pins[i], desc->name, gname); |
Sonic Zhang | 744f0a9 | 2013-08-14 13:26:43 +0800 | [diff] [blame] | 504 | } |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 505 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 506 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 507 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 508 | #ifdef CONFIG_DEBUG_FS |
| 509 | |
| 510 | /* Called from pincontrol core */ |
| 511 | static int pinmux_functions_show(struct seq_file *s, void *what) |
| 512 | { |
| 513 | struct pinctrl_dev *pctldev = s->private; |
| 514 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; |
Dong Aisheng | ad8bb72 | 2012-04-10 12:41:34 +0800 | [diff] [blame] | 515 | unsigned nfuncs; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 516 | unsigned func_selector = 0; |
| 517 | |
Dong Aisheng | ad8bb72 | 2012-04-10 12:41:34 +0800 | [diff] [blame] | 518 | if (!pmxops) |
| 519 | return 0; |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 520 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 521 | mutex_lock(&pctldev->mutex); |
Dong Aisheng | ad8bb72 | 2012-04-10 12:41:34 +0800 | [diff] [blame] | 522 | nfuncs = pmxops->get_functions_count(pctldev); |
Viresh Kumar | d1e90e9 | 2012-03-30 11:25:40 +0530 | [diff] [blame] | 523 | while (func_selector < nfuncs) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 524 | const char *func = pmxops->get_function_name(pctldev, |
| 525 | func_selector); |
| 526 | const char * const *groups; |
| 527 | unsigned num_groups; |
| 528 | int ret; |
| 529 | int i; |
| 530 | |
| 531 | ret = pmxops->get_function_groups(pctldev, func_selector, |
| 532 | &groups, &num_groups); |
Ludovic Desroches | 9d7ebbb | 2015-06-08 17:16:37 +0200 | [diff] [blame] | 533 | if (ret) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 534 | seq_printf(s, "function %s: COULD NOT GET GROUPS\n", |
| 535 | func); |
Ludovic Desroches | 9d7ebbb | 2015-06-08 17:16:37 +0200 | [diff] [blame] | 536 | func_selector++; |
| 537 | continue; |
| 538 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 539 | |
| 540 | seq_printf(s, "function: %s, groups = [ ", func); |
| 541 | for (i = 0; i < num_groups; i++) |
| 542 | seq_printf(s, "%s ", groups[i]); |
| 543 | seq_puts(s, "]\n"); |
| 544 | |
| 545 | func_selector++; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 546 | } |
| 547 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 548 | mutex_unlock(&pctldev->mutex); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 549 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | static int pinmux_pins_show(struct seq_file *s, void *what) |
| 554 | { |
| 555 | struct pinctrl_dev *pctldev = s->private; |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 556 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
| 557 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 558 | unsigned i, pin; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 559 | |
Dong Aisheng | ad8bb72 | 2012-04-10 12:41:34 +0800 | [diff] [blame] | 560 | if (!pmxops) |
| 561 | return 0; |
| 562 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 563 | seq_puts(s, "Pinmux settings per pin\n"); |
Linus Walleij | 8150885 | 2015-05-28 10:11:19 +0200 | [diff] [blame] | 564 | if (pmxops->strict) |
| 565 | seq_puts(s, |
| 566 | "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n"); |
| 567 | else |
| 568 | seq_puts(s, |
| 569 | "Format: pin (name): mux_owner gpio_owner hog?\n"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 570 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 571 | mutex_lock(&pctldev->mutex); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 572 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 573 | /* The pin number can be retrived from the pin controller descriptor */ |
| 574 | for (i = 0; i < pctldev->desc->npins; i++) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 575 | struct pin_desc *desc; |
Linus Walleij | 1cf94c4 | 2012-02-24 06:53:04 +0100 | [diff] [blame] | 576 | bool is_hog = false; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 577 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 578 | pin = pctldev->desc->pins[i].number; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 579 | desc = pin_desc_get(pctldev, pin); |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 580 | /* Skip if we cannot search the pin */ |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 581 | if (desc == NULL) |
| 582 | continue; |
| 583 | |
Stephen Warren | 652162d | 2012-03-05 17:22:15 -0700 | [diff] [blame] | 584 | if (desc->mux_owner && |
| 585 | !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev))) |
Linus Walleij | 1cf94c4 | 2012-02-24 06:53:04 +0100 | [diff] [blame] | 586 | is_hog = true; |
| 587 | |
Linus Walleij | 8150885 | 2015-05-28 10:11:19 +0200 | [diff] [blame] | 588 | if (pmxops->strict) { |
| 589 | if (desc->mux_owner) |
| 590 | seq_printf(s, "pin %d (%s): device %s%s", |
Masahiro Yamada | cf9d994 | 2016-05-24 14:26:26 +0900 | [diff] [blame] | 591 | pin, desc->name, desc->mux_owner, |
Linus Walleij | 8150885 | 2015-05-28 10:11:19 +0200 | [diff] [blame] | 592 | is_hog ? " (HOG)" : ""); |
| 593 | else if (desc->gpio_owner) |
| 594 | seq_printf(s, "pin %d (%s): GPIO %s", |
Masahiro Yamada | cf9d994 | 2016-05-24 14:26:26 +0900 | [diff] [blame] | 595 | pin, desc->name, desc->gpio_owner); |
Linus Walleij | 8150885 | 2015-05-28 10:11:19 +0200 | [diff] [blame] | 596 | else |
| 597 | seq_printf(s, "pin %d (%s): UNCLAIMED", |
Masahiro Yamada | cf9d994 | 2016-05-24 14:26:26 +0900 | [diff] [blame] | 598 | pin, desc->name); |
Linus Walleij | 8150885 | 2015-05-28 10:11:19 +0200 | [diff] [blame] | 599 | } else { |
| 600 | /* For non-strict controllers */ |
Masahiro Yamada | cf9d994 | 2016-05-24 14:26:26 +0900 | [diff] [blame] | 601 | seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name, |
Linus Walleij | 8150885 | 2015-05-28 10:11:19 +0200 | [diff] [blame] | 602 | desc->mux_owner ? desc->mux_owner |
| 603 | : "(MUX UNCLAIMED)", |
| 604 | desc->gpio_owner ? desc->gpio_owner |
| 605 | : "(GPIO UNCLAIMED)", |
| 606 | is_hog ? " (HOG)" : ""); |
| 607 | } |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 608 | |
Linus Walleij | 8150885 | 2015-05-28 10:11:19 +0200 | [diff] [blame] | 609 | /* If mux: print function+group claiming the pin */ |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 610 | if (desc->mux_setting) |
| 611 | seq_printf(s, " function %s group %s\n", |
| 612 | pmxops->get_function_name(pctldev, |
| 613 | desc->mux_setting->func), |
| 614 | pctlops->get_group_name(pctldev, |
| 615 | desc->mux_setting->group)); |
| 616 | else |
Markus Elfring | ffd10c2 | 2018-01-13 11:33:47 +0100 | [diff] [blame] | 617 | seq_putc(s, '\n'); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Patrice Chotard | 42fed7b | 2013-04-11 11:01:27 +0200 | [diff] [blame] | 620 | mutex_unlock(&pctldev->mutex); |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 621 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 622 | return 0; |
| 623 | } |
| 624 | |
Masahiro Yamada | 3f713b7 | 2017-08-04 11:22:31 +0900 | [diff] [blame] | 625 | void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map) |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 626 | { |
| 627 | seq_printf(s, "group %s\nfunction %s\n", |
| 628 | map->data.mux.group ? map->data.mux.group : "(default)", |
| 629 | map->data.mux.function); |
| 630 | } |
| 631 | |
| 632 | void pinmux_show_setting(struct seq_file *s, |
Masahiro Yamada | 3f713b7 | 2017-08-04 11:22:31 +0900 | [diff] [blame] | 633 | const struct pinctrl_setting *setting) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 634 | { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 635 | struct pinctrl_dev *pctldev = setting->pctldev; |
| 636 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; |
| 637 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 638 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 639 | seq_printf(s, "group: %s (%u) function: %s (%u)\n", |
| 640 | pctlops->get_group_name(pctldev, setting->data.mux.group), |
| 641 | setting->data.mux.group, |
| 642 | pmxops->get_function_name(pctldev, setting->data.mux.func), |
| 643 | setting->data.mux.func); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 644 | } |
| 645 | |
Yangtao Li | 0819dc7 | 2018-11-30 11:36:17 -0500 | [diff] [blame] | 646 | DEFINE_SHOW_ATTRIBUTE(pinmux_functions); |
| 647 | DEFINE_SHOW_ATTRIBUTE(pinmux_pins); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 648 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 649 | void pinmux_init_device_debugfs(struct dentry *devroot, |
| 650 | struct pinctrl_dev *pctldev) |
| 651 | { |
| 652 | debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO, |
Yangtao Li | 0819dc7 | 2018-11-30 11:36:17 -0500 | [diff] [blame] | 653 | devroot, pctldev, &pinmux_functions_fops); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 654 | debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO, |
Yangtao Li | 0819dc7 | 2018-11-30 11:36:17 -0500 | [diff] [blame] | 655 | devroot, pctldev, &pinmux_pins_fops); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | #endif /* CONFIG_DEBUG_FS */ |
Tony Lindgren | a76edc8 | 2016-12-27 09:20:01 -0800 | [diff] [blame] | 659 | |
| 660 | #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS |
| 661 | |
| 662 | /** |
| 663 | * pinmux_generic_get_function_count() - returns number of functions |
| 664 | * @pctldev: pin controller device |
| 665 | */ |
| 666 | int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev) |
| 667 | { |
| 668 | return pctldev->num_functions; |
| 669 | } |
| 670 | EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count); |
| 671 | |
| 672 | /** |
| 673 | * pinmux_generic_get_function_name() - returns the function name |
| 674 | * @pctldev: pin controller device |
| 675 | * @selector: function number |
| 676 | */ |
| 677 | const char * |
| 678 | pinmux_generic_get_function_name(struct pinctrl_dev *pctldev, |
| 679 | unsigned int selector) |
| 680 | { |
| 681 | struct function_desc *function; |
| 682 | |
| 683 | function = radix_tree_lookup(&pctldev->pin_function_tree, |
| 684 | selector); |
| 685 | if (!function) |
| 686 | return NULL; |
| 687 | |
| 688 | return function->name; |
| 689 | } |
| 690 | EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name); |
| 691 | |
| 692 | /** |
| 693 | * pinmux_generic_get_function_groups() - gets the function groups |
| 694 | * @pctldev: pin controller device |
| 695 | * @selector: function number |
| 696 | * @groups: array of pin groups |
| 697 | * @num_groups: number of pin groups |
| 698 | */ |
| 699 | int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev, |
| 700 | unsigned int selector, |
| 701 | const char * const **groups, |
| 702 | unsigned * const num_groups) |
| 703 | { |
| 704 | struct function_desc *function; |
| 705 | |
| 706 | function = radix_tree_lookup(&pctldev->pin_function_tree, |
| 707 | selector); |
| 708 | if (!function) { |
| 709 | dev_err(pctldev->dev, "%s could not find function%i\n", |
| 710 | __func__, selector); |
| 711 | return -EINVAL; |
| 712 | } |
| 713 | *groups = function->group_names; |
| 714 | *num_groups = function->num_group_names; |
| 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups); |
| 719 | |
| 720 | /** |
| 721 | * pinmux_generic_get_function() - returns a function based on the number |
| 722 | * @pctldev: pin controller device |
| 723 | * @group_selector: function number |
| 724 | */ |
| 725 | struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev, |
| 726 | unsigned int selector) |
| 727 | { |
| 728 | struct function_desc *function; |
| 729 | |
| 730 | function = radix_tree_lookup(&pctldev->pin_function_tree, |
| 731 | selector); |
| 732 | if (!function) |
| 733 | return NULL; |
| 734 | |
| 735 | return function; |
| 736 | } |
| 737 | EXPORT_SYMBOL_GPL(pinmux_generic_get_function); |
| 738 | |
| 739 | /** |
Geert Uytterhoeven | 6bffa7e | 2017-04-03 10:32:42 +0200 | [diff] [blame] | 740 | * pinmux_generic_add_function() - adds a function group |
Tony Lindgren | a76edc8 | 2016-12-27 09:20:01 -0800 | [diff] [blame] | 741 | * @pctldev: pin controller device |
| 742 | * @name: name of the function |
| 743 | * @groups: array of pin groups |
| 744 | * @num_groups: number of pin groups |
| 745 | * @data: pin controller driver specific data |
| 746 | */ |
| 747 | int pinmux_generic_add_function(struct pinctrl_dev *pctldev, |
| 748 | const char *name, |
| 749 | const char **groups, |
| 750 | const unsigned int num_groups, |
| 751 | void *data) |
| 752 | { |
| 753 | struct function_desc *function; |
Tony Lindgren | f913cfc | 2018-07-05 02:10:15 -0700 | [diff] [blame] | 754 | int selector; |
| 755 | |
| 756 | if (!name) |
| 757 | return -EINVAL; |
| 758 | |
| 759 | selector = pinmux_func_name_to_selector(pctldev, name); |
| 760 | if (selector >= 0) |
| 761 | return selector; |
| 762 | |
| 763 | selector = pctldev->num_functions; |
Tony Lindgren | a76edc8 | 2016-12-27 09:20:01 -0800 | [diff] [blame] | 764 | |
| 765 | function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL); |
| 766 | if (!function) |
| 767 | return -ENOMEM; |
| 768 | |
| 769 | function->name = name; |
| 770 | function->group_names = groups; |
| 771 | function->num_group_names = num_groups; |
| 772 | function->data = data; |
| 773 | |
Tony Lindgren | f913cfc | 2018-07-05 02:10:15 -0700 | [diff] [blame] | 774 | radix_tree_insert(&pctldev->pin_function_tree, selector, function); |
Tony Lindgren | a76edc8 | 2016-12-27 09:20:01 -0800 | [diff] [blame] | 775 | |
| 776 | pctldev->num_functions++; |
| 777 | |
Tony Lindgren | f913cfc | 2018-07-05 02:10:15 -0700 | [diff] [blame] | 778 | return selector; |
Tony Lindgren | a76edc8 | 2016-12-27 09:20:01 -0800 | [diff] [blame] | 779 | } |
| 780 | EXPORT_SYMBOL_GPL(pinmux_generic_add_function); |
| 781 | |
| 782 | /** |
| 783 | * pinmux_generic_remove_function() - removes a numbered function |
| 784 | * @pctldev: pin controller device |
| 785 | * @selector: function number |
| 786 | * |
| 787 | * Note that the caller must take care of locking. |
| 788 | */ |
| 789 | int pinmux_generic_remove_function(struct pinctrl_dev *pctldev, |
| 790 | unsigned int selector) |
| 791 | { |
| 792 | struct function_desc *function; |
| 793 | |
| 794 | function = radix_tree_lookup(&pctldev->pin_function_tree, |
| 795 | selector); |
| 796 | if (!function) |
| 797 | return -ENOENT; |
| 798 | |
| 799 | radix_tree_delete(&pctldev->pin_function_tree, selector); |
| 800 | devm_kfree(pctldev->dev, function); |
| 801 | |
| 802 | pctldev->num_functions--; |
| 803 | |
| 804 | return 0; |
| 805 | } |
| 806 | EXPORT_SYMBOL_GPL(pinmux_generic_remove_function); |
| 807 | |
| 808 | /** |
| 809 | * pinmux_generic_free_functions() - removes all functions |
| 810 | * @pctldev: pin controller device |
| 811 | * |
Tony Lindgren | 664b7c4 | 2017-05-12 08:47:57 -0700 | [diff] [blame] | 812 | * Note that the caller must take care of locking. The pinctrl |
| 813 | * functions are allocated with devm_kzalloc() so no need to free |
| 814 | * them here. |
Tony Lindgren | a76edc8 | 2016-12-27 09:20:01 -0800 | [diff] [blame] | 815 | */ |
| 816 | void pinmux_generic_free_functions(struct pinctrl_dev *pctldev) |
| 817 | { |
| 818 | struct radix_tree_iter iter; |
Masahiro Yamada | 906a2a3 | 2017-08-04 13:52:05 +0900 | [diff] [blame] | 819 | void __rcu **slot; |
Tony Lindgren | a76edc8 | 2016-12-27 09:20:01 -0800 | [diff] [blame] | 820 | |
| 821 | radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0) |
Tony Lindgren | 664b7c4 | 2017-05-12 08:47:57 -0700 | [diff] [blame] | 822 | radix_tree_delete(&pctldev->pin_function_tree, iter.index); |
Tony Lindgren | a76edc8 | 2016-12-27 09:20:01 -0800 | [diff] [blame] | 823 | |
| 824 | pctldev->num_functions = 0; |
| 825 | } |
| 826 | |
| 827 | #endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */ |