Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Core driver for the pin muxing portions of the pin control subsystem |
| 3 | * |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame] | 4 | * Copyright (C) 2011-2012 ST-Ericsson SA |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 5 | * Written on behalf of Linaro for ST-Ericsson |
| 6 | * Based on bits of regulator core, gpio core and clk core |
| 7 | * |
| 8 | * Author: Linus Walleij <linus.walleij@linaro.org> |
| 9 | * |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 10 | * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved. |
| 11 | * |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 12 | * License terms: GNU General Public License (GPL) version 2 |
| 13 | */ |
| 14 | #define pr_fmt(fmt) "pinmux core: " fmt |
| 15 | |
| 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/sysfs.h> |
| 26 | #include <linux/debugfs.h> |
| 27 | #include <linux/seq_file.h> |
| 28 | #include <linux/pinctrl/machine.h> |
| 29 | #include <linux/pinctrl/pinmux.h> |
| 30 | #include "core.h" |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 31 | #include "pinmux.h" |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 32 | |
Stephen Warren | 03665e0 | 2012-02-19 23:45:45 -0700 | [diff] [blame] | 33 | int pinmux_check_ops(struct pinctrl_dev *pctldev) |
| 34 | { |
| 35 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 36 | unsigned selector = 0; |
| 37 | |
| 38 | /* Check that we implement required operations */ |
| 39 | if (!ops->list_functions || |
| 40 | !ops->get_function_name || |
| 41 | !ops->get_function_groups || |
| 42 | !ops->enable || |
| 43 | !ops->disable) |
| 44 | return -EINVAL; |
| 45 | |
| 46 | /* Check that all functions registered have names */ |
| 47 | while (ops->list_functions(pctldev, selector) >= 0) { |
| 48 | const char *fname = ops->get_function_name(pctldev, |
| 49 | selector); |
| 50 | if (!fname) { |
| 51 | pr_err("pinmux ops has no name for function%u\n", |
| 52 | selector); |
| 53 | return -EINVAL; |
| 54 | } |
| 55 | selector++; |
| 56 | } |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 61 | int pinmux_validate_map(struct pinctrl_map const *map, int i) |
| 62 | { |
| 63 | if (!map->data.mux.function) { |
| 64 | pr_err("failed to register map %s (%d): no function given\n", |
| 65 | map->name, i); |
| 66 | return -EINVAL; |
| 67 | } |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 72 | /** |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 73 | * pin_request() - request a single pin to be muxed in, typically for GPIO |
| 74 | * @pin: the pin number in the global pin space |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 75 | * @owner: a representation of the owner of this pin; typically the device |
| 76 | * name that controls its mux function, or the requested GPIO name |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 77 | * @gpio_range: the range matching the GPIO pin if this is a request for a |
| 78 | * single GPIO pin |
| 79 | */ |
| 80 | static int pin_request(struct pinctrl_dev *pctldev, |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 81 | int pin, const char *owner, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 82 | struct pinctrl_gpio_range *gpio_range) |
| 83 | { |
| 84 | struct pin_desc *desc; |
| 85 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 86 | int status = -EINVAL; |
| 87 | |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 88 | dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, owner); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 89 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 90 | desc = pin_desc_get(pctldev, pin); |
| 91 | if (desc == NULL) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 92 | dev_err(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 93 | "pin is not registered so it cannot be requested\n"); |
| 94 | goto out; |
| 95 | } |
| 96 | |
Stephen Warren | 652162d | 2012-03-05 17:22:15 -0700 | [diff] [blame] | 97 | if (gpio_range) { |
| 98 | /* There's no need to support multiple GPIO requests */ |
| 99 | if (desc->gpio_owner) { |
| 100 | dev_err(pctldev->dev, |
| 101 | "pin already requested\n"); |
| 102 | goto out; |
| 103 | } |
| 104 | |
| 105 | desc->gpio_owner = owner; |
| 106 | } else { |
| 107 | if (desc->mux_usecount && strcmp(desc->mux_owner, owner)) { |
| 108 | dev_err(pctldev->dev, |
| 109 | "pin already requested\n"); |
| 110 | goto out; |
| 111 | } |
| 112 | |
| 113 | desc->mux_usecount++; |
| 114 | if (desc->mux_usecount > 1) |
| 115 | return 0; |
| 116 | |
| 117 | desc->mux_owner = owner; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 118 | } |
Stephen Warren | 0e3db173 | 2012-03-02 13:05:46 -0700 | [diff] [blame] | 119 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 120 | /* Let each pin increase references to this module */ |
| 121 | if (!try_module_get(pctldev->owner)) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 122 | dev_err(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 123 | "could not increase module refcount for pin %d\n", |
| 124 | pin); |
| 125 | status = -EINVAL; |
| 126 | goto out_free_pin; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * If there is no kind of request function for the pin we just assume |
| 131 | * we got it by default and proceed. |
| 132 | */ |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 133 | if (gpio_range && ops->gpio_request_enable) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 134 | /* This requests and enables a single GPIO pin */ |
| 135 | status = ops->gpio_request_enable(pctldev, gpio_range, pin); |
| 136 | else if (ops->request) |
| 137 | status = ops->request(pctldev, pin); |
| 138 | else |
| 139 | status = 0; |
| 140 | |
Stephen Warren | 0e3db173 | 2012-03-02 13:05:46 -0700 | [diff] [blame] | 141 | if (status) { |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 142 | dev_err(pctldev->dev, "->request on device %s failed for pin %d\n", |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 143 | pctldev->desc->name, pin); |
Stephen Warren | 0e3db173 | 2012-03-02 13:05:46 -0700 | [diff] [blame] | 144 | module_put(pctldev->owner); |
| 145 | } |
| 146 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 147 | out_free_pin: |
Stephen Warren | 0e3db173 | 2012-03-02 13:05:46 -0700 | [diff] [blame] | 148 | if (status) { |
Stephen Warren | 652162d | 2012-03-05 17:22:15 -0700 | [diff] [blame] | 149 | if (gpio_range) { |
| 150 | desc->gpio_owner = NULL; |
| 151 | } else { |
| 152 | desc->mux_usecount--; |
| 153 | if (!desc->mux_usecount) |
| 154 | desc->mux_owner = NULL; |
| 155 | } |
Stephen Warren | 0e3db173 | 2012-03-02 13:05:46 -0700 | [diff] [blame] | 156 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 157 | out: |
| 158 | if (status) |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 159 | dev_err(pctldev->dev, "pin-%d (%s) status %d\n", |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 160 | pin, owner, status); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 161 | |
| 162 | return status; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * pin_free() - release a single muxed in pin so something else can be muxed |
| 167 | * @pctldev: pin controller device handling this pin |
| 168 | * @pin: the pin to free |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 169 | * @gpio_range: the range matching the GPIO pin if this is a request for a |
| 170 | * single GPIO pin |
Linus Walleij | 336cdba0 | 2011-11-10 09:27:41 +0100 | [diff] [blame] | 171 | * |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 172 | * This function returns a pointer to the previous owner. This is used |
| 173 | * 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] | 174 | * once the pin is free. This is done for GPIO request functions. |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 175 | */ |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 176 | static const char *pin_free(struct pinctrl_dev *pctldev, int pin, |
| 177 | struct pinctrl_gpio_range *gpio_range) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 178 | { |
| 179 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 180 | struct pin_desc *desc; |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 181 | const char *owner; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 182 | |
| 183 | desc = pin_desc_get(pctldev, pin); |
| 184 | if (desc == NULL) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 185 | dev_err(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 186 | "pin is not registered so it cannot be freed\n"); |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 187 | return NULL; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Stephen Warren | 652162d | 2012-03-05 17:22:15 -0700 | [diff] [blame] | 190 | if (!gpio_range) { |
| 191 | desc->mux_usecount--; |
| 192 | if (desc->mux_usecount) |
| 193 | return NULL; |
| 194 | } |
Stephen Warren | 0e3db173 | 2012-03-02 13:05:46 -0700 | [diff] [blame] | 195 | |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 196 | /* |
| 197 | * If there is no kind of request function for the pin we just assume |
| 198 | * we got it by default and proceed. |
| 199 | */ |
| 200 | if (gpio_range && ops->gpio_disable_free) |
| 201 | ops->gpio_disable_free(pctldev, gpio_range, pin); |
| 202 | else if (ops->free) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 203 | ops->free(pctldev, pin); |
| 204 | |
Stephen Warren | 652162d | 2012-03-05 17:22:15 -0700 | [diff] [blame] | 205 | if (gpio_range) { |
| 206 | owner = desc->gpio_owner; |
| 207 | desc->gpio_owner = NULL; |
| 208 | } else { |
| 209 | owner = desc->mux_owner; |
| 210 | desc->mux_owner = NULL; |
| 211 | desc->mux_setting = NULL; |
| 212 | } |
| 213 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 214 | module_put(pctldev->owner); |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 215 | |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 216 | return owner; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 220 | * pinmux_request_gpio() - request pinmuxing for a GPIO pin |
| 221 | * @pctldev: pin controller device affected |
| 222 | * @pin: the pin to mux in for GPIO |
| 223 | * @range: the applicable GPIO range |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 224 | */ |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 225 | int pinmux_request_gpio(struct pinctrl_dev *pctldev, |
| 226 | struct pinctrl_gpio_range *range, |
| 227 | unsigned pin, unsigned gpio) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 228 | { |
| 229 | char gpiostr[16]; |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 230 | const char *owner; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 231 | int ret; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 232 | |
| 233 | /* Conjure some name stating what chip and pin this is taken by */ |
| 234 | snprintf(gpiostr, 15, "%s:%d", range->name, gpio); |
| 235 | |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 236 | owner = kstrdup(gpiostr, GFP_KERNEL); |
| 237 | if (!owner) |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 238 | return -EINVAL; |
| 239 | |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 240 | ret = pin_request(pctldev, pin, owner, range); |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 241 | if (ret < 0) |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 242 | kfree(owner); |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 243 | |
| 244 | return ret; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 245 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 246 | |
| 247 | /** |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 248 | * pinmux_free_gpio() - release a pin from GPIO muxing |
| 249 | * @pctldev: the pin controller device for the pin |
| 250 | * @pin: the affected currently GPIO-muxed in pin |
| 251 | * @range: applicable GPIO range |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 252 | */ |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 253 | void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin, |
| 254 | struct pinctrl_gpio_range *range) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 255 | { |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 256 | const char *owner; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 257 | |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 258 | owner = pin_free(pctldev, pin, range); |
| 259 | kfree(owner); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 260 | } |
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 | /** |
| 263 | * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin |
| 264 | * @pctldev: the pin controller handling this pin |
| 265 | * @range: applicable GPIO range |
| 266 | * @pin: the affected GPIO pin in this controller |
| 267 | * @input: true if we set the pin as input, false for output |
| 268 | */ |
| 269 | int pinmux_gpio_direction(struct pinctrl_dev *pctldev, |
| 270 | struct pinctrl_gpio_range *range, |
| 271 | unsigned pin, bool input) |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 272 | { |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 273 | const struct pinmux_ops *ops; |
| 274 | int ret; |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 275 | |
| 276 | ops = pctldev->desc->pmxops; |
| 277 | |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 278 | if (ops->gpio_set_direction) |
| 279 | ret = ops->gpio_set_direction(pctldev, range, pin, input); |
| 280 | else |
| 281 | ret = 0; |
| 282 | |
| 283 | return ret; |
| 284 | } |
| 285 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 286 | static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev, |
| 287 | const char *function) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 288 | { |
| 289 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 290 | unsigned selector = 0; |
| 291 | |
| 292 | /* See if this pctldev has this function */ |
| 293 | while (ops->list_functions(pctldev, selector) >= 0) { |
| 294 | const char *fname = ops->get_function_name(pctldev, |
| 295 | selector); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 296 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 297 | if (!strcmp(function, fname)) |
| 298 | return selector; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 299 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 300 | selector++; |
| 301 | } |
| 302 | |
| 303 | pr_err("%s does not support function %s\n", |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 304 | pinctrl_dev_get_name(pctldev), function); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 305 | return -EINVAL; |
| 306 | } |
| 307 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 308 | int pinmux_map_to_setting(struct pinctrl_map const *map, |
| 309 | struct pinctrl_setting *setting) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 310 | { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 311 | struct pinctrl_dev *pctldev = setting->pctldev; |
| 312 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; |
| 313 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
| 314 | char const * const *groups; |
| 315 | unsigned num_groups; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 316 | int ret; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 317 | const char *group; |
| 318 | int i; |
| 319 | const unsigned *pins; |
| 320 | unsigned num_pins; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 321 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 322 | setting->data.mux.func = |
| 323 | pinmux_func_name_to_selector(pctldev, map->data.mux.function); |
| 324 | if (setting->data.mux.func < 0) |
| 325 | return setting->data.mux.func; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 326 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 327 | ret = pmxops->get_function_groups(pctldev, setting->data.mux.func, |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 328 | &groups, &num_groups); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 329 | if (ret < 0) |
| 330 | return ret; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 331 | if (!num_groups) |
| 332 | return -EINVAL; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 333 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 334 | if (map->data.mux.group) { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 335 | bool found = false; |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 336 | group = map->data.mux.group; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 337 | for (i = 0; i < num_groups; i++) { |
| 338 | if (!strcmp(group, groups[i])) { |
| 339 | found = true; |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | if (!found) |
| 344 | return -EINVAL; |
| 345 | } else { |
| 346 | group = groups[0]; |
| 347 | } |
| 348 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 349 | setting->data.mux.group = pinctrl_get_group_selector(pctldev, group); |
| 350 | if (setting->data.mux.group < 0) |
| 351 | return setting->data.mux.group; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 352 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 353 | ret = pctlops->get_group_pins(pctldev, setting->data.mux.group, &pins, |
| 354 | &num_pins); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 355 | if (ret) { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 356 | dev_err(pctldev->dev, |
| 357 | "could not get pins for device %s group selector %d\n", |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 358 | pinctrl_dev_get_name(pctldev), setting->data.mux.group); |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 359 | return -ENODEV; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 360 | } |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 361 | |
| 362 | /* Try to allocate all pins in this group, one by one */ |
| 363 | for (i = 0; i < num_pins; i++) { |
| 364 | ret = pin_request(pctldev, pins[i], map->dev_name, NULL); |
| 365 | if (ret) { |
| 366 | dev_err(pctldev->dev, |
| 367 | "could not get request pin %d on device %s\n", |
| 368 | pins[i], pinctrl_dev_get_name(pctldev)); |
| 369 | /* On error release all taken pins */ |
| 370 | i--; /* this pin just failed */ |
| 371 | for (; i >= 0; i--) |
| 372 | pin_free(pctldev, pins[i], NULL); |
| 373 | return -ENODEV; |
| 374 | } |
| 375 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 380 | void pinmux_free_setting(struct pinctrl_setting const *setting) |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 381 | { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 382 | struct pinctrl_dev *pctldev = setting->pctldev; |
| 383 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
| 384 | const unsigned *pins; |
| 385 | unsigned num_pins; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 386 | int ret; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 387 | int i; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 388 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 389 | ret = pctlops->get_group_pins(pctldev, setting->data.mux.group, |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 390 | &pins, &num_pins); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 391 | if (ret) { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 392 | dev_err(pctldev->dev, |
| 393 | "could not get pins for device %s group selector %d\n", |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 394 | pinctrl_dev_get_name(pctldev), setting->data.mux.group); |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 395 | return; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 396 | } |
| 397 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 398 | for (i = 0; i < num_pins; i++) |
| 399 | pin_free(pctldev, pins[i], NULL); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 400 | } |
| 401 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 402 | int pinmux_enable_setting(struct pinctrl_setting const *setting) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 403 | { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 404 | struct pinctrl_dev *pctldev = setting->pctldev; |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 405 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 406 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 407 | int ret; |
| 408 | const unsigned *pins; |
| 409 | unsigned num_pins; |
| 410 | int i; |
| 411 | struct pin_desc *desc; |
| 412 | |
| 413 | ret = pctlops->get_group_pins(pctldev, setting->data.mux.group, |
| 414 | &pins, &num_pins); |
| 415 | if (ret) { |
| 416 | /* errors only affect debug data, so just warn */ |
| 417 | dev_warn(pctldev->dev, |
| 418 | "could not get pins for group selector %d\n", |
| 419 | setting->data.mux.group); |
| 420 | num_pins = 0; |
| 421 | } |
| 422 | |
| 423 | for (i = 0; i < num_pins; i++) { |
| 424 | desc = pin_desc_get(pctldev, pins[i]); |
| 425 | if (desc == NULL) { |
| 426 | dev_warn(pctldev->dev, |
| 427 | "could not get pin desc for pin %d\n", |
| 428 | pins[i]); |
| 429 | continue; |
| 430 | } |
| 431 | desc->mux_setting = &(setting->data.mux); |
| 432 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 433 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 434 | return ops->enable(pctldev, setting->data.mux.func, |
| 435 | setting->data.mux.group); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 436 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 437 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 438 | void pinmux_disable_setting(struct pinctrl_setting const *setting) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 439 | { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 440 | struct pinctrl_dev *pctldev = setting->pctldev; |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 441 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 442 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 443 | int ret; |
| 444 | const unsigned *pins; |
| 445 | unsigned num_pins; |
| 446 | int i; |
| 447 | struct pin_desc *desc; |
| 448 | |
| 449 | ret = pctlops->get_group_pins(pctldev, setting->data.mux.group, |
| 450 | &pins, &num_pins); |
| 451 | if (ret) { |
| 452 | /* errors only affect debug data, so just warn */ |
| 453 | dev_warn(pctldev->dev, |
| 454 | "could not get pins for group selector %d\n", |
| 455 | setting->data.mux.group); |
| 456 | num_pins = 0; |
| 457 | } |
| 458 | |
| 459 | for (i = 0; i < num_pins; i++) { |
| 460 | desc = pin_desc_get(pctldev, pins[i]); |
| 461 | if (desc == NULL) { |
| 462 | dev_warn(pctldev->dev, |
| 463 | "could not get pin desc for pin %d\n", |
| 464 | pins[i]); |
| 465 | continue; |
| 466 | } |
| 467 | desc->mux_setting = NULL; |
| 468 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 469 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 470 | ops->disable(pctldev, setting->data.mux.func, setting->data.mux.group); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 471 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 472 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 473 | #ifdef CONFIG_DEBUG_FS |
| 474 | |
| 475 | /* Called from pincontrol core */ |
| 476 | static int pinmux_functions_show(struct seq_file *s, void *what) |
| 477 | { |
| 478 | struct pinctrl_dev *pctldev = s->private; |
| 479 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; |
| 480 | unsigned func_selector = 0; |
| 481 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 482 | mutex_lock(&pinctrl_mutex); |
| 483 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 484 | while (pmxops->list_functions(pctldev, func_selector) >= 0) { |
| 485 | const char *func = pmxops->get_function_name(pctldev, |
| 486 | func_selector); |
| 487 | const char * const *groups; |
| 488 | unsigned num_groups; |
| 489 | int ret; |
| 490 | int i; |
| 491 | |
| 492 | ret = pmxops->get_function_groups(pctldev, func_selector, |
| 493 | &groups, &num_groups); |
| 494 | if (ret) |
| 495 | seq_printf(s, "function %s: COULD NOT GET GROUPS\n", |
| 496 | func); |
| 497 | |
| 498 | seq_printf(s, "function: %s, groups = [ ", func); |
| 499 | for (i = 0; i < num_groups; i++) |
| 500 | seq_printf(s, "%s ", groups[i]); |
| 501 | seq_puts(s, "]\n"); |
| 502 | |
| 503 | func_selector++; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 504 | } |
| 505 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 506 | mutex_unlock(&pinctrl_mutex); |
| 507 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | static int pinmux_pins_show(struct seq_file *s, void *what) |
| 512 | { |
| 513 | struct pinctrl_dev *pctldev = s->private; |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 514 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
| 515 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 516 | unsigned i, pin; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 517 | |
| 518 | seq_puts(s, "Pinmux settings per pin\n"); |
Stephen Warren | 652162d | 2012-03-05 17:22:15 -0700 | [diff] [blame] | 519 | seq_puts(s, "Format: pin (name): mux_owner gpio_owner hog?\n"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 520 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 521 | mutex_lock(&pinctrl_mutex); |
| 522 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 523 | /* The pin number can be retrived from the pin controller descriptor */ |
| 524 | for (i = 0; i < pctldev->desc->npins; i++) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 525 | struct pin_desc *desc; |
Linus Walleij | 1cf94c4 | 2012-02-24 06:53:04 +0100 | [diff] [blame] | 526 | bool is_hog = false; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 527 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 528 | pin = pctldev->desc->pins[i].number; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 529 | desc = pin_desc_get(pctldev, pin); |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 530 | /* Skip if we cannot search the pin */ |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 531 | if (desc == NULL) |
| 532 | continue; |
| 533 | |
Stephen Warren | 652162d | 2012-03-05 17:22:15 -0700 | [diff] [blame] | 534 | if (desc->mux_owner && |
| 535 | !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev))) |
Linus Walleij | 1cf94c4 | 2012-02-24 06:53:04 +0100 | [diff] [blame] | 536 | is_hog = true; |
| 537 | |
Stephen Warren | 652162d | 2012-03-05 17:22:15 -0700 | [diff] [blame] | 538 | seq_printf(s, "pin %d (%s): %s %s%s", pin, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 539 | desc->name ? desc->name : "unnamed", |
Stephen Warren | 652162d | 2012-03-05 17:22:15 -0700 | [diff] [blame] | 540 | desc->mux_owner ? desc->mux_owner |
| 541 | : "(MUX UNCLAIMED)", |
| 542 | desc->gpio_owner ? desc->gpio_owner |
| 543 | : "(GPIO UNCLAIMED)", |
Linus Walleij | 1cf94c4 | 2012-02-24 06:53:04 +0100 | [diff] [blame] | 544 | is_hog ? " (HOG)" : ""); |
Stephen Warren | ba110d9 | 2012-03-02 13:05:49 -0700 | [diff] [blame] | 545 | |
| 546 | if (desc->mux_setting) |
| 547 | seq_printf(s, " function %s group %s\n", |
| 548 | pmxops->get_function_name(pctldev, |
| 549 | desc->mux_setting->func), |
| 550 | pctlops->get_group_name(pctldev, |
| 551 | desc->mux_setting->group)); |
| 552 | else |
| 553 | seq_printf(s, "\n"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 554 | } |
| 555 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 556 | mutex_unlock(&pinctrl_mutex); |
| 557 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 558 | return 0; |
| 559 | } |
| 560 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 561 | void pinmux_show_map(struct seq_file *s, struct pinctrl_map const *map) |
| 562 | { |
| 563 | seq_printf(s, "group %s\nfunction %s\n", |
| 564 | map->data.mux.group ? map->data.mux.group : "(default)", |
| 565 | map->data.mux.function); |
| 566 | } |
| 567 | |
| 568 | void pinmux_show_setting(struct seq_file *s, |
| 569 | struct pinctrl_setting const *setting) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 570 | { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame] | 571 | struct pinctrl_dev *pctldev = setting->pctldev; |
| 572 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; |
| 573 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 574 | |
Stephen Warren | 1e2082b | 2012-03-02 13:05:48 -0700 | [diff] [blame] | 575 | seq_printf(s, "group: %s (%u) function: %s (%u)\n", |
| 576 | pctlops->get_group_name(pctldev, setting->data.mux.group), |
| 577 | setting->data.mux.group, |
| 578 | pmxops->get_function_name(pctldev, setting->data.mux.func), |
| 579 | setting->data.mux.func); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | static int pinmux_functions_open(struct inode *inode, struct file *file) |
| 583 | { |
| 584 | return single_open(file, pinmux_functions_show, inode->i_private); |
| 585 | } |
| 586 | |
| 587 | static int pinmux_pins_open(struct inode *inode, struct file *file) |
| 588 | { |
| 589 | return single_open(file, pinmux_pins_show, inode->i_private); |
| 590 | } |
| 591 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 592 | static const struct file_operations pinmux_functions_ops = { |
| 593 | .open = pinmux_functions_open, |
| 594 | .read = seq_read, |
| 595 | .llseek = seq_lseek, |
| 596 | .release = single_release, |
| 597 | }; |
| 598 | |
| 599 | static const struct file_operations pinmux_pins_ops = { |
| 600 | .open = pinmux_pins_open, |
| 601 | .read = seq_read, |
| 602 | .llseek = seq_lseek, |
| 603 | .release = single_release, |
| 604 | }; |
| 605 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 606 | void pinmux_init_device_debugfs(struct dentry *devroot, |
| 607 | struct pinctrl_dev *pctldev) |
| 608 | { |
| 609 | debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO, |
| 610 | devroot, pctldev, &pinmux_functions_ops); |
| 611 | debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO, |
| 612 | devroot, pctldev, &pinmux_pins_ops); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | #endif /* CONFIG_DEBUG_FS */ |