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