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 | * |
| 10 | * License terms: GNU General Public License (GPL) version 2 |
| 11 | */ |
| 12 | #define pr_fmt(fmt) "pinmux core: " fmt |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/radix-tree.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/list.h> |
| 22 | #include <linux/mutex.h> |
| 23 | #include <linux/spinlock.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 | |
| 33 | /** |
| 34 | * struct pinmux_group - group list item for pinmux groups |
| 35 | * @node: pinmux group list node |
| 36 | * @group_selector: the group selector for this group |
| 37 | */ |
| 38 | struct pinmux_group { |
| 39 | struct list_head node; |
| 40 | unsigned group_selector; |
| 41 | }; |
| 42 | |
Stephen Warren | 03665e0 | 2012-02-19 23:45:45 -0700 | [diff] [blame^] | 43 | int pinmux_check_ops(struct pinctrl_dev *pctldev) |
| 44 | { |
| 45 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 46 | unsigned selector = 0; |
| 47 | |
| 48 | /* Check that we implement required operations */ |
| 49 | if (!ops->list_functions || |
| 50 | !ops->get_function_name || |
| 51 | !ops->get_function_groups || |
| 52 | !ops->enable || |
| 53 | !ops->disable) |
| 54 | return -EINVAL; |
| 55 | |
| 56 | /* Check that all functions registered have names */ |
| 57 | while (ops->list_functions(pctldev, selector) >= 0) { |
| 58 | const char *fname = ops->get_function_name(pctldev, |
| 59 | selector); |
| 60 | if (!fname) { |
| 61 | pr_err("pinmux ops has no name for function%u\n", |
| 62 | selector); |
| 63 | return -EINVAL; |
| 64 | } |
| 65 | selector++; |
| 66 | } |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 71 | /** |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 72 | * pin_request() - request a single pin to be muxed in, typically for GPIO |
| 73 | * @pin: the pin number in the global pin space |
| 74 | * @function: a functional name to give to this pin, passed to the driver |
| 75 | * so it knows what function to mux in, e.g. the string "gpioNN" |
| 76 | * means that you want to mux in the pin for use as GPIO number NN |
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 | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 81 | int pin, const char *function, |
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 | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 88 | dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, function); |
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 | |
Marek Belisko | d2f6a1c | 2011-10-26 22:57:20 +0200 | [diff] [blame] | 97 | if (!function) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 98 | dev_err(pctldev->dev, "no function name given\n"); |
Marek Belisko | d2f6a1c | 2011-10-26 22:57:20 +0200 | [diff] [blame] | 99 | return -EINVAL; |
| 100 | } |
| 101 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 102 | spin_lock(&desc->lock); |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 103 | if (desc->mux_function) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 104 | spin_unlock(&desc->lock); |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 105 | dev_err(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 106 | "pin already requested\n"); |
| 107 | goto out; |
| 108 | } |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 109 | desc->mux_function = function; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 110 | spin_unlock(&desc->lock); |
| 111 | |
| 112 | /* Let each pin increase references to this module */ |
| 113 | if (!try_module_get(pctldev->owner)) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 114 | dev_err(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 115 | "could not increase module refcount for pin %d\n", |
| 116 | pin); |
| 117 | status = -EINVAL; |
| 118 | goto out_free_pin; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * If there is no kind of request function for the pin we just assume |
| 123 | * we got it by default and proceed. |
| 124 | */ |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 125 | if (gpio_range && ops->gpio_request_enable) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 126 | /* This requests and enables a single GPIO pin */ |
| 127 | status = ops->gpio_request_enable(pctldev, gpio_range, pin); |
| 128 | else if (ops->request) |
| 129 | status = ops->request(pctldev, pin); |
| 130 | else |
| 131 | status = 0; |
| 132 | |
| 133 | if (status) |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 134 | 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] | 135 | pctldev->desc->name, pin); |
| 136 | out_free_pin: |
| 137 | if (status) { |
| 138 | spin_lock(&desc->lock); |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 139 | desc->mux_function = NULL; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 140 | spin_unlock(&desc->lock); |
| 141 | } |
| 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", |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 145 | pin, function ? : "?", status); |
| 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 | * |
| 157 | * This function returns a pointer to the function name in use. This is used |
| 158 | * for callers that dynamically allocate a function name so it can be freed |
| 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 | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 166 | const char *func; |
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 | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 175 | /* |
| 176 | * If there is no kind of request function for the pin we just assume |
| 177 | * we got it by default and proceed. |
| 178 | */ |
| 179 | if (gpio_range && ops->gpio_disable_free) |
| 180 | ops->gpio_disable_free(pctldev, gpio_range, pin); |
| 181 | else if (ops->free) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 182 | ops->free(pctldev, pin); |
| 183 | |
| 184 | spin_lock(&desc->lock); |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 185 | func = desc->mux_function; |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 186 | desc->mux_function = NULL; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 187 | spin_unlock(&desc->lock); |
| 188 | module_put(pctldev->owner); |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 189 | |
| 190 | return func; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 194 | * pinmux_request_gpio() - request pinmuxing for a GPIO pin |
| 195 | * @pctldev: pin controller device affected |
| 196 | * @pin: the pin to mux in for GPIO |
| 197 | * @range: the applicable GPIO range |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 198 | */ |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 199 | int pinmux_request_gpio(struct pinctrl_dev *pctldev, |
| 200 | struct pinctrl_gpio_range *range, |
| 201 | unsigned pin, unsigned gpio) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 202 | { |
| 203 | char gpiostr[16]; |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 204 | const char *function; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 205 | int ret; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 206 | |
| 207 | /* Conjure some name stating what chip and pin this is taken by */ |
| 208 | snprintf(gpiostr, 15, "%s:%d", range->name, gpio); |
| 209 | |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 210 | function = kstrdup(gpiostr, GFP_KERNEL); |
| 211 | if (!function) |
| 212 | return -EINVAL; |
| 213 | |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 214 | ret = pin_request(pctldev, pin, function, range); |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 215 | if (ret < 0) |
| 216 | kfree(function); |
| 217 | |
| 218 | return ret; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 219 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 220 | |
| 221 | /** |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 222 | * pinmux_free_gpio() - release a pin from GPIO muxing |
| 223 | * @pctldev: the pin controller device for the pin |
| 224 | * @pin: the affected currently GPIO-muxed in pin |
| 225 | * @range: applicable GPIO range |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 226 | */ |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 227 | void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin, |
| 228 | struct pinctrl_gpio_range *range) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 229 | { |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 230 | const char *func; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 231 | |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 232 | func = pin_free(pctldev, pin, range); |
| 233 | kfree(func); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 234 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 235 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 236 | /** |
| 237 | * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin |
| 238 | * @pctldev: the pin controller handling this pin |
| 239 | * @range: applicable GPIO range |
| 240 | * @pin: the affected GPIO pin in this controller |
| 241 | * @input: true if we set the pin as input, false for output |
| 242 | */ |
| 243 | int pinmux_gpio_direction(struct pinctrl_dev *pctldev, |
| 244 | struct pinctrl_gpio_range *range, |
| 245 | unsigned pin, bool input) |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 246 | { |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 247 | const struct pinmux_ops *ops; |
| 248 | int ret; |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 249 | |
| 250 | ops = pctldev->desc->pmxops; |
| 251 | |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 252 | if (ops->gpio_set_direction) |
| 253 | ret = ops->gpio_set_direction(pctldev, range, pin, input); |
| 254 | else |
| 255 | ret = 0; |
| 256 | |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | /** |
Tony Lindgren | de849ee | 2012-01-20 08:17:33 -0800 | [diff] [blame] | 261 | * acquire_pins() - acquire all the pins for a certain function on a pinmux |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 262 | * @pctldev: the device to take the pins on |
| 263 | * @func_selector: the function selector to acquire the pins for |
| 264 | * @group_selector: the group selector containing the pins to acquire |
| 265 | */ |
| 266 | static int acquire_pins(struct pinctrl_dev *pctldev, |
| 267 | unsigned func_selector, |
| 268 | unsigned group_selector) |
| 269 | { |
| 270 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
| 271 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; |
| 272 | const char *func = pmxops->get_function_name(pctldev, |
| 273 | func_selector); |
Stephen Warren | a5818a8 | 2011-10-19 16:19:25 -0600 | [diff] [blame] | 274 | const unsigned *pins; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 275 | unsigned num_pins; |
| 276 | int ret; |
| 277 | int i; |
| 278 | |
| 279 | ret = pctlops->get_group_pins(pctldev, group_selector, |
| 280 | &pins, &num_pins); |
| 281 | if (ret) |
| 282 | return ret; |
| 283 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 284 | dev_dbg(pctldev->dev, "requesting the %u pins from group %u\n", |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 285 | num_pins, group_selector); |
| 286 | |
| 287 | /* Try to allocate all pins in this group, one by one */ |
| 288 | for (i = 0; i < num_pins; i++) { |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 289 | ret = pin_request(pctldev, pins[i], func, NULL); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 290 | if (ret) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 291 | dev_err(pctldev->dev, |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 292 | "could not get pin %d for function %s on device %s - conflicting mux mappings?\n", |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 293 | pins[i], func ? : "(undefined)", |
| 294 | pinctrl_dev_get_name(pctldev)); |
| 295 | /* On error release all taken pins */ |
| 296 | i--; /* this pin just failed */ |
| 297 | for (; i >= 0; i--) |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 298 | pin_free(pctldev, pins[i], NULL); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 299 | return -ENODEV; |
| 300 | } |
| 301 | } |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * release_pins() - release pins taken by earlier acquirement |
Tony Lindgren | de849ee | 2012-01-20 08:17:33 -0800 | [diff] [blame] | 307 | * @pctldev: the device to free the pins on |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 308 | * @group_selector: the group selector containing the pins to free |
| 309 | */ |
| 310 | static void release_pins(struct pinctrl_dev *pctldev, |
| 311 | unsigned group_selector) |
| 312 | { |
| 313 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
Stephen Warren | a5818a8 | 2011-10-19 16:19:25 -0600 | [diff] [blame] | 314 | const unsigned *pins; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 315 | unsigned num_pins; |
| 316 | int ret; |
| 317 | int i; |
| 318 | |
| 319 | ret = pctlops->get_group_pins(pctldev, group_selector, |
| 320 | &pins, &num_pins); |
| 321 | if (ret) { |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 322 | dev_err(pctldev->dev, "could not get pins to release for group selector %d\n", |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 323 | group_selector); |
| 324 | return; |
| 325 | } |
| 326 | for (i = 0; i < num_pins; i++) |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 327 | pin_free(pctldev, pins[i], NULL); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /** |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 331 | * pinmux_check_pin_group() - check function and pin group combo |
| 332 | * @pctldev: device to check the pin group vs function for |
| 333 | * @func_selector: the function selector to check the pin group for, we have |
| 334 | * already looked this up in the calling function |
| 335 | * @pin_group: the pin group to match to the function |
| 336 | * |
| 337 | * This function will check that the pinmux driver can supply the |
| 338 | * selected pin group for a certain function, returns the group selector if |
| 339 | * the group and function selector will work fine together, else returns |
| 340 | * negative |
| 341 | */ |
| 342 | static int pinmux_check_pin_group(struct pinctrl_dev *pctldev, |
| 343 | unsigned func_selector, |
| 344 | const char *pin_group) |
| 345 | { |
| 346 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; |
| 347 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
| 348 | int ret; |
| 349 | |
| 350 | /* |
| 351 | * If the driver does not support different pin groups for the |
| 352 | * functions, we only support group 0, and assume this exists. |
| 353 | */ |
| 354 | if (!pctlops || !pctlops->list_groups) |
| 355 | return 0; |
| 356 | |
| 357 | /* |
| 358 | * Passing NULL (no specific group) will select the first and |
| 359 | * hopefully only group of pins available for this function. |
| 360 | */ |
| 361 | if (!pin_group) { |
| 362 | char const * const *groups; |
| 363 | unsigned num_groups; |
| 364 | |
| 365 | ret = pmxops->get_function_groups(pctldev, func_selector, |
| 366 | &groups, &num_groups); |
| 367 | if (ret) |
| 368 | return ret; |
| 369 | if (num_groups < 1) |
| 370 | return -EINVAL; |
Linus Walleij | 7afde8b | 2011-10-19 17:07:16 +0200 | [diff] [blame] | 371 | ret = pinctrl_get_group_selector(pctldev, groups[0]); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 372 | if (ret < 0) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 373 | dev_err(pctldev->dev, |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 374 | "function %s wants group %s but the pin controller does not seem to have that group\n", |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 375 | pmxops->get_function_name(pctldev, func_selector), |
| 376 | groups[0]); |
| 377 | return ret; |
| 378 | } |
| 379 | |
| 380 | if (num_groups > 1) |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 381 | dev_dbg(pctldev->dev, |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 382 | "function %s support more than one group, default-selecting first group %s (%d)\n", |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 383 | pmxops->get_function_name(pctldev, func_selector), |
| 384 | groups[0], |
| 385 | ret); |
| 386 | |
| 387 | return ret; |
| 388 | } |
| 389 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 390 | dev_dbg(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 391 | "check if we have pin group %s on controller %s\n", |
| 392 | pin_group, pinctrl_dev_get_name(pctldev)); |
| 393 | |
Linus Walleij | 7afde8b | 2011-10-19 17:07:16 +0200 | [diff] [blame] | 394 | ret = pinctrl_get_group_selector(pctldev, pin_group); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 395 | if (ret < 0) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 396 | dev_dbg(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 397 | "%s does not support pin group %s with function %s\n", |
| 398 | pinctrl_dev_get_name(pctldev), |
| 399 | pin_group, |
| 400 | pmxops->get_function_name(pctldev, func_selector)); |
| 401 | } |
| 402 | return ret; |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * pinmux_search_function() - check pin control driver for a certain function |
| 407 | * @pctldev: device to check for function and position |
| 408 | * @map: function map containing the function and position to look for |
| 409 | * @func_selector: returns the applicable function selector if found |
| 410 | * @group_selector: returns the applicable group selector if found |
| 411 | * |
| 412 | * This will search the pinmux driver for an applicable |
| 413 | * function with a specific pin group, returns 0 if these can be mapped |
| 414 | * negative otherwise |
| 415 | */ |
| 416 | static int pinmux_search_function(struct pinctrl_dev *pctldev, |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame] | 417 | struct pinctrl_map const *map, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 418 | unsigned *func_selector, |
| 419 | unsigned *group_selector) |
| 420 | { |
| 421 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 422 | unsigned selector = 0; |
| 423 | |
| 424 | /* See if this pctldev has this function */ |
| 425 | while (ops->list_functions(pctldev, selector) >= 0) { |
| 426 | const char *fname = ops->get_function_name(pctldev, |
| 427 | selector); |
| 428 | int ret; |
| 429 | |
| 430 | if (!strcmp(map->function, fname)) { |
| 431 | /* Found the function, check pin group */ |
| 432 | ret = pinmux_check_pin_group(pctldev, selector, |
| 433 | map->group); |
| 434 | if (ret < 0) |
| 435 | return ret; |
| 436 | |
| 437 | /* This function and group selector can be used */ |
| 438 | *func_selector = selector; |
| 439 | *group_selector = ret; |
| 440 | return 0; |
| 441 | |
| 442 | } |
| 443 | selector++; |
| 444 | } |
| 445 | |
| 446 | pr_err("%s does not support function %s\n", |
| 447 | pinctrl_dev_get_name(pctldev), map->function); |
| 448 | return -EINVAL; |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * pinmux_enable_muxmap() - enable a map entry for a certain pinmux |
| 453 | */ |
| 454 | static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev, |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame] | 455 | struct pinctrl *p, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 456 | struct device *dev, |
| 457 | const char *devname, |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame] | 458 | struct pinctrl_map const *map) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 459 | { |
| 460 | unsigned func_selector; |
| 461 | unsigned group_selector; |
| 462 | struct pinmux_group *grp; |
| 463 | int ret; |
| 464 | |
| 465 | /* |
| 466 | * Note that we're not locking the pinmux mutex here, because |
| 467 | * this is only called at pinmux initialization time when it |
| 468 | * has not been added to any list and thus is not reachable |
| 469 | * by anyone else. |
| 470 | */ |
| 471 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame] | 472 | if (p->pctldev && p->pctldev != pctldev) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 473 | dev_err(pctldev->dev, |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 474 | "different pin control devices given for device %s, function %s\n", |
| 475 | devname, map->function); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 476 | return -EINVAL; |
| 477 | } |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame] | 478 | p->dev = dev; |
| 479 | p->pctldev = pctldev; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 480 | |
| 481 | /* Now go into the driver and try to match a function and group */ |
| 482 | ret = pinmux_search_function(pctldev, map, &func_selector, |
| 483 | &group_selector); |
| 484 | if (ret < 0) |
| 485 | return ret; |
| 486 | |
| 487 | /* |
| 488 | * If the function selector is already set, it needs to be identical, |
| 489 | * we support several groups with one function but not several |
| 490 | * functions with one or several groups in the same pinmux. |
| 491 | */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame] | 492 | if (p->func_selector != UINT_MAX && |
| 493 | p->func_selector != func_selector) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 494 | dev_err(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 495 | "dual function defines in the map for device %s\n", |
| 496 | devname); |
| 497 | return -EINVAL; |
| 498 | } |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame] | 499 | p->func_selector = func_selector; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 500 | |
| 501 | /* Now add this group selector, we may have many of them */ |
| 502 | grp = kmalloc(sizeof(struct pinmux_group), GFP_KERNEL); |
| 503 | if (!grp) |
| 504 | return -ENOMEM; |
| 505 | grp->group_selector = group_selector; |
| 506 | ret = acquire_pins(pctldev, func_selector, group_selector); |
| 507 | if (ret) { |
| 508 | kfree(grp); |
| 509 | return ret; |
| 510 | } |
Stephen Warren | 8b9c139 | 2012-02-19 23:45:42 -0700 | [diff] [blame] | 511 | list_add_tail(&grp->node, &p->groups); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 516 | /** |
| 517 | * pinmux_apply_muxmap() - apply a certain mux mapping entry |
| 518 | */ |
| 519 | int pinmux_apply_muxmap(struct pinctrl_dev *pctldev, |
| 520 | struct pinctrl *p, |
| 521 | struct device *dev, |
| 522 | const char *devname, |
| 523 | struct pinctrl_map const *map) |
| 524 | { |
| 525 | int ret; |
| 526 | |
| 527 | ret = pinmux_enable_muxmap(pctldev, p, dev, |
| 528 | devname, map); |
| 529 | if (ret) { |
| 530 | pinmux_put(p); |
| 531 | return ret; |
| 532 | } |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * pinmux_put() - free up the pinmux portions of a pin controller handle |
| 539 | */ |
| 540 | void pinmux_put(struct pinctrl *p) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 541 | { |
| 542 | struct list_head *node, *tmp; |
| 543 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame] | 544 | list_for_each_safe(node, tmp, &p->groups) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 545 | struct pinmux_group *grp = |
| 546 | list_entry(node, struct pinmux_group, node); |
| 547 | /* Release all pins taken by this group */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame] | 548 | release_pins(p->pctldev, grp->group_selector); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 549 | list_del(node); |
| 550 | kfree(grp); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /** |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 555 | * pinmux_enable() - enable the pinmux portion of a pin control handle |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 556 | */ |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 557 | int pinmux_enable(struct pinctrl *p) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 558 | { |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 559 | struct pinctrl_dev *pctldev = p->pctldev; |
| 560 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 561 | struct pinmux_group *grp; |
| 562 | int ret; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 563 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 564 | list_for_each_entry(grp, &p->groups, node) { |
| 565 | ret = ops->enable(pctldev, p->func_selector, |
| 566 | grp->group_selector); |
| 567 | if (ret) |
| 568 | /* |
| 569 | * TODO: call disable() on all groups we called |
| 570 | * enable() on to this point? |
| 571 | */ |
| 572 | return ret; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 573 | } |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 574 | return 0; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 575 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 576 | |
| 577 | /** |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 578 | * pinmux_disable() - disable the pinmux portions of a pin control handle |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 579 | */ |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 580 | void pinmux_disable(struct pinctrl *p) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 581 | { |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 582 | struct pinctrl_dev *pctldev = p->pctldev; |
| 583 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 584 | struct pinmux_group *grp; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 585 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 586 | list_for_each_entry(grp, &p->groups, node) { |
| 587 | ops->disable(pctldev, p->func_selector, |
| 588 | grp->group_selector); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 589 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 590 | } |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 591 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 592 | #ifdef CONFIG_DEBUG_FS |
| 593 | |
| 594 | /* Called from pincontrol core */ |
| 595 | static int pinmux_functions_show(struct seq_file *s, void *what) |
| 596 | { |
| 597 | struct pinctrl_dev *pctldev = s->private; |
| 598 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; |
| 599 | unsigned func_selector = 0; |
| 600 | |
| 601 | while (pmxops->list_functions(pctldev, func_selector) >= 0) { |
| 602 | const char *func = pmxops->get_function_name(pctldev, |
| 603 | func_selector); |
| 604 | const char * const *groups; |
| 605 | unsigned num_groups; |
| 606 | int ret; |
| 607 | int i; |
| 608 | |
| 609 | ret = pmxops->get_function_groups(pctldev, func_selector, |
| 610 | &groups, &num_groups); |
| 611 | if (ret) |
| 612 | seq_printf(s, "function %s: COULD NOT GET GROUPS\n", |
| 613 | func); |
| 614 | |
| 615 | seq_printf(s, "function: %s, groups = [ ", func); |
| 616 | for (i = 0; i < num_groups; i++) |
| 617 | seq_printf(s, "%s ", groups[i]); |
| 618 | seq_puts(s, "]\n"); |
| 619 | |
| 620 | func_selector++; |
| 621 | |
| 622 | } |
| 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | static int pinmux_pins_show(struct seq_file *s, void *what) |
| 628 | { |
| 629 | struct pinctrl_dev *pctldev = s->private; |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 630 | unsigned i, pin; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 631 | |
| 632 | seq_puts(s, "Pinmux settings per pin\n"); |
| 633 | seq_puts(s, "Format: pin (name): pinmuxfunction\n"); |
| 634 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 635 | /* The pin number can be retrived from the pin controller descriptor */ |
| 636 | for (i = 0; i < pctldev->desc->npins; i++) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 637 | |
| 638 | struct pin_desc *desc; |
| 639 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 640 | pin = pctldev->desc->pins[i].number; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 641 | desc = pin_desc_get(pctldev, pin); |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 642 | /* Skip if we cannot search the pin */ |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 643 | if (desc == NULL) |
| 644 | continue; |
| 645 | |
| 646 | seq_printf(s, "pin %d (%s): %s\n", pin, |
| 647 | desc->name ? desc->name : "unnamed", |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 648 | desc->mux_function ? desc->mux_function |
| 649 | : "UNCLAIMED"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 655 | void pinmux_dbg_show(struct seq_file *s, struct pinctrl *p) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 656 | { |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 657 | struct pinctrl_dev *pctldev = p->pctldev; |
| 658 | const struct pinmux_ops *pmxops; |
| 659 | const struct pinctrl_ops *pctlops; |
| 660 | struct pinmux_group *grp; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 661 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 662 | pmxops = pctldev->desc->pmxops; |
| 663 | pctlops = pctldev->desc->pctlops; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 664 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 665 | seq_printf(s, " function: %s (%u),", |
| 666 | pmxops->get_function_name(pctldev, |
| 667 | p->func_selector), |
| 668 | p->func_selector); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 669 | |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 670 | seq_printf(s, " groups: ["); |
| 671 | list_for_each_entry(grp, &p->groups, node) { |
| 672 | seq_printf(s, " %s (%u)", |
| 673 | pctlops->get_group_name(pctldev, |
| 674 | grp->group_selector), |
| 675 | grp->group_selector); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 676 | } |
Linus Walleij | befe5bd | 2012-02-09 19:47:48 +0100 | [diff] [blame] | 677 | seq_printf(s, " ]"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | static int pinmux_functions_open(struct inode *inode, struct file *file) |
| 681 | { |
| 682 | return single_open(file, pinmux_functions_show, inode->i_private); |
| 683 | } |
| 684 | |
| 685 | static int pinmux_pins_open(struct inode *inode, struct file *file) |
| 686 | { |
| 687 | return single_open(file, pinmux_pins_show, inode->i_private); |
| 688 | } |
| 689 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 690 | static const struct file_operations pinmux_functions_ops = { |
| 691 | .open = pinmux_functions_open, |
| 692 | .read = seq_read, |
| 693 | .llseek = seq_lseek, |
| 694 | .release = single_release, |
| 695 | }; |
| 696 | |
| 697 | static const struct file_operations pinmux_pins_ops = { |
| 698 | .open = pinmux_pins_open, |
| 699 | .read = seq_read, |
| 700 | .llseek = seq_lseek, |
| 701 | .release = single_release, |
| 702 | }; |
| 703 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 704 | void pinmux_init_device_debugfs(struct dentry *devroot, |
| 705 | struct pinctrl_dev *pctldev) |
| 706 | { |
| 707 | debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO, |
| 708 | devroot, pctldev, &pinmux_functions_ops); |
| 709 | debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO, |
| 710 | devroot, pctldev, &pinmux_pins_ops); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | #endif /* CONFIG_DEBUG_FS */ |