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