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 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 61 | /** |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 62 | * pin_request() - request a single pin to be muxed in, typically for GPIO |
| 63 | * @pin: the pin number in the global pin space |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 64 | * @owner: a representation of the owner of this pin; typically the device |
| 65 | * name that controls its mux function, or the requested GPIO name |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 66 | * @gpio_range: the range matching the GPIO pin if this is a request for a |
| 67 | * single GPIO pin |
| 68 | */ |
| 69 | static int pin_request(struct pinctrl_dev *pctldev, |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 70 | int pin, const char *owner, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 71 | struct pinctrl_gpio_range *gpio_range) |
| 72 | { |
| 73 | struct pin_desc *desc; |
| 74 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 75 | int status = -EINVAL; |
| 76 | |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 77 | dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, owner); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 78 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 79 | desc = pin_desc_get(pctldev, pin); |
| 80 | if (desc == NULL) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 81 | dev_err(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 82 | "pin is not registered so it cannot be requested\n"); |
| 83 | goto out; |
| 84 | } |
| 85 | |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 86 | if (desc->owner && strcmp(desc->owner, owner)) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 87 | dev_err(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 88 | "pin already requested\n"); |
| 89 | goto out; |
| 90 | } |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 91 | desc->owner = owner; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 92 | |
| 93 | /* Let each pin increase references to this module */ |
| 94 | if (!try_module_get(pctldev->owner)) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 95 | dev_err(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 96 | "could not increase module refcount for pin %d\n", |
| 97 | pin); |
| 98 | status = -EINVAL; |
| 99 | goto out_free_pin; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * If there is no kind of request function for the pin we just assume |
| 104 | * we got it by default and proceed. |
| 105 | */ |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 106 | if (gpio_range && ops->gpio_request_enable) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 107 | /* This requests and enables a single GPIO pin */ |
| 108 | status = ops->gpio_request_enable(pctldev, gpio_range, pin); |
| 109 | else if (ops->request) |
| 110 | status = ops->request(pctldev, pin); |
| 111 | else |
| 112 | status = 0; |
| 113 | |
| 114 | if (status) |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 115 | 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] | 116 | pctldev->desc->name, pin); |
| 117 | out_free_pin: |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 118 | if (status) |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 119 | desc->owner = NULL; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 120 | out: |
| 121 | if (status) |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 122 | dev_err(pctldev->dev, "pin-%d (%s) status %d\n", |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 123 | pin, owner, status); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 124 | |
| 125 | return status; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * pin_free() - release a single muxed in pin so something else can be muxed |
| 130 | * @pctldev: pin controller device handling this pin |
| 131 | * @pin: the pin to free |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 132 | * @gpio_range: the range matching the GPIO pin if this is a request for a |
| 133 | * single GPIO pin |
Linus Walleij | 336cdba0 | 2011-11-10 09:27:41 +0100 | [diff] [blame] | 134 | * |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 135 | * This function returns a pointer to the previous owner. This is used |
| 136 | * 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] | 137 | * once the pin is free. This is done for GPIO request functions. |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 138 | */ |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 139 | static const char *pin_free(struct pinctrl_dev *pctldev, int pin, |
| 140 | struct pinctrl_gpio_range *gpio_range) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 141 | { |
| 142 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 143 | struct pin_desc *desc; |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 144 | const char *owner; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 145 | |
| 146 | desc = pin_desc_get(pctldev, pin); |
| 147 | if (desc == NULL) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 148 | dev_err(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 149 | "pin is not registered so it cannot be freed\n"); |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 150 | return NULL; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 153 | /* |
| 154 | * If there is no kind of request function for the pin we just assume |
| 155 | * we got it by default and proceed. |
| 156 | */ |
| 157 | if (gpio_range && ops->gpio_disable_free) |
| 158 | ops->gpio_disable_free(pctldev, gpio_range, pin); |
| 159 | else if (ops->free) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 160 | ops->free(pctldev, pin); |
| 161 | |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 162 | owner = desc->owner; |
| 163 | desc->owner = NULL; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 164 | module_put(pctldev->owner); |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 165 | |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 166 | return owner; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 170 | * pinmux_request_gpio() - request pinmuxing for a GPIO pin |
| 171 | * @pctldev: pin controller device affected |
| 172 | * @pin: the pin to mux in for GPIO |
| 173 | * @range: the applicable GPIO range |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 174 | */ |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 175 | int pinmux_request_gpio(struct pinctrl_dev *pctldev, |
| 176 | struct pinctrl_gpio_range *range, |
| 177 | unsigned pin, unsigned gpio) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 178 | { |
| 179 | char gpiostr[16]; |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 180 | const char *owner; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 181 | int ret; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 182 | |
| 183 | /* Conjure some name stating what chip and pin this is taken by */ |
| 184 | snprintf(gpiostr, 15, "%s:%d", range->name, gpio); |
| 185 | |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 186 | owner = kstrdup(gpiostr, GFP_KERNEL); |
| 187 | if (!owner) |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 188 | return -EINVAL; |
| 189 | |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 190 | ret = pin_request(pctldev, pin, owner, range); |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 191 | if (ret < 0) |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 192 | kfree(owner); |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 193 | |
| 194 | return ret; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 195 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 196 | |
| 197 | /** |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 198 | * pinmux_free_gpio() - release a pin from GPIO muxing |
| 199 | * @pctldev: the pin controller device for the pin |
| 200 | * @pin: the affected currently GPIO-muxed in pin |
| 201 | * @range: applicable GPIO range |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 202 | */ |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 203 | void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin, |
| 204 | struct pinctrl_gpio_range *range) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 205 | { |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 206 | const char *owner; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 207 | |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 208 | owner = pin_free(pctldev, pin, range); |
| 209 | kfree(owner); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 210 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 211 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 212 | /** |
| 213 | * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin |
| 214 | * @pctldev: the pin controller handling this pin |
| 215 | * @range: applicable GPIO range |
| 216 | * @pin: the affected GPIO pin in this controller |
| 217 | * @input: true if we set the pin as input, false for output |
| 218 | */ |
| 219 | int pinmux_gpio_direction(struct pinctrl_dev *pctldev, |
| 220 | struct pinctrl_gpio_range *range, |
| 221 | unsigned pin, bool input) |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 222 | { |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 223 | const struct pinmux_ops *ops; |
| 224 | int ret; |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 225 | |
| 226 | ops = pctldev->desc->pmxops; |
| 227 | |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 228 | if (ops->gpio_set_direction) |
| 229 | ret = ops->gpio_set_direction(pctldev, range, pin, input); |
| 230 | else |
| 231 | ret = 0; |
| 232 | |
| 233 | return ret; |
| 234 | } |
| 235 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 236 | static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev, |
| 237 | const char *function) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 238 | { |
| 239 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 240 | unsigned selector = 0; |
| 241 | |
| 242 | /* See if this pctldev has this function */ |
| 243 | while (ops->list_functions(pctldev, selector) >= 0) { |
| 244 | const char *fname = ops->get_function_name(pctldev, |
| 245 | selector); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 246 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 247 | if (!strcmp(function, fname)) |
| 248 | return selector; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 249 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 250 | selector++; |
| 251 | } |
| 252 | |
| 253 | pr_err("%s does not support function %s\n", |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 254 | pinctrl_dev_get_name(pctldev), function); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 255 | return -EINVAL; |
| 256 | } |
| 257 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 258 | int pinmux_map_to_setting(struct pinctrl_map const *map, |
| 259 | struct pinctrl_setting *setting) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 260 | { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 261 | struct pinctrl_dev *pctldev = setting->pctldev; |
| 262 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; |
| 263 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
| 264 | char const * const *groups; |
| 265 | unsigned num_groups; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 266 | int ret; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 267 | const char *group; |
| 268 | int i; |
| 269 | const unsigned *pins; |
| 270 | unsigned num_pins; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 271 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 272 | setting->func_selector = |
| 273 | pinmux_func_name_to_selector(pctldev, map->function); |
| 274 | if (setting->func_selector < 0) |
| 275 | return setting->func_selector; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 276 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 277 | ret = pmxops->get_function_groups(pctldev, setting->func_selector, |
| 278 | &groups, &num_groups); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 279 | if (ret < 0) |
| 280 | return ret; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 281 | if (!num_groups) |
| 282 | return -EINVAL; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 283 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 284 | if (map->group) { |
| 285 | bool found = false; |
| 286 | group = map->group; |
| 287 | for (i = 0; i < num_groups; i++) { |
| 288 | if (!strcmp(group, groups[i])) { |
| 289 | found = true; |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | if (!found) |
| 294 | return -EINVAL; |
| 295 | } else { |
| 296 | group = groups[0]; |
| 297 | } |
| 298 | |
| 299 | setting->group_selector = |
| 300 | pinctrl_get_group_selector(pctldev, group); |
| 301 | if (setting->group_selector < 0) |
| 302 | return setting->group_selector; |
| 303 | |
| 304 | ret = pctlops->get_group_pins(pctldev, setting->group_selector, |
| 305 | &pins, &num_pins); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 306 | if (ret) { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 307 | dev_err(pctldev->dev, |
| 308 | "could not get pins for device %s group selector %d\n", |
| 309 | pinctrl_dev_get_name(pctldev), setting->group_selector); |
| 310 | return -ENODEV; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 311 | } |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 312 | |
| 313 | /* Try to allocate all pins in this group, one by one */ |
| 314 | for (i = 0; i < num_pins; i++) { |
| 315 | ret = pin_request(pctldev, pins[i], map->dev_name, NULL); |
| 316 | if (ret) { |
| 317 | dev_err(pctldev->dev, |
| 318 | "could not get request pin %d on device %s\n", |
| 319 | pins[i], pinctrl_dev_get_name(pctldev)); |
| 320 | /* On error release all taken pins */ |
| 321 | i--; /* this pin just failed */ |
| 322 | for (; i >= 0; i--) |
| 323 | pin_free(pctldev, pins[i], NULL); |
| 324 | return -ENODEV; |
| 325 | } |
| 326 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 331 | void pinmux_free_setting(struct pinctrl_setting const *setting) |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 332 | { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 333 | struct pinctrl_dev *pctldev = setting->pctldev; |
| 334 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
| 335 | const unsigned *pins; |
| 336 | unsigned num_pins; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 337 | int ret; |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 338 | int i; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 339 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 340 | ret = pctlops->get_group_pins(pctldev, setting->group_selector, |
| 341 | &pins, &num_pins); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 342 | if (ret) { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 343 | dev_err(pctldev->dev, |
| 344 | "could not get pins for device %s group selector %d\n", |
| 345 | pinctrl_dev_get_name(pctldev), setting->group_selector); |
| 346 | return; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 347 | } |
| 348 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 349 | for (i = 0; i < num_pins; i++) |
| 350 | pin_free(pctldev, pins[i], NULL); |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 351 | } |
| 352 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 353 | int pinmux_enable_setting(struct pinctrl_setting const *setting) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 354 | { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 355 | struct pinctrl_dev *pctldev = setting->pctldev; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 356 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 357 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 358 | return ops->enable(pctldev, setting->func_selector, |
| 359 | setting->group_selector); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 360 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 361 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 362 | void pinmux_disable_setting(struct pinctrl_setting const *setting) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 363 | { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 364 | struct pinctrl_dev *pctldev = setting->pctldev; |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 365 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 366 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 367 | ops->disable(pctldev, setting->func_selector, setting->group_selector); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 368 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 369 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 370 | #ifdef CONFIG_DEBUG_FS |
| 371 | |
| 372 | /* Called from pincontrol core */ |
| 373 | static int pinmux_functions_show(struct seq_file *s, void *what) |
| 374 | { |
| 375 | struct pinctrl_dev *pctldev = s->private; |
| 376 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; |
| 377 | unsigned func_selector = 0; |
| 378 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 379 | mutex_lock(&pinctrl_mutex); |
| 380 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 381 | while (pmxops->list_functions(pctldev, func_selector) >= 0) { |
| 382 | const char *func = pmxops->get_function_name(pctldev, |
| 383 | func_selector); |
| 384 | const char * const *groups; |
| 385 | unsigned num_groups; |
| 386 | int ret; |
| 387 | int i; |
| 388 | |
| 389 | ret = pmxops->get_function_groups(pctldev, func_selector, |
| 390 | &groups, &num_groups); |
| 391 | if (ret) |
| 392 | seq_printf(s, "function %s: COULD NOT GET GROUPS\n", |
| 393 | func); |
| 394 | |
| 395 | seq_printf(s, "function: %s, groups = [ ", func); |
| 396 | for (i = 0; i < num_groups; i++) |
| 397 | seq_printf(s, "%s ", groups[i]); |
| 398 | seq_puts(s, "]\n"); |
| 399 | |
| 400 | func_selector++; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 401 | } |
| 402 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 403 | mutex_unlock(&pinctrl_mutex); |
| 404 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static int pinmux_pins_show(struct seq_file *s, void *what) |
| 409 | { |
| 410 | struct pinctrl_dev *pctldev = s->private; |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 411 | unsigned i, pin; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 412 | |
| 413 | seq_puts(s, "Pinmux settings per pin\n"); |
Stephen Warren | 3cc70ed | 2012-02-19 23:45:44 -0700 | [diff] [blame] | 414 | seq_puts(s, "Format: pin (name): owner\n"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 415 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 416 | mutex_lock(&pinctrl_mutex); |
| 417 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 418 | /* The pin number can be retrived from the pin controller descriptor */ |
| 419 | for (i = 0; i < pctldev->desc->npins; i++) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 420 | struct pin_desc *desc; |
Linus Walleij | 1cf94c4 | 2012-02-24 06:53:04 +0100 | [diff] [blame] | 421 | bool is_hog = false; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 422 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 423 | pin = pctldev->desc->pins[i].number; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 424 | desc = pin_desc_get(pctldev, pin); |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 425 | /* Skip if we cannot search the pin */ |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 426 | if (desc == NULL) |
| 427 | continue; |
| 428 | |
Linus Walleij | 1cf94c4 | 2012-02-24 06:53:04 +0100 | [diff] [blame] | 429 | if (desc->owner && |
| 430 | !strcmp(desc->owner, pinctrl_dev_get_name(pctldev))) |
| 431 | is_hog = true; |
| 432 | |
| 433 | seq_printf(s, "pin %d (%s): %s%s\n", pin, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 434 | desc->name ? desc->name : "unnamed", |
Linus Walleij | 1cf94c4 | 2012-02-24 06:53:04 +0100 | [diff] [blame] | 435 | desc->owner ? desc->owner : "UNCLAIMED", |
| 436 | is_hog ? " (HOG)" : ""); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Stephen Warren | 57b676f | 2012-03-02 13:05:44 -0700 | [diff] [blame] | 439 | mutex_unlock(&pinctrl_mutex); |
| 440 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 444 | void pinmux_dbg_show(struct seq_file *s, struct pinctrl_setting const *setting) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 445 | { |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 446 | struct pinctrl_dev *pctldev = setting->pctldev; |
| 447 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; |
| 448 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 449 | |
Stephen Warren | 7ecdb16 | 2012-03-02 13:05:45 -0700 | [diff] [blame^] | 450 | seq_printf(s, "controller: %s group: %s (%u) function: %s (%u)\n", |
| 451 | pinctrl_dev_get_name(pctldev), |
| 452 | pctlops->get_group_name(pctldev, setting->group_selector), |
| 453 | setting->group_selector, |
| 454 | pmxops->get_function_name(pctldev, setting->func_selector), |
| 455 | setting->func_selector); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | static int pinmux_functions_open(struct inode *inode, struct file *file) |
| 459 | { |
| 460 | return single_open(file, pinmux_functions_show, inode->i_private); |
| 461 | } |
| 462 | |
| 463 | static int pinmux_pins_open(struct inode *inode, struct file *file) |
| 464 | { |
| 465 | return single_open(file, pinmux_pins_show, inode->i_private); |
| 466 | } |
| 467 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 468 | static const struct file_operations pinmux_functions_ops = { |
| 469 | .open = pinmux_functions_open, |
| 470 | .read = seq_read, |
| 471 | .llseek = seq_lseek, |
| 472 | .release = single_release, |
| 473 | }; |
| 474 | |
| 475 | static const struct file_operations pinmux_pins_ops = { |
| 476 | .open = pinmux_pins_open, |
| 477 | .read = seq_read, |
| 478 | .llseek = seq_lseek, |
| 479 | .release = single_release, |
| 480 | }; |
| 481 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 482 | void pinmux_init_device_debugfs(struct dentry *devroot, |
| 483 | struct pinctrl_dev *pctldev) |
| 484 | { |
| 485 | debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO, |
| 486 | devroot, pctldev, &pinmux_functions_ops); |
| 487 | debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO, |
| 488 | devroot, pctldev, &pinmux_pins_ops); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | #endif /* CONFIG_DEBUG_FS */ |