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" |
| 31 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 32 | /* List of pin controller handles */ |
| 33 | static DEFINE_MUTEX(pinctrl_list_mutex); |
| 34 | static LIST_HEAD(pinctrl_list); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 35 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 36 | /* Global pinctrl maps */ |
| 37 | static struct pinctrl_map *pinctrl_maps; |
| 38 | static unsigned pinctrl_maps_num; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * struct pinmux_group - group list item for pinmux groups |
| 42 | * @node: pinmux group list node |
| 43 | * @group_selector: the group selector for this group |
| 44 | */ |
| 45 | struct pinmux_group { |
| 46 | struct list_head node; |
| 47 | unsigned group_selector; |
| 48 | }; |
| 49 | |
| 50 | /** |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 51 | * struct pinctrl - per-device pin control state holder |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 52 | * @node: global list node |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 53 | * @dev: the device using this pin control handle |
| 54 | * @usecount: the number of active users of this pin controller setting, used |
| 55 | * to keep track of nested use cases |
| 56 | * @pctldev: pin control device handling this pin control handle |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 57 | * @func_selector: the function selector for the pinmux device handling |
| 58 | * this pinmux |
| 59 | * @groups: the group selectors for the pinmux device and |
| 60 | * selector combination handling this pinmux, this is a list that |
| 61 | * will be traversed on all pinmux operations such as |
| 62 | * get/put/enable/disable |
| 63 | * @mutex: a lock for the pinmux state holder |
| 64 | */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 65 | struct pinctrl { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 66 | struct list_head node; |
| 67 | struct device *dev; |
| 68 | unsigned usecount; |
| 69 | struct pinctrl_dev *pctldev; |
| 70 | unsigned func_selector; |
| 71 | struct list_head groups; |
| 72 | struct mutex mutex; |
| 73 | }; |
| 74 | |
| 75 | /** |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 76 | * struct pinctrl_hog - a list item to stash control hogs |
| 77 | * @node: pin control hog list node |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 78 | * @map: map entry responsible for this hogging |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 79 | * @pmx: the pin control hogged by this item |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 80 | */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 81 | struct pinctrl_hog { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 82 | struct list_head node; |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 83 | struct pinctrl_map const *map; |
| 84 | struct pinctrl *p; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | /** |
| 88 | * pin_request() - request a single pin to be muxed in, typically for GPIO |
| 89 | * @pin: the pin number in the global pin space |
| 90 | * @function: a functional name to give to this pin, passed to the driver |
| 91 | * so it knows what function to mux in, e.g. the string "gpioNN" |
| 92 | * 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] | 93 | * @gpio_range: the range matching the GPIO pin if this is a request for a |
| 94 | * single GPIO pin |
| 95 | */ |
| 96 | static int pin_request(struct pinctrl_dev *pctldev, |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 97 | int pin, const char *function, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 98 | struct pinctrl_gpio_range *gpio_range) |
| 99 | { |
| 100 | struct pin_desc *desc; |
| 101 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 102 | int status = -EINVAL; |
| 103 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 104 | dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, function); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 105 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 106 | desc = pin_desc_get(pctldev, pin); |
| 107 | if (desc == NULL) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 108 | dev_err(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 109 | "pin is not registered so it cannot be requested\n"); |
| 110 | goto out; |
| 111 | } |
| 112 | |
Marek Belisko | d2f6a1c | 2011-10-26 22:57:20 +0200 | [diff] [blame] | 113 | if (!function) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 114 | dev_err(pctldev->dev, "no function name given\n"); |
Marek Belisko | d2f6a1c | 2011-10-26 22:57:20 +0200 | [diff] [blame] | 115 | return -EINVAL; |
| 116 | } |
| 117 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 118 | spin_lock(&desc->lock); |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 119 | if (desc->mux_function) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 120 | spin_unlock(&desc->lock); |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 121 | dev_err(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 122 | "pin already requested\n"); |
| 123 | goto out; |
| 124 | } |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 125 | desc->mux_function = function; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 126 | spin_unlock(&desc->lock); |
| 127 | |
| 128 | /* Let each pin increase references to this module */ |
| 129 | if (!try_module_get(pctldev->owner)) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 130 | dev_err(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 131 | "could not increase module refcount for pin %d\n", |
| 132 | pin); |
| 133 | status = -EINVAL; |
| 134 | goto out_free_pin; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * If there is no kind of request function for the pin we just assume |
| 139 | * we got it by default and proceed. |
| 140 | */ |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 141 | if (gpio_range && ops->gpio_request_enable) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 142 | /* This requests and enables a single GPIO pin */ |
| 143 | status = ops->gpio_request_enable(pctldev, gpio_range, pin); |
| 144 | else if (ops->request) |
| 145 | status = ops->request(pctldev, pin); |
| 146 | else |
| 147 | status = 0; |
| 148 | |
| 149 | if (status) |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 150 | 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] | 151 | pctldev->desc->name, pin); |
| 152 | out_free_pin: |
| 153 | if (status) { |
| 154 | spin_lock(&desc->lock); |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 155 | desc->mux_function = NULL; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 156 | spin_unlock(&desc->lock); |
| 157 | } |
| 158 | out: |
| 159 | if (status) |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 160 | dev_err(pctldev->dev, "pin-%d (%s) status %d\n", |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 161 | pin, function ? : "?", status); |
| 162 | |
| 163 | return status; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * pin_free() - release a single muxed in pin so something else can be muxed |
| 168 | * @pctldev: pin controller device handling this pin |
| 169 | * @pin: the pin to free |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 170 | * @gpio_range: the range matching the GPIO pin if this is a request for a |
| 171 | * single GPIO pin |
Linus Walleij | 336cdba0 | 2011-11-10 09:27:41 +0100 | [diff] [blame] | 172 | * |
| 173 | * This function returns a pointer to the function name in use. This is used |
| 174 | * for callers that dynamically allocate a function name so it can be freed |
| 175 | * once the pin is free. This is done for GPIO request functions. |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 176 | */ |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 177 | static const char *pin_free(struct pinctrl_dev *pctldev, int pin, |
| 178 | struct pinctrl_gpio_range *gpio_range) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 179 | { |
| 180 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 181 | struct pin_desc *desc; |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 182 | const char *func; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 183 | |
| 184 | desc = pin_desc_get(pctldev, pin); |
| 185 | if (desc == NULL) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 186 | dev_err(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 187 | "pin is not registered so it cannot be freed\n"); |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 188 | return NULL; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 191 | /* |
| 192 | * If there is no kind of request function for the pin we just assume |
| 193 | * we got it by default and proceed. |
| 194 | */ |
| 195 | if (gpio_range && ops->gpio_disable_free) |
| 196 | ops->gpio_disable_free(pctldev, gpio_range, pin); |
| 197 | else if (ops->free) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 198 | ops->free(pctldev, pin); |
| 199 | |
| 200 | spin_lock(&desc->lock); |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 201 | func = desc->mux_function; |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 202 | desc->mux_function = NULL; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 203 | spin_unlock(&desc->lock); |
| 204 | module_put(pctldev->owner); |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 205 | |
| 206 | return func; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 210 | * pinctrl_request_gpio() - request a single pin to be used in as GPIO |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 211 | * @gpio: the GPIO pin number from the GPIO subsystem number space |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 212 | * |
| 213 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, |
| 214 | * as part of their gpio_request() semantics, platforms and individual drivers |
| 215 | * shall *NOT* request GPIO pins to be muxed in. |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 216 | */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 217 | int pinctrl_request_gpio(unsigned gpio) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 218 | { |
| 219 | char gpiostr[16]; |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 220 | const char *function; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 221 | struct pinctrl_dev *pctldev; |
| 222 | struct pinctrl_gpio_range *range; |
| 223 | int ret; |
| 224 | int pin; |
| 225 | |
| 226 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); |
| 227 | if (ret) |
| 228 | return -EINVAL; |
| 229 | |
| 230 | /* Convert to the pin controllers number space */ |
Chanho Park | 3c739ad | 2011-11-11 18:47:58 +0900 | [diff] [blame] | 231 | pin = gpio - range->base + range->pin_base; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 232 | |
| 233 | /* Conjure some name stating what chip and pin this is taken by */ |
| 234 | snprintf(gpiostr, 15, "%s:%d", range->name, gpio); |
| 235 | |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 236 | function = kstrdup(gpiostr, GFP_KERNEL); |
| 237 | if (!function) |
| 238 | return -EINVAL; |
| 239 | |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 240 | ret = pin_request(pctldev, pin, function, range); |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 241 | if (ret < 0) |
| 242 | kfree(function); |
| 243 | |
| 244 | return ret; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 245 | } |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 246 | EXPORT_SYMBOL_GPL(pinctrl_request_gpio); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 247 | |
| 248 | /** |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 249 | * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 250 | * @gpio: the GPIO pin number from the GPIO subsystem number space |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 251 | * |
| 252 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, |
| 253 | * as part of their gpio_free() semantics, platforms and individual drivers |
| 254 | * shall *NOT* request GPIO pins to be muxed out. |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 255 | */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 256 | void pinctrl_free_gpio(unsigned gpio) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 257 | { |
| 258 | struct pinctrl_dev *pctldev; |
| 259 | struct pinctrl_gpio_range *range; |
| 260 | int ret; |
| 261 | int pin; |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 262 | const char *func; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 263 | |
| 264 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); |
| 265 | if (ret) |
| 266 | return; |
| 267 | |
| 268 | /* Convert to the pin controllers number space */ |
Chanho Park | 3c739ad | 2011-11-11 18:47:58 +0900 | [diff] [blame] | 269 | pin = gpio - range->base + range->pin_base; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 270 | |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 271 | func = pin_free(pctldev, pin, range); |
| 272 | kfree(func); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 273 | } |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 274 | EXPORT_SYMBOL_GPL(pinctrl_free_gpio); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 275 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 276 | static int pinctrl_gpio_direction(unsigned gpio, bool input) |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 277 | { |
| 278 | struct pinctrl_dev *pctldev; |
| 279 | struct pinctrl_gpio_range *range; |
| 280 | const struct pinmux_ops *ops; |
| 281 | int ret; |
| 282 | int pin; |
| 283 | |
| 284 | ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); |
| 285 | if (ret) |
| 286 | return ret; |
| 287 | |
| 288 | ops = pctldev->desc->pmxops; |
| 289 | |
| 290 | /* Convert to the pin controllers number space */ |
| 291 | pin = gpio - range->base + range->pin_base; |
| 292 | |
| 293 | if (ops->gpio_set_direction) |
| 294 | ret = ops->gpio_set_direction(pctldev, range, pin, input); |
| 295 | else |
| 296 | ret = 0; |
| 297 | |
| 298 | return ret; |
| 299 | } |
| 300 | |
| 301 | /** |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 302 | * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 303 | * @gpio: the GPIO pin number from the GPIO subsystem number space |
| 304 | * |
| 305 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, |
| 306 | * as part of their gpio_direction_input() semantics, platforms and individual |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 307 | * drivers shall *NOT* touch pin control GPIO calls. |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 308 | */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 309 | int pinctrl_gpio_direction_input(unsigned gpio) |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 310 | { |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 311 | return pinctrl_gpio_direction(gpio, true); |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 312 | } |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 313 | EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input); |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 314 | |
| 315 | /** |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 316 | * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 317 | * @gpio: the GPIO pin number from the GPIO subsystem number space |
| 318 | * |
| 319 | * This function should *ONLY* be used from gpiolib-based GPIO drivers, |
| 320 | * as part of their gpio_direction_output() semantics, platforms and individual |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 321 | * drivers shall *NOT* touch pin control GPIO calls. |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 322 | */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 323 | int pinctrl_gpio_direction_output(unsigned gpio) |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 324 | { |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 325 | return pinctrl_gpio_direction(gpio, false); |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 326 | } |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 327 | EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output); |
Linus Walleij | 542e704 | 2011-11-14 10:06:22 +0100 | [diff] [blame] | 328 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 329 | /** |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 330 | * pinctrl_register_mappings() - register a set of pin controller mappings |
| 331 | * @maps: the pincontrol mappings table to register, this should be marked with |
Linus Walleij | 97607d1 | 2011-11-29 12:52:39 +0100 | [diff] [blame] | 332 | * __initdata so it can be discarded after boot, this function will |
| 333 | * perform a shallow copy for the mapping entries. |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 334 | * @num_maps: the number of maps in the mapping table |
| 335 | * |
| 336 | * Only call this once during initialization of your machine, the function is |
| 337 | * tagged as __init and won't be callable after init has completed. The map |
| 338 | * passed into this function will be owned by the pinmux core and cannot be |
Dong Aisheng | e6337c3 | 2011-12-20 17:51:59 +0800 | [diff] [blame] | 339 | * freed. |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 340 | */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 341 | int __init pinctrl_register_mappings(struct pinctrl_map const *maps, |
| 342 | unsigned num_maps) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 343 | { |
Linus Walleij | 59b099b | 2011-11-30 13:28:14 +0100 | [diff] [blame] | 344 | void *tmp_maps; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 345 | int i; |
| 346 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 347 | pr_debug("add %d pinmux maps\n", num_maps); |
Linus Walleij | 97607d1 | 2011-11-29 12:52:39 +0100 | [diff] [blame] | 348 | |
Linus Walleij | 59b099b | 2011-11-30 13:28:14 +0100 | [diff] [blame] | 349 | /* First sanity check the new mapping */ |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 350 | for (i = 0; i < num_maps; i++) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 351 | if (!maps[i].name) { |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 352 | pr_err("failed to register map %d: no map name given\n", |
| 353 | i); |
Linus Walleij | 59b099b | 2011-11-30 13:28:14 +0100 | [diff] [blame] | 354 | return -EINVAL; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 355 | } |
Linus Walleij | 97607d1 | 2011-11-29 12:52:39 +0100 | [diff] [blame] | 356 | |
Linus Walleij | 9dfac4f | 2012-02-01 18:02:47 +0100 | [diff] [blame] | 357 | if (!maps[i].ctrl_dev_name) { |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 358 | pr_err("failed to register map %s (%d): no pin control device given\n", |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 359 | maps[i].name, i); |
Linus Walleij | 59b099b | 2011-11-30 13:28:14 +0100 | [diff] [blame] | 360 | return -EINVAL; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 361 | } |
Linus Walleij | 97607d1 | 2011-11-29 12:52:39 +0100 | [diff] [blame] | 362 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 363 | if (!maps[i].function) { |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 364 | pr_err("failed to register map %s (%d): no function ID given\n", |
| 365 | maps[i].name, i); |
Linus Walleij | 59b099b | 2011-11-30 13:28:14 +0100 | [diff] [blame] | 366 | return -EINVAL; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 367 | } |
| 368 | |
Linus Walleij | 9dfac4f | 2012-02-01 18:02:47 +0100 | [diff] [blame] | 369 | if (!maps[i].dev_name) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 370 | pr_debug("add system map %s function %s with no device\n", |
| 371 | maps[i].name, |
| 372 | maps[i].function); |
| 373 | else |
| 374 | pr_debug("register map %s, function %s\n", |
| 375 | maps[i].name, |
| 376 | maps[i].function); |
| 377 | } |
| 378 | |
Linus Walleij | 59b099b | 2011-11-30 13:28:14 +0100 | [diff] [blame] | 379 | /* |
| 380 | * Make a copy of the map array - string pointers will end up in the |
| 381 | * kernel const section anyway so these do not need to be deep copied. |
| 382 | */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 383 | if (!pinctrl_maps_num) { |
Linus Walleij | 59b099b | 2011-11-30 13:28:14 +0100 | [diff] [blame] | 384 | /* On first call, just copy them */ |
| 385 | tmp_maps = kmemdup(maps, |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 386 | sizeof(struct pinctrl_map) * num_maps, |
Linus Walleij | 59b099b | 2011-11-30 13:28:14 +0100 | [diff] [blame] | 387 | GFP_KERNEL); |
| 388 | if (!tmp_maps) |
| 389 | return -ENOMEM; |
| 390 | } else { |
| 391 | /* Subsequent calls, reallocate array to new size */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 392 | size_t oldsize = sizeof(struct pinctrl_map) * pinctrl_maps_num; |
| 393 | size_t newsize = sizeof(struct pinctrl_map) * num_maps; |
Linus Walleij | 97607d1 | 2011-11-29 12:52:39 +0100 | [diff] [blame] | 394 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 395 | tmp_maps = krealloc(pinctrl_maps, |
| 396 | oldsize + newsize, GFP_KERNEL); |
Linus Walleij | 59b099b | 2011-11-30 13:28:14 +0100 | [diff] [blame] | 397 | if (!tmp_maps) |
| 398 | return -ENOMEM; |
| 399 | memcpy((tmp_maps + oldsize), maps, newsize); |
| 400 | } |
| 401 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 402 | pinctrl_maps = tmp_maps; |
| 403 | pinctrl_maps_num += num_maps; |
Linus Walleij | 59b099b | 2011-11-30 13:28:14 +0100 | [diff] [blame] | 404 | return 0; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /** |
Tony Lindgren | de849ee | 2012-01-20 08:17:33 -0800 | [diff] [blame] | 408 | * 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] | 409 | * @pctldev: the device to take the pins on |
| 410 | * @func_selector: the function selector to acquire the pins for |
| 411 | * @group_selector: the group selector containing the pins to acquire |
| 412 | */ |
| 413 | static int acquire_pins(struct pinctrl_dev *pctldev, |
| 414 | unsigned func_selector, |
| 415 | unsigned group_selector) |
| 416 | { |
| 417 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
| 418 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; |
| 419 | const char *func = pmxops->get_function_name(pctldev, |
| 420 | func_selector); |
Stephen Warren | a5818a8 | 2011-10-19 16:19:25 -0600 | [diff] [blame] | 421 | const unsigned *pins; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 422 | unsigned num_pins; |
| 423 | int ret; |
| 424 | int i; |
| 425 | |
| 426 | ret = pctlops->get_group_pins(pctldev, group_selector, |
| 427 | &pins, &num_pins); |
| 428 | if (ret) |
| 429 | return ret; |
| 430 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 431 | dev_dbg(pctldev->dev, "requesting the %u pins from group %u\n", |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 432 | num_pins, group_selector); |
| 433 | |
| 434 | /* Try to allocate all pins in this group, one by one */ |
| 435 | for (i = 0; i < num_pins; i++) { |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 436 | ret = pin_request(pctldev, pins[i], func, NULL); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 437 | if (ret) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 438 | dev_err(pctldev->dev, |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 439 | "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] | 440 | pins[i], func ? : "(undefined)", |
| 441 | pinctrl_dev_get_name(pctldev)); |
| 442 | /* On error release all taken pins */ |
| 443 | i--; /* this pin just failed */ |
| 444 | for (; i >= 0; i--) |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 445 | pin_free(pctldev, pins[i], NULL); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 446 | return -ENODEV; |
| 447 | } |
| 448 | } |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * release_pins() - release pins taken by earlier acquirement |
Tony Lindgren | de849ee | 2012-01-20 08:17:33 -0800 | [diff] [blame] | 454 | * @pctldev: the device to free the pins on |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 455 | * @group_selector: the group selector containing the pins to free |
| 456 | */ |
| 457 | static void release_pins(struct pinctrl_dev *pctldev, |
| 458 | unsigned group_selector) |
| 459 | { |
| 460 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
Stephen Warren | a5818a8 | 2011-10-19 16:19:25 -0600 | [diff] [blame] | 461 | const unsigned *pins; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 462 | unsigned num_pins; |
| 463 | int ret; |
| 464 | int i; |
| 465 | |
| 466 | ret = pctlops->get_group_pins(pctldev, group_selector, |
| 467 | &pins, &num_pins); |
| 468 | if (ret) { |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 469 | 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] | 470 | group_selector); |
| 471 | return; |
| 472 | } |
| 473 | for (i = 0; i < num_pins; i++) |
Stephen Warren | 3712a3c | 2011-10-21 12:25:53 -0600 | [diff] [blame] | 474 | pin_free(pctldev, pins[i], NULL); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /** |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 478 | * pinmux_check_pin_group() - check function and pin group combo |
| 479 | * @pctldev: device to check the pin group vs function for |
| 480 | * @func_selector: the function selector to check the pin group for, we have |
| 481 | * already looked this up in the calling function |
| 482 | * @pin_group: the pin group to match to the function |
| 483 | * |
| 484 | * This function will check that the pinmux driver can supply the |
| 485 | * selected pin group for a certain function, returns the group selector if |
| 486 | * the group and function selector will work fine together, else returns |
| 487 | * negative |
| 488 | */ |
| 489 | static int pinmux_check_pin_group(struct pinctrl_dev *pctldev, |
| 490 | unsigned func_selector, |
| 491 | const char *pin_group) |
| 492 | { |
| 493 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; |
| 494 | const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; |
| 495 | int ret; |
| 496 | |
| 497 | /* |
| 498 | * If the driver does not support different pin groups for the |
| 499 | * functions, we only support group 0, and assume this exists. |
| 500 | */ |
| 501 | if (!pctlops || !pctlops->list_groups) |
| 502 | return 0; |
| 503 | |
| 504 | /* |
| 505 | * Passing NULL (no specific group) will select the first and |
| 506 | * hopefully only group of pins available for this function. |
| 507 | */ |
| 508 | if (!pin_group) { |
| 509 | char const * const *groups; |
| 510 | unsigned num_groups; |
| 511 | |
| 512 | ret = pmxops->get_function_groups(pctldev, func_selector, |
| 513 | &groups, &num_groups); |
| 514 | if (ret) |
| 515 | return ret; |
| 516 | if (num_groups < 1) |
| 517 | return -EINVAL; |
Linus Walleij | 7afde8b | 2011-10-19 17:07:16 +0200 | [diff] [blame] | 518 | ret = pinctrl_get_group_selector(pctldev, groups[0]); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 519 | if (ret < 0) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 520 | dev_err(pctldev->dev, |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 521 | "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] | 522 | pmxops->get_function_name(pctldev, func_selector), |
| 523 | groups[0]); |
| 524 | return ret; |
| 525 | } |
| 526 | |
| 527 | if (num_groups > 1) |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 528 | dev_dbg(pctldev->dev, |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 529 | "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] | 530 | pmxops->get_function_name(pctldev, func_selector), |
| 531 | groups[0], |
| 532 | ret); |
| 533 | |
| 534 | return ret; |
| 535 | } |
| 536 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 537 | dev_dbg(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 538 | "check if we have pin group %s on controller %s\n", |
| 539 | pin_group, pinctrl_dev_get_name(pctldev)); |
| 540 | |
Linus Walleij | 7afde8b | 2011-10-19 17:07:16 +0200 | [diff] [blame] | 541 | ret = pinctrl_get_group_selector(pctldev, pin_group); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 542 | if (ret < 0) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 543 | dev_dbg(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 544 | "%s does not support pin group %s with function %s\n", |
| 545 | pinctrl_dev_get_name(pctldev), |
| 546 | pin_group, |
| 547 | pmxops->get_function_name(pctldev, func_selector)); |
| 548 | } |
| 549 | return ret; |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * pinmux_search_function() - check pin control driver for a certain function |
| 554 | * @pctldev: device to check for function and position |
| 555 | * @map: function map containing the function and position to look for |
| 556 | * @func_selector: returns the applicable function selector if found |
| 557 | * @group_selector: returns the applicable group selector if found |
| 558 | * |
| 559 | * This will search the pinmux driver for an applicable |
| 560 | * function with a specific pin group, returns 0 if these can be mapped |
| 561 | * negative otherwise |
| 562 | */ |
| 563 | static int pinmux_search_function(struct pinctrl_dev *pctldev, |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 564 | struct pinctrl_map const *map, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 565 | unsigned *func_selector, |
| 566 | unsigned *group_selector) |
| 567 | { |
| 568 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 569 | unsigned selector = 0; |
| 570 | |
| 571 | /* See if this pctldev has this function */ |
| 572 | while (ops->list_functions(pctldev, selector) >= 0) { |
| 573 | const char *fname = ops->get_function_name(pctldev, |
| 574 | selector); |
| 575 | int ret; |
| 576 | |
| 577 | if (!strcmp(map->function, fname)) { |
| 578 | /* Found the function, check pin group */ |
| 579 | ret = pinmux_check_pin_group(pctldev, selector, |
| 580 | map->group); |
| 581 | if (ret < 0) |
| 582 | return ret; |
| 583 | |
| 584 | /* This function and group selector can be used */ |
| 585 | *func_selector = selector; |
| 586 | *group_selector = ret; |
| 587 | return 0; |
| 588 | |
| 589 | } |
| 590 | selector++; |
| 591 | } |
| 592 | |
| 593 | pr_err("%s does not support function %s\n", |
| 594 | pinctrl_dev_get_name(pctldev), map->function); |
| 595 | return -EINVAL; |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * pinmux_enable_muxmap() - enable a map entry for a certain pinmux |
| 600 | */ |
| 601 | static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev, |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 602 | struct pinctrl *p, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 603 | struct device *dev, |
| 604 | const char *devname, |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 605 | struct pinctrl_map const *map) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 606 | { |
| 607 | unsigned func_selector; |
| 608 | unsigned group_selector; |
| 609 | struct pinmux_group *grp; |
| 610 | int ret; |
| 611 | |
| 612 | /* |
| 613 | * Note that we're not locking the pinmux mutex here, because |
| 614 | * this is only called at pinmux initialization time when it |
| 615 | * has not been added to any list and thus is not reachable |
| 616 | * by anyone else. |
| 617 | */ |
| 618 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 619 | if (p->pctldev && p->pctldev != pctldev) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 620 | dev_err(pctldev->dev, |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 621 | "different pin control devices given for device %s, function %s\n", |
| 622 | devname, map->function); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 623 | return -EINVAL; |
| 624 | } |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 625 | p->dev = dev; |
| 626 | p->pctldev = pctldev; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 627 | |
| 628 | /* Now go into the driver and try to match a function and group */ |
| 629 | ret = pinmux_search_function(pctldev, map, &func_selector, |
| 630 | &group_selector); |
| 631 | if (ret < 0) |
| 632 | return ret; |
| 633 | |
| 634 | /* |
| 635 | * If the function selector is already set, it needs to be identical, |
| 636 | * we support several groups with one function but not several |
| 637 | * functions with one or several groups in the same pinmux. |
| 638 | */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 639 | if (p->func_selector != UINT_MAX && |
| 640 | p->func_selector != func_selector) { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 641 | dev_err(pctldev->dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 642 | "dual function defines in the map for device %s\n", |
| 643 | devname); |
| 644 | return -EINVAL; |
| 645 | } |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 646 | p->func_selector = func_selector; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 647 | |
| 648 | /* Now add this group selector, we may have many of them */ |
| 649 | grp = kmalloc(sizeof(struct pinmux_group), GFP_KERNEL); |
| 650 | if (!grp) |
| 651 | return -ENOMEM; |
| 652 | grp->group_selector = group_selector; |
| 653 | ret = acquire_pins(pctldev, func_selector, group_selector); |
| 654 | if (ret) { |
| 655 | kfree(grp); |
| 656 | return ret; |
| 657 | } |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 658 | list_add(&grp->node, &p->groups); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 659 | |
| 660 | return 0; |
| 661 | } |
| 662 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 663 | static void pinmux_free_groups(struct pinctrl *p) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 664 | { |
| 665 | struct list_head *node, *tmp; |
| 666 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 667 | list_for_each_safe(node, tmp, &p->groups) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 668 | struct pinmux_group *grp = |
| 669 | list_entry(node, struct pinmux_group, node); |
| 670 | /* Release all pins taken by this group */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 671 | release_pins(p->pctldev, grp->group_selector); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 672 | list_del(node); |
| 673 | kfree(grp); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | /** |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 678 | * pinctrl_get() - retrieves the pin controller handle for a certain device |
| 679 | * @dev: the device to get the pin controller handle for |
| 680 | * @name: an optional specific control mapping name or NULL, the name is only |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 681 | * needed if you want to have more than one mapping per device, or if you |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 682 | * need an anonymous pin control (not tied to any specific device) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 683 | */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 684 | struct pinctrl *pinctrl_get(struct device *dev, const char *name) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 685 | { |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 686 | struct pinctrl_map const *map = NULL; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 687 | struct pinctrl_dev *pctldev = NULL; |
| 688 | const char *devname = NULL; |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 689 | struct pinctrl *p; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 690 | bool found_map; |
| 691 | unsigned num_maps = 0; |
| 692 | int ret = -ENODEV; |
| 693 | int i; |
| 694 | |
| 695 | /* We must have dev or ID or both */ |
| 696 | if (!dev && !name) |
| 697 | return ERR_PTR(-EINVAL); |
| 698 | |
| 699 | if (dev) |
| 700 | devname = dev_name(dev); |
| 701 | |
| 702 | pr_debug("get mux %s for device %s\n", name, |
| 703 | devname ? devname : "(none)"); |
| 704 | |
| 705 | /* |
| 706 | * create the state cookie holder struct pinmux for each |
| 707 | * mapping, this is what consumers will get when requesting |
| 708 | * a pinmux handle with pinmux_get() |
| 709 | */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 710 | p = kzalloc(sizeof(struct pinctrl), GFP_KERNEL); |
| 711 | if (p == NULL) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 712 | return ERR_PTR(-ENOMEM); |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 713 | mutex_init(&p->mutex); |
| 714 | p->func_selector = UINT_MAX; |
| 715 | INIT_LIST_HEAD(&p->groups); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 716 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 717 | /* Iterate over the pin control maps to locate the right ones */ |
| 718 | for (i = 0; i < pinctrl_maps_num; i++) { |
| 719 | map = &pinctrl_maps[i]; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 720 | found_map = false; |
| 721 | |
| 722 | /* |
| 723 | * First, try to find the pctldev given in the map |
| 724 | */ |
Linus Walleij | 9dfac4f | 2012-02-01 18:02:47 +0100 | [diff] [blame] | 725 | pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 726 | if (!pctldev) { |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 727 | pr_warning("could not find a pinctrl device for pinmux function %s, fishy, they shall all have one\n", |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 728 | map->function); |
| 729 | pr_warning("given pinctrl device name: %s", |
Linus Walleij | 9dfac4f | 2012-02-01 18:02:47 +0100 | [diff] [blame] | 730 | map->ctrl_dev_name); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 731 | |
| 732 | /* Continue to check the other mappings anyway... */ |
| 733 | continue; |
| 734 | } |
| 735 | |
| 736 | pr_debug("in map, found pctldev %s to handle function %s", |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 737 | dev_name(pctldev->dev), map->function); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 738 | |
| 739 | |
| 740 | /* |
| 741 | * If we're looking for a specific named map, this must match, |
| 742 | * else we loop and look for the next. |
| 743 | */ |
| 744 | if (name != NULL) { |
| 745 | if (map->name == NULL) |
| 746 | continue; |
| 747 | if (strcmp(map->name, name)) |
| 748 | continue; |
| 749 | } |
| 750 | |
| 751 | /* |
| 752 | * This is for the case where no device name is given, we |
| 753 | * already know that the function name matches from above |
| 754 | * code. |
| 755 | */ |
| 756 | if (!map->dev_name && (name != NULL)) |
| 757 | found_map = true; |
| 758 | |
| 759 | /* If the mapping has a device set up it must match */ |
| 760 | if (map->dev_name && |
| 761 | (!devname || !strcmp(map->dev_name, devname))) |
| 762 | /* MATCH! */ |
| 763 | found_map = true; |
| 764 | |
| 765 | /* If this map is applicable, then apply it */ |
| 766 | if (found_map) { |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 767 | ret = pinmux_enable_muxmap(pctldev, p, dev, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 768 | devname, map); |
| 769 | if (ret) { |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 770 | pinmux_free_groups(p); |
| 771 | kfree(p); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 772 | return ERR_PTR(ret); |
| 773 | } |
| 774 | num_maps++; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | |
| 779 | /* We should have atleast one map, right */ |
| 780 | if (!num_maps) { |
| 781 | pr_err("could not find any mux maps for device %s, ID %s\n", |
| 782 | devname ? devname : "(anonymous)", |
| 783 | name ? name : "(undefined)"); |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 784 | kfree(p); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 785 | return ERR_PTR(-EINVAL); |
| 786 | } |
| 787 | |
| 788 | pr_debug("found %u mux maps for device %s, UD %s\n", |
| 789 | num_maps, |
| 790 | devname ? devname : "(anonymous)", |
| 791 | name ? name : "(undefined)"); |
| 792 | |
| 793 | /* Add the pinmux to the global list */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 794 | mutex_lock(&pinctrl_list_mutex); |
| 795 | list_add(&p->node, &pinctrl_list); |
| 796 | mutex_unlock(&pinctrl_list_mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 797 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 798 | return p; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 799 | } |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 800 | EXPORT_SYMBOL_GPL(pinctrl_get); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 801 | |
| 802 | /** |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 803 | * pinctrl_put() - release a previously claimed pin control handle |
| 804 | * @p: a pin control handle previously claimed by pinctrl_get() |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 805 | */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 806 | void pinctrl_put(struct pinctrl *p) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 807 | { |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 808 | if (p == NULL) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 809 | return; |
| 810 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 811 | mutex_lock(&p->mutex); |
| 812 | if (p->usecount) |
| 813 | pr_warn("releasing pin control handle with active users!\n"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 814 | /* Free the groups and all acquired pins */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 815 | pinmux_free_groups(p); |
| 816 | mutex_unlock(&p->mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 817 | |
| 818 | /* Remove from list */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 819 | mutex_lock(&pinctrl_list_mutex); |
| 820 | list_del(&p->node); |
| 821 | mutex_unlock(&pinctrl_list_mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 822 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 823 | kfree(p); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 824 | } |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 825 | EXPORT_SYMBOL_GPL(pinctrl_put); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 826 | |
| 827 | /** |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 828 | * pinctrl_enable() - enable a certain pin controller setting |
| 829 | * @p: the pin control handle to enable, previously claimed by pinctrl_get() |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 830 | */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 831 | int pinctrl_enable(struct pinctrl *p) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 832 | { |
| 833 | int ret = 0; |
| 834 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 835 | if (p == NULL) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 836 | return -EINVAL; |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 837 | mutex_lock(&p->mutex); |
| 838 | if (p->usecount++ == 0) { |
| 839 | struct pinctrl_dev *pctldev = p->pctldev; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 840 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 841 | struct pinmux_group *grp; |
| 842 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 843 | list_for_each_entry(grp, &p->groups, node) { |
| 844 | ret = ops->enable(pctldev, p->func_selector, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 845 | grp->group_selector); |
| 846 | if (ret) { |
| 847 | /* |
| 848 | * TODO: call disable() on all groups we called |
| 849 | * enable() on to this point? |
| 850 | */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 851 | p->usecount--; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 852 | break; |
| 853 | } |
| 854 | } |
| 855 | } |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 856 | mutex_unlock(&p->mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 857 | return ret; |
| 858 | } |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 859 | EXPORT_SYMBOL_GPL(pinctrl_enable); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 860 | |
| 861 | /** |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 862 | * pinctrl_disable() - disable a certain pin control setting |
| 863 | * @p: the pin control handle to disable, previously claimed by pinctrl_get() |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 864 | */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 865 | void pinctrl_disable(struct pinctrl *p) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 866 | { |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 867 | if (p == NULL) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 868 | return; |
| 869 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 870 | mutex_lock(&p->mutex); |
| 871 | if (--p->usecount == 0) { |
| 872 | struct pinctrl_dev *pctldev = p->pctldev; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 873 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 874 | struct pinmux_group *grp; |
| 875 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 876 | list_for_each_entry(grp, &p->groups, node) { |
| 877 | ops->disable(pctldev, p->func_selector, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 878 | grp->group_selector); |
| 879 | } |
| 880 | } |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 881 | mutex_unlock(&p->mutex); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 882 | } |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 883 | EXPORT_SYMBOL_GPL(pinctrl_disable); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 884 | |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 885 | int pinmux_check_ops(struct pinctrl_dev *pctldev) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 886 | { |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 887 | const struct pinmux_ops *ops = pctldev->desc->pmxops; |
| 888 | unsigned selector = 0; |
| 889 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 890 | /* Check that we implement required operations */ |
| 891 | if (!ops->list_functions || |
| 892 | !ops->get_function_name || |
| 893 | !ops->get_function_groups || |
| 894 | !ops->enable || |
| 895 | !ops->disable) |
| 896 | return -EINVAL; |
| 897 | |
Tony Lindgren | b9130b7 | 2012-01-24 16:28:08 -0800 | [diff] [blame] | 898 | /* Check that all functions registered have names */ |
| 899 | while (ops->list_functions(pctldev, selector) >= 0) { |
| 900 | const char *fname = ops->get_function_name(pctldev, |
| 901 | selector); |
| 902 | if (!fname) { |
| 903 | pr_err("pinmux ops has no name for function%u\n", |
| 904 | selector); |
| 905 | return -EINVAL; |
| 906 | } |
| 907 | selector++; |
| 908 | } |
| 909 | |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 910 | return 0; |
| 911 | } |
| 912 | |
| 913 | /* Hog a single map entry and add to the hoglist */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 914 | static int pinctrl_hog_map(struct pinctrl_dev *pctldev, |
| 915 | struct pinctrl_map const *map) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 916 | { |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 917 | struct pinctrl_hog *hog; |
| 918 | struct pinctrl *p; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 919 | int ret; |
| 920 | |
Linus Walleij | 9dfac4f | 2012-02-01 18:02:47 +0100 | [diff] [blame] | 921 | if (map->dev_name) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 922 | /* |
| 923 | * TODO: the day we have device tree support, we can |
| 924 | * traverse the device tree and hog to specific device nodes |
| 925 | * without any problems, so then we can hog pinmuxes for |
| 926 | * all devices that just want a static pin mux at this point. |
| 927 | */ |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 928 | dev_err(pctldev->dev, "map %s wants to hog a non-system pinmux, this is not going to work\n", |
| 929 | map->name); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 930 | return -EINVAL; |
| 931 | } |
| 932 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 933 | hog = kzalloc(sizeof(struct pinctrl_hog), GFP_KERNEL); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 934 | if (!hog) |
| 935 | return -ENOMEM; |
| 936 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 937 | p = pinctrl_get(NULL, map->name); |
| 938 | if (IS_ERR(p)) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 939 | kfree(hog); |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 940 | dev_err(pctldev->dev, |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 941 | "could not get the %s pin control mapping for hogging\n", |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 942 | map->name); |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 943 | return PTR_ERR(p); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 944 | } |
| 945 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 946 | ret = pinctrl_enable(p); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 947 | if (ret) { |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 948 | pinctrl_put(p); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 949 | kfree(hog); |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 950 | dev_err(pctldev->dev, |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 951 | "could not enable the %s pin control mapping for hogging\n", |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 952 | map->name); |
| 953 | return ret; |
| 954 | } |
| 955 | |
| 956 | hog->map = map; |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 957 | hog->p = p; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 958 | |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 959 | dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 960 | map->function); |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 961 | mutex_lock(&pctldev->pinctrl_hogs_lock); |
| 962 | list_add(&hog->node, &pctldev->pinctrl_hogs); |
| 963 | mutex_unlock(&pctldev->pinctrl_hogs_lock); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 964 | |
| 965 | return 0; |
| 966 | } |
| 967 | |
| 968 | /** |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 969 | * pinctrl_hog_maps() - hog specific map entries on controller device |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 970 | * @pctldev: the pin control device to hog entries on |
| 971 | * |
| 972 | * When the pin controllers are registered, there may be some specific pinmux |
| 973 | * map entries that need to be hogged, i.e. get+enabled until the system shuts |
| 974 | * down. |
| 975 | */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 976 | int pinctrl_hog_maps(struct pinctrl_dev *pctldev) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 977 | { |
Stephen Warren | 51cd24e | 2011-12-09 16:59:05 -0700 | [diff] [blame] | 978 | struct device *dev = pctldev->dev; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 979 | const char *devname = dev_name(dev); |
| 980 | int ret; |
| 981 | int i; |
| 982 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 983 | INIT_LIST_HEAD(&pctldev->pinctrl_hogs); |
| 984 | mutex_init(&pctldev->pinctrl_hogs_lock); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 985 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 986 | for (i = 0; i < pinctrl_maps_num; i++) { |
| 987 | struct pinctrl_map const *map = &pinctrl_maps[i]; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 988 | |
Tony Lindgren | 9e2551e | 2012-01-20 07:43:53 -0800 | [diff] [blame] | 989 | if (!map->hog_on_boot) |
| 990 | continue; |
| 991 | |
Linus Walleij | 9dfac4f | 2012-02-01 18:02:47 +0100 | [diff] [blame] | 992 | if (map->ctrl_dev_name && |
| 993 | !strcmp(map->ctrl_dev_name, devname)) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 994 | /* OK time to hog! */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 995 | ret = pinctrl_hog_map(pctldev, map); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 996 | if (ret) |
| 997 | return ret; |
| 998 | } |
| 999 | } |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | /** |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1004 | * pinctrl_unhog_maps() - unhog specific map entries on controller device |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1005 | * @pctldev: the pin control device to unhog entries on |
| 1006 | */ |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1007 | void pinctrl_unhog_maps(struct pinctrl_dev *pctldev) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1008 | { |
| 1009 | struct list_head *node, *tmp; |
| 1010 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1011 | mutex_lock(&pctldev->pinctrl_hogs_lock); |
| 1012 | list_for_each_safe(node, tmp, &pctldev->pinctrl_hogs) { |
| 1013 | struct pinctrl_hog *hog = |
| 1014 | list_entry(node, struct pinctrl_hog, node); |
| 1015 | pinctrl_disable(hog->p); |
| 1016 | pinctrl_put(hog->p); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1017 | list_del(node); |
| 1018 | kfree(hog); |
| 1019 | } |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1020 | mutex_unlock(&pctldev->pinctrl_hogs_lock); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | #ifdef CONFIG_DEBUG_FS |
| 1024 | |
| 1025 | /* Called from pincontrol core */ |
| 1026 | static int pinmux_functions_show(struct seq_file *s, void *what) |
| 1027 | { |
| 1028 | struct pinctrl_dev *pctldev = s->private; |
| 1029 | const struct pinmux_ops *pmxops = pctldev->desc->pmxops; |
| 1030 | unsigned func_selector = 0; |
| 1031 | |
| 1032 | while (pmxops->list_functions(pctldev, func_selector) >= 0) { |
| 1033 | const char *func = pmxops->get_function_name(pctldev, |
| 1034 | func_selector); |
| 1035 | const char * const *groups; |
| 1036 | unsigned num_groups; |
| 1037 | int ret; |
| 1038 | int i; |
| 1039 | |
| 1040 | ret = pmxops->get_function_groups(pctldev, func_selector, |
| 1041 | &groups, &num_groups); |
| 1042 | if (ret) |
| 1043 | seq_printf(s, "function %s: COULD NOT GET GROUPS\n", |
| 1044 | func); |
| 1045 | |
| 1046 | seq_printf(s, "function: %s, groups = [ ", func); |
| 1047 | for (i = 0; i < num_groups; i++) |
| 1048 | seq_printf(s, "%s ", groups[i]); |
| 1049 | seq_puts(s, "]\n"); |
| 1050 | |
| 1051 | func_selector++; |
| 1052 | |
| 1053 | } |
| 1054 | |
| 1055 | return 0; |
| 1056 | } |
| 1057 | |
| 1058 | static int pinmux_pins_show(struct seq_file *s, void *what) |
| 1059 | { |
| 1060 | struct pinctrl_dev *pctldev = s->private; |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 1061 | unsigned i, pin; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1062 | |
| 1063 | seq_puts(s, "Pinmux settings per pin\n"); |
| 1064 | seq_puts(s, "Format: pin (name): pinmuxfunction\n"); |
| 1065 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 1066 | /* The pin number can be retrived from the pin controller descriptor */ |
| 1067 | for (i = 0; i < pctldev->desc->npins; i++) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1068 | |
| 1069 | struct pin_desc *desc; |
| 1070 | |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 1071 | pin = pctldev->desc->pins[i].number; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1072 | desc = pin_desc_get(pctldev, pin); |
Chanho Park | 706e852 | 2012-01-03 16:47:50 +0900 | [diff] [blame] | 1073 | /* Skip if we cannot search the pin */ |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1074 | if (desc == NULL) |
| 1075 | continue; |
| 1076 | |
| 1077 | seq_printf(s, "pin %d (%s): %s\n", pin, |
| 1078 | desc->name ? desc->name : "unnamed", |
Stephen Warren | 5d2eaf8 | 2011-10-19 16:19:28 -0600 | [diff] [blame] | 1079 | desc->mux_function ? desc->mux_function |
| 1080 | : "UNCLAIMED"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
| 1086 | static int pinmux_hogs_show(struct seq_file *s, void *what) |
| 1087 | { |
| 1088 | struct pinctrl_dev *pctldev = s->private; |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1089 | struct pinctrl_hog *hog; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1090 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1091 | seq_puts(s, "Pin control map hogs held by device\n"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1092 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1093 | list_for_each_entry(hog, &pctldev->pinctrl_hogs, node) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1094 | seq_printf(s, "%s\n", hog->map->name); |
| 1095 | |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | static int pinmux_show(struct seq_file *s, void *what) |
| 1100 | { |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1101 | struct pinctrl *p; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1102 | |
| 1103 | seq_puts(s, "Requested pinmuxes and their maps:\n"); |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1104 | list_for_each_entry(p, &pinctrl_list, node) { |
| 1105 | struct pinctrl_dev *pctldev = p->pctldev; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1106 | const struct pinmux_ops *pmxops; |
| 1107 | const struct pinctrl_ops *pctlops; |
| 1108 | struct pinmux_group *grp; |
| 1109 | |
| 1110 | if (!pctldev) { |
| 1111 | seq_puts(s, "NO PIN CONTROLLER DEVICE\n"); |
| 1112 | continue; |
| 1113 | } |
| 1114 | |
| 1115 | pmxops = pctldev->desc->pmxops; |
| 1116 | pctlops = pctldev->desc->pctlops; |
| 1117 | |
| 1118 | seq_printf(s, "device: %s function: %s (%u),", |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1119 | pinctrl_dev_get_name(p->pctldev), |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 1120 | pmxops->get_function_name(pctldev, |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1121 | p->func_selector), |
| 1122 | p->func_selector); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1123 | |
| 1124 | seq_printf(s, " groups: ["); |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1125 | list_for_each_entry(grp, &p->groups, node) { |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1126 | seq_printf(s, " %s (%u)", |
Uwe Kleine-König | f9d41d7 | 2012-01-19 22:42:48 +0100 | [diff] [blame] | 1127 | pctlops->get_group_name(pctldev, |
| 1128 | grp->group_selector), |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1129 | grp->group_selector); |
| 1130 | } |
| 1131 | seq_printf(s, " ]"); |
| 1132 | |
| 1133 | seq_printf(s, " users: %u map-> %s\n", |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1134 | p->usecount, |
| 1135 | p->dev ? dev_name(p->dev) : "(system)"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1141 | static int pinctrl_maps_show(struct seq_file *s, void *what) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1142 | { |
| 1143 | int i; |
| 1144 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1145 | seq_puts(s, "Pinctrl maps:\n"); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1146 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1147 | for (i = 0; i < pinctrl_maps_num; i++) { |
| 1148 | struct pinctrl_map const *map = &pinctrl_maps[i]; |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1149 | |
| 1150 | seq_printf(s, "%s:\n", map->name); |
Linus Walleij | 9dfac4f | 2012-02-01 18:02:47 +0100 | [diff] [blame] | 1151 | if (map->dev_name) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1152 | seq_printf(s, " device: %s\n", |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1153 | map->dev_name); |
| 1154 | else |
| 1155 | seq_printf(s, " SYSTEM MUX\n"); |
| 1156 | seq_printf(s, " controlling device %s\n", |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1157 | map->ctrl_dev_name); |
| 1158 | seq_printf(s, " function: %s\n", map->function); |
| 1159 | seq_printf(s, " group: %s\n", map->group ? map->group : |
| 1160 | "(default)"); |
| 1161 | } |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | static int pinmux_functions_open(struct inode *inode, struct file *file) |
| 1166 | { |
| 1167 | return single_open(file, pinmux_functions_show, inode->i_private); |
| 1168 | } |
| 1169 | |
| 1170 | static int pinmux_pins_open(struct inode *inode, struct file *file) |
| 1171 | { |
| 1172 | return single_open(file, pinmux_pins_show, inode->i_private); |
| 1173 | } |
| 1174 | |
| 1175 | static int pinmux_hogs_open(struct inode *inode, struct file *file) |
| 1176 | { |
| 1177 | return single_open(file, pinmux_hogs_show, inode->i_private); |
| 1178 | } |
| 1179 | |
| 1180 | static int pinmux_open(struct inode *inode, struct file *file) |
| 1181 | { |
| 1182 | return single_open(file, pinmux_show, NULL); |
| 1183 | } |
| 1184 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1185 | static int pinctrl_maps_open(struct inode *inode, struct file *file) |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1186 | { |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1187 | return single_open(file, pinctrl_maps_show, NULL); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | static const struct file_operations pinmux_functions_ops = { |
| 1191 | .open = pinmux_functions_open, |
| 1192 | .read = seq_read, |
| 1193 | .llseek = seq_lseek, |
| 1194 | .release = single_release, |
| 1195 | }; |
| 1196 | |
| 1197 | static const struct file_operations pinmux_pins_ops = { |
| 1198 | .open = pinmux_pins_open, |
| 1199 | .read = seq_read, |
| 1200 | .llseek = seq_lseek, |
| 1201 | .release = single_release, |
| 1202 | }; |
| 1203 | |
| 1204 | static const struct file_operations pinmux_hogs_ops = { |
| 1205 | .open = pinmux_hogs_open, |
| 1206 | .read = seq_read, |
| 1207 | .llseek = seq_lseek, |
| 1208 | .release = single_release, |
| 1209 | }; |
| 1210 | |
| 1211 | static const struct file_operations pinmux_ops = { |
| 1212 | .open = pinmux_open, |
| 1213 | .read = seq_read, |
| 1214 | .llseek = seq_lseek, |
| 1215 | .release = single_release, |
| 1216 | }; |
| 1217 | |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1218 | static const struct file_operations pinctrl_maps_ops = { |
| 1219 | .open = pinctrl_maps_open, |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1220 | .read = seq_read, |
| 1221 | .llseek = seq_lseek, |
| 1222 | .release = single_release, |
| 1223 | }; |
| 1224 | |
| 1225 | void pinmux_init_device_debugfs(struct dentry *devroot, |
| 1226 | struct pinctrl_dev *pctldev) |
| 1227 | { |
| 1228 | debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO, |
| 1229 | devroot, pctldev, &pinmux_functions_ops); |
| 1230 | debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO, |
| 1231 | devroot, pctldev, &pinmux_pins_ops); |
| 1232 | debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO, |
| 1233 | devroot, pctldev, &pinmux_hogs_ops); |
| 1234 | } |
| 1235 | |
| 1236 | void pinmux_init_debugfs(struct dentry *subsys_root) |
| 1237 | { |
| 1238 | debugfs_create_file("pinmuxes", S_IFREG | S_IRUGO, |
| 1239 | subsys_root, NULL, &pinmux_ops); |
Linus Walleij | e93bcee | 2012-02-09 07:23:28 +0100 | [diff] [blame^] | 1240 | debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO, |
| 1241 | subsys_root, NULL, &pinctrl_maps_ops); |
Linus Walleij | 2744e8a | 2011-05-02 20:50:54 +0200 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | #endif /* CONFIG_DEBUG_FS */ |