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