blob: 5a09cd20220862cc188e1ba29aafd49c38ba2458 [file] [log] [blame]
Linus Walleij2744e8a2011-05-02 20:50:54 +02001/*
2 * Core driver for the pin muxing portions of the pin control subsystem
3 *
Linus Walleije93bcee2012-02-09 07:23:28 +01004 * Copyright (C) 2011-2012 ST-Ericsson SA
Linus Walleij2744e8a2011-05-02 20:50:54 +02005 * 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 Walleij97607d12011-11-29 12:52:39 +010024#include <linux/string.h>
Linus Walleij2744e8a2011-05-02 20:50:54 +020025#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 Walleijbefe5bd2012-02-09 19:47:48 +010031#include "pinmux.h"
Linus Walleij2744e8a2011-05-02 20:50:54 +020032
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 */
38struct pinmux_group {
39 struct list_head node;
40 unsigned group_selector;
41};
42
43/**
Linus Walleij2744e8a2011-05-02 20:50:54 +020044 * pin_request() - request a single pin to be muxed in, typically for GPIO
45 * @pin: the pin number in the global pin space
46 * @function: a functional name to give to this pin, passed to the driver
47 * so it knows what function to mux in, e.g. the string "gpioNN"
48 * means that you want to mux in the pin for use as GPIO number NN
Linus Walleij2744e8a2011-05-02 20:50:54 +020049 * @gpio_range: the range matching the GPIO pin if this is a request for a
50 * single GPIO pin
51 */
52static int pin_request(struct pinctrl_dev *pctldev,
Stephen Warren3712a3c2011-10-21 12:25:53 -060053 int pin, const char *function,
Linus Walleij2744e8a2011-05-02 20:50:54 +020054 struct pinctrl_gpio_range *gpio_range)
55{
56 struct pin_desc *desc;
57 const struct pinmux_ops *ops = pctldev->desc->pmxops;
58 int status = -EINVAL;
59
Stephen Warren51cd24e2011-12-09 16:59:05 -070060 dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, function);
Linus Walleij2744e8a2011-05-02 20:50:54 +020061
Linus Walleij2744e8a2011-05-02 20:50:54 +020062 desc = pin_desc_get(pctldev, pin);
63 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070064 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +020065 "pin is not registered so it cannot be requested\n");
66 goto out;
67 }
68
Marek Beliskod2f6a1c2011-10-26 22:57:20 +020069 if (!function) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070070 dev_err(pctldev->dev, "no function name given\n");
Marek Beliskod2f6a1c2011-10-26 22:57:20 +020071 return -EINVAL;
72 }
73
Linus Walleij2744e8a2011-05-02 20:50:54 +020074 spin_lock(&desc->lock);
Stephen Warren5d2eaf82011-10-19 16:19:28 -060075 if (desc->mux_function) {
Linus Walleij2744e8a2011-05-02 20:50:54 +020076 spin_unlock(&desc->lock);
Stephen Warren51cd24e2011-12-09 16:59:05 -070077 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +020078 "pin already requested\n");
79 goto out;
80 }
Stephen Warren5d2eaf82011-10-19 16:19:28 -060081 desc->mux_function = function;
Linus Walleij2744e8a2011-05-02 20:50:54 +020082 spin_unlock(&desc->lock);
83
84 /* Let each pin increase references to this module */
85 if (!try_module_get(pctldev->owner)) {
Stephen Warren51cd24e2011-12-09 16:59:05 -070086 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +020087 "could not increase module refcount for pin %d\n",
88 pin);
89 status = -EINVAL;
90 goto out_free_pin;
91 }
92
93 /*
94 * If there is no kind of request function for the pin we just assume
95 * we got it by default and proceed.
96 */
Stephen Warren3712a3c2011-10-21 12:25:53 -060097 if (gpio_range && ops->gpio_request_enable)
Linus Walleij2744e8a2011-05-02 20:50:54 +020098 /* This requests and enables a single GPIO pin */
99 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
100 else if (ops->request)
101 status = ops->request(pctldev, pin);
102 else
103 status = 0;
104
105 if (status)
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100106 dev_err(pctldev->dev, "->request on device %s failed for pin %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200107 pctldev->desc->name, pin);
108out_free_pin:
109 if (status) {
110 spin_lock(&desc->lock);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600111 desc->mux_function = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200112 spin_unlock(&desc->lock);
113 }
114out:
115 if (status)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700116 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200117 pin, function ? : "?", status);
118
119 return status;
120}
121
122/**
123 * pin_free() - release a single muxed in pin so something else can be muxed
124 * @pctldev: pin controller device handling this pin
125 * @pin: the pin to free
Stephen Warren3712a3c2011-10-21 12:25:53 -0600126 * @gpio_range: the range matching the GPIO pin if this is a request for a
127 * single GPIO pin
Linus Walleij336cdba02011-11-10 09:27:41 +0100128 *
129 * This function returns a pointer to the function name in use. This is used
130 * for callers that dynamically allocate a function name so it can be freed
131 * once the pin is free. This is done for GPIO request functions.
Linus Walleij2744e8a2011-05-02 20:50:54 +0200132 */
Stephen Warren3712a3c2011-10-21 12:25:53 -0600133static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
134 struct pinctrl_gpio_range *gpio_range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200135{
136 const struct pinmux_ops *ops = pctldev->desc->pmxops;
137 struct pin_desc *desc;
Stephen Warren3712a3c2011-10-21 12:25:53 -0600138 const char *func;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200139
140 desc = pin_desc_get(pctldev, pin);
141 if (desc == NULL) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700142 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200143 "pin is not registered so it cannot be freed\n");
Stephen Warren3712a3c2011-10-21 12:25:53 -0600144 return NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200145 }
146
Stephen Warren3712a3c2011-10-21 12:25:53 -0600147 /*
148 * If there is no kind of request function for the pin we just assume
149 * we got it by default and proceed.
150 */
151 if (gpio_range && ops->gpio_disable_free)
152 ops->gpio_disable_free(pctldev, gpio_range, pin);
153 else if (ops->free)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200154 ops->free(pctldev, pin);
155
156 spin_lock(&desc->lock);
Stephen Warren3712a3c2011-10-21 12:25:53 -0600157 func = desc->mux_function;
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600158 desc->mux_function = NULL;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200159 spin_unlock(&desc->lock);
160 module_put(pctldev->owner);
Stephen Warren3712a3c2011-10-21 12:25:53 -0600161
162 return func;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200163}
164
165/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100166 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
167 * @pctldev: pin controller device affected
168 * @pin: the pin to mux in for GPIO
169 * @range: the applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200170 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100171int pinmux_request_gpio(struct pinctrl_dev *pctldev,
172 struct pinctrl_gpio_range *range,
173 unsigned pin, unsigned gpio)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200174{
175 char gpiostr[16];
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600176 const char *function;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200177 int ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200178
179 /* Conjure some name stating what chip and pin this is taken by */
180 snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
181
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600182 function = kstrdup(gpiostr, GFP_KERNEL);
183 if (!function)
184 return -EINVAL;
185
Stephen Warren3712a3c2011-10-21 12:25:53 -0600186 ret = pin_request(pctldev, pin, function, range);
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600187 if (ret < 0)
188 kfree(function);
189
190 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200191}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200192
193/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100194 * pinmux_free_gpio() - release a pin from GPIO muxing
195 * @pctldev: the pin controller device for the pin
196 * @pin: the affected currently GPIO-muxed in pin
197 * @range: applicable GPIO range
Linus Walleij2744e8a2011-05-02 20:50:54 +0200198 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100199void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
200 struct pinctrl_gpio_range *range)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200201{
Stephen Warren3712a3c2011-10-21 12:25:53 -0600202 const char *func;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200203
Stephen Warren3712a3c2011-10-21 12:25:53 -0600204 func = pin_free(pctldev, pin, range);
205 kfree(func);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200206}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200207
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100208/**
209 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
210 * @pctldev: the pin controller handling this pin
211 * @range: applicable GPIO range
212 * @pin: the affected GPIO pin in this controller
213 * @input: true if we set the pin as input, false for output
214 */
215int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
216 struct pinctrl_gpio_range *range,
217 unsigned pin, bool input)
Linus Walleij542e7042011-11-14 10:06:22 +0100218{
Linus Walleij542e7042011-11-14 10:06:22 +0100219 const struct pinmux_ops *ops;
220 int ret;
Linus Walleij542e7042011-11-14 10:06:22 +0100221
222 ops = pctldev->desc->pmxops;
223
Linus Walleij542e7042011-11-14 10:06:22 +0100224 if (ops->gpio_set_direction)
225 ret = ops->gpio_set_direction(pctldev, range, pin, input);
226 else
227 ret = 0;
228
229 return ret;
230}
231
232/**
Tony Lindgrende849ee2012-01-20 08:17:33 -0800233 * acquire_pins() - acquire all the pins for a certain function on a pinmux
Linus Walleij2744e8a2011-05-02 20:50:54 +0200234 * @pctldev: the device to take the pins on
235 * @func_selector: the function selector to acquire the pins for
236 * @group_selector: the group selector containing the pins to acquire
237 */
238static int acquire_pins(struct pinctrl_dev *pctldev,
239 unsigned func_selector,
240 unsigned group_selector)
241{
242 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
243 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
244 const char *func = pmxops->get_function_name(pctldev,
245 func_selector);
Stephen Warrena5818a82011-10-19 16:19:25 -0600246 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200247 unsigned num_pins;
248 int ret;
249 int i;
250
251 ret = pctlops->get_group_pins(pctldev, group_selector,
252 &pins, &num_pins);
253 if (ret)
254 return ret;
255
Stephen Warren51cd24e2011-12-09 16:59:05 -0700256 dev_dbg(pctldev->dev, "requesting the %u pins from group %u\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200257 num_pins, group_selector);
258
259 /* Try to allocate all pins in this group, one by one */
260 for (i = 0; i < num_pins; i++) {
Stephen Warren3712a3c2011-10-21 12:25:53 -0600261 ret = pin_request(pctldev, pins[i], func, NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200262 if (ret) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700263 dev_err(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100264 "could not get pin %d for function %s on device %s - conflicting mux mappings?\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200265 pins[i], func ? : "(undefined)",
266 pinctrl_dev_get_name(pctldev));
267 /* On error release all taken pins */
268 i--; /* this pin just failed */
269 for (; i >= 0; i--)
Stephen Warren3712a3c2011-10-21 12:25:53 -0600270 pin_free(pctldev, pins[i], NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200271 return -ENODEV;
272 }
273 }
274 return 0;
275}
276
277/**
278 * release_pins() - release pins taken by earlier acquirement
Tony Lindgrende849ee2012-01-20 08:17:33 -0800279 * @pctldev: the device to free the pins on
Linus Walleij2744e8a2011-05-02 20:50:54 +0200280 * @group_selector: the group selector containing the pins to free
281 */
282static void release_pins(struct pinctrl_dev *pctldev,
283 unsigned group_selector)
284{
285 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
Stephen Warrena5818a82011-10-19 16:19:25 -0600286 const unsigned *pins;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200287 unsigned num_pins;
288 int ret;
289 int i;
290
291 ret = pctlops->get_group_pins(pctldev, group_selector,
292 &pins, &num_pins);
293 if (ret) {
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100294 dev_err(pctldev->dev, "could not get pins to release for group selector %d\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200295 group_selector);
296 return;
297 }
298 for (i = 0; i < num_pins; i++)
Stephen Warren3712a3c2011-10-21 12:25:53 -0600299 pin_free(pctldev, pins[i], NULL);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200300}
301
302/**
Linus Walleij2744e8a2011-05-02 20:50:54 +0200303 * pinmux_check_pin_group() - check function and pin group combo
304 * @pctldev: device to check the pin group vs function for
305 * @func_selector: the function selector to check the pin group for, we have
306 * already looked this up in the calling function
307 * @pin_group: the pin group to match to the function
308 *
309 * This function will check that the pinmux driver can supply the
310 * selected pin group for a certain function, returns the group selector if
311 * the group and function selector will work fine together, else returns
312 * negative
313 */
314static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
315 unsigned func_selector,
316 const char *pin_group)
317{
318 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
319 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
320 int ret;
321
322 /*
323 * If the driver does not support different pin groups for the
324 * functions, we only support group 0, and assume this exists.
325 */
326 if (!pctlops || !pctlops->list_groups)
327 return 0;
328
329 /*
330 * Passing NULL (no specific group) will select the first and
331 * hopefully only group of pins available for this function.
332 */
333 if (!pin_group) {
334 char const * const *groups;
335 unsigned num_groups;
336
337 ret = pmxops->get_function_groups(pctldev, func_selector,
338 &groups, &num_groups);
339 if (ret)
340 return ret;
341 if (num_groups < 1)
342 return -EINVAL;
Linus Walleij7afde8b2011-10-19 17:07:16 +0200343 ret = pinctrl_get_group_selector(pctldev, groups[0]);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200344 if (ret < 0) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700345 dev_err(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100346 "function %s wants group %s but the pin controller does not seem to have that group\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200347 pmxops->get_function_name(pctldev, func_selector),
348 groups[0]);
349 return ret;
350 }
351
352 if (num_groups > 1)
Stephen Warren51cd24e2011-12-09 16:59:05 -0700353 dev_dbg(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100354 "function %s support more than one group, default-selecting first group %s (%d)\n",
Linus Walleij2744e8a2011-05-02 20:50:54 +0200355 pmxops->get_function_name(pctldev, func_selector),
356 groups[0],
357 ret);
358
359 return ret;
360 }
361
Stephen Warren51cd24e2011-12-09 16:59:05 -0700362 dev_dbg(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200363 "check if we have pin group %s on controller %s\n",
364 pin_group, pinctrl_dev_get_name(pctldev));
365
Linus Walleij7afde8b2011-10-19 17:07:16 +0200366 ret = pinctrl_get_group_selector(pctldev, pin_group);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200367 if (ret < 0) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700368 dev_dbg(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200369 "%s does not support pin group %s with function %s\n",
370 pinctrl_dev_get_name(pctldev),
371 pin_group,
372 pmxops->get_function_name(pctldev, func_selector));
373 }
374 return ret;
375}
376
377/**
378 * pinmux_search_function() - check pin control driver for a certain function
379 * @pctldev: device to check for function and position
380 * @map: function map containing the function and position to look for
381 * @func_selector: returns the applicable function selector if found
382 * @group_selector: returns the applicable group selector if found
383 *
384 * This will search the pinmux driver for an applicable
385 * function with a specific pin group, returns 0 if these can be mapped
386 * negative otherwise
387 */
388static int pinmux_search_function(struct pinctrl_dev *pctldev,
Linus Walleije93bcee2012-02-09 07:23:28 +0100389 struct pinctrl_map const *map,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200390 unsigned *func_selector,
391 unsigned *group_selector)
392{
393 const struct pinmux_ops *ops = pctldev->desc->pmxops;
394 unsigned selector = 0;
395
396 /* See if this pctldev has this function */
397 while (ops->list_functions(pctldev, selector) >= 0) {
398 const char *fname = ops->get_function_name(pctldev,
399 selector);
400 int ret;
401
402 if (!strcmp(map->function, fname)) {
403 /* Found the function, check pin group */
404 ret = pinmux_check_pin_group(pctldev, selector,
405 map->group);
406 if (ret < 0)
407 return ret;
408
409 /* This function and group selector can be used */
410 *func_selector = selector;
411 *group_selector = ret;
412 return 0;
413
414 }
415 selector++;
416 }
417
418 pr_err("%s does not support function %s\n",
419 pinctrl_dev_get_name(pctldev), map->function);
420 return -EINVAL;
421}
422
423/**
424 * pinmux_enable_muxmap() - enable a map entry for a certain pinmux
425 */
426static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
Linus Walleije93bcee2012-02-09 07:23:28 +0100427 struct pinctrl *p,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200428 struct device *dev,
429 const char *devname,
Linus Walleije93bcee2012-02-09 07:23:28 +0100430 struct pinctrl_map const *map)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200431{
432 unsigned func_selector;
433 unsigned group_selector;
434 struct pinmux_group *grp;
435 int ret;
436
437 /*
438 * Note that we're not locking the pinmux mutex here, because
439 * this is only called at pinmux initialization time when it
440 * has not been added to any list and thus is not reachable
441 * by anyone else.
442 */
443
Linus Walleije93bcee2012-02-09 07:23:28 +0100444 if (p->pctldev && p->pctldev != pctldev) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700445 dev_err(pctldev->dev,
Uwe Kleine-Königf9d41d72012-01-19 22:42:48 +0100446 "different pin control devices given for device %s, function %s\n",
447 devname, map->function);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200448 return -EINVAL;
449 }
Linus Walleije93bcee2012-02-09 07:23:28 +0100450 p->dev = dev;
451 p->pctldev = pctldev;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200452
453 /* Now go into the driver and try to match a function and group */
454 ret = pinmux_search_function(pctldev, map, &func_selector,
455 &group_selector);
456 if (ret < 0)
457 return ret;
458
459 /*
460 * If the function selector is already set, it needs to be identical,
461 * we support several groups with one function but not several
462 * functions with one or several groups in the same pinmux.
463 */
Linus Walleije93bcee2012-02-09 07:23:28 +0100464 if (p->func_selector != UINT_MAX &&
465 p->func_selector != func_selector) {
Stephen Warren51cd24e2011-12-09 16:59:05 -0700466 dev_err(pctldev->dev,
Linus Walleij2744e8a2011-05-02 20:50:54 +0200467 "dual function defines in the map for device %s\n",
468 devname);
469 return -EINVAL;
470 }
Linus Walleije93bcee2012-02-09 07:23:28 +0100471 p->func_selector = func_selector;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200472
473 /* Now add this group selector, we may have many of them */
474 grp = kmalloc(sizeof(struct pinmux_group), GFP_KERNEL);
475 if (!grp)
476 return -ENOMEM;
477 grp->group_selector = group_selector;
478 ret = acquire_pins(pctldev, func_selector, group_selector);
479 if (ret) {
480 kfree(grp);
481 return ret;
482 }
Stephen Warren8b9c1392012-02-19 23:45:42 -0700483 list_add_tail(&grp->node, &p->groups);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200484
485 return 0;
486}
487
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100488/**
489 * pinmux_apply_muxmap() - apply a certain mux mapping entry
490 */
491int pinmux_apply_muxmap(struct pinctrl_dev *pctldev,
492 struct pinctrl *p,
493 struct device *dev,
494 const char *devname,
495 struct pinctrl_map const *map)
496{
497 int ret;
498
499 ret = pinmux_enable_muxmap(pctldev, p, dev,
500 devname, map);
501 if (ret) {
502 pinmux_put(p);
503 return ret;
504 }
505
506 return 0;
507}
508
509/**
510 * pinmux_put() - free up the pinmux portions of a pin controller handle
511 */
512void pinmux_put(struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200513{
514 struct list_head *node, *tmp;
515
Linus Walleije93bcee2012-02-09 07:23:28 +0100516 list_for_each_safe(node, tmp, &p->groups) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200517 struct pinmux_group *grp =
518 list_entry(node, struct pinmux_group, node);
519 /* Release all pins taken by this group */
Linus Walleije93bcee2012-02-09 07:23:28 +0100520 release_pins(p->pctldev, grp->group_selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200521 list_del(node);
522 kfree(grp);
523 }
524}
525
526/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100527 * pinmux_enable() - enable the pinmux portion of a pin control handle
Linus Walleij2744e8a2011-05-02 20:50:54 +0200528 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100529int pinmux_enable(struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200530{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100531 struct pinctrl_dev *pctldev = p->pctldev;
532 const struct pinmux_ops *ops = pctldev->desc->pmxops;
533 struct pinmux_group *grp;
534 int ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200535
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100536 list_for_each_entry(grp, &p->groups, node) {
537 ret = ops->enable(pctldev, p->func_selector,
538 grp->group_selector);
539 if (ret)
540 /*
541 * TODO: call disable() on all groups we called
542 * enable() on to this point?
543 */
544 return ret;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200545 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100546 return 0;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200547}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200548
549/**
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100550 * pinmux_disable() - disable the pinmux portions of a pin control handle
Linus Walleij2744e8a2011-05-02 20:50:54 +0200551 */
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100552void pinmux_disable(struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200553{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100554 struct pinctrl_dev *pctldev = p->pctldev;
555 const struct pinmux_ops *ops = pctldev->desc->pmxops;
556 struct pinmux_group *grp;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200557
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100558 list_for_each_entry(grp, &p->groups, node) {
559 ops->disable(pctldev, p->func_selector,
560 grp->group_selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200561 }
Linus Walleij2744e8a2011-05-02 20:50:54 +0200562}
Linus Walleij2744e8a2011-05-02 20:50:54 +0200563
Tony Lindgrenb9130b72012-01-24 16:28:08 -0800564int pinmux_check_ops(struct pinctrl_dev *pctldev)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200565{
Tony Lindgrenb9130b72012-01-24 16:28:08 -0800566 const struct pinmux_ops *ops = pctldev->desc->pmxops;
567 unsigned selector = 0;
568
Linus Walleij2744e8a2011-05-02 20:50:54 +0200569 /* Check that we implement required operations */
570 if (!ops->list_functions ||
571 !ops->get_function_name ||
572 !ops->get_function_groups ||
573 !ops->enable ||
574 !ops->disable)
575 return -EINVAL;
576
Tony Lindgrenb9130b72012-01-24 16:28:08 -0800577 /* Check that all functions registered have names */
578 while (ops->list_functions(pctldev, selector) >= 0) {
579 const char *fname = ops->get_function_name(pctldev,
580 selector);
581 if (!fname) {
582 pr_err("pinmux ops has no name for function%u\n",
583 selector);
584 return -EINVAL;
585 }
586 selector++;
587 }
588
Linus Walleij2744e8a2011-05-02 20:50:54 +0200589 return 0;
590}
591
Linus Walleij2744e8a2011-05-02 20:50:54 +0200592#ifdef CONFIG_DEBUG_FS
593
594/* Called from pincontrol core */
595static 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
627static int pinmux_pins_show(struct seq_file *s, void *what)
628{
629 struct pinctrl_dev *pctldev = s->private;
Chanho Park706e8522012-01-03 16:47:50 +0900630 unsigned i, pin;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200631
632 seq_puts(s, "Pinmux settings per pin\n");
633 seq_puts(s, "Format: pin (name): pinmuxfunction\n");
634
Chanho Park706e8522012-01-03 16:47:50 +0900635 /* The pin number can be retrived from the pin controller descriptor */
636 for (i = 0; i < pctldev->desc->npins; i++) {
Linus Walleij2744e8a2011-05-02 20:50:54 +0200637
638 struct pin_desc *desc;
639
Chanho Park706e8522012-01-03 16:47:50 +0900640 pin = pctldev->desc->pins[i].number;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200641 desc = pin_desc_get(pctldev, pin);
Chanho Park706e8522012-01-03 16:47:50 +0900642 /* Skip if we cannot search the pin */
Linus Walleij2744e8a2011-05-02 20:50:54 +0200643 if (desc == NULL)
644 continue;
645
646 seq_printf(s, "pin %d (%s): %s\n", pin,
647 desc->name ? desc->name : "unnamed",
Stephen Warren5d2eaf82011-10-19 16:19:28 -0600648 desc->mux_function ? desc->mux_function
649 : "UNCLAIMED");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200650 }
651
652 return 0;
653}
654
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100655void pinmux_dbg_show(struct seq_file *s, struct pinctrl *p)
Linus Walleij2744e8a2011-05-02 20:50:54 +0200656{
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100657 struct pinctrl_dev *pctldev = p->pctldev;
658 const struct pinmux_ops *pmxops;
659 const struct pinctrl_ops *pctlops;
660 struct pinmux_group *grp;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200661
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100662 pmxops = pctldev->desc->pmxops;
663 pctlops = pctldev->desc->pctlops;
Linus Walleij2744e8a2011-05-02 20:50:54 +0200664
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100665 seq_printf(s, " function: %s (%u),",
666 pmxops->get_function_name(pctldev,
667 p->func_selector),
668 p->func_selector);
Linus Walleij2744e8a2011-05-02 20:50:54 +0200669
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100670 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 Walleij2744e8a2011-05-02 20:50:54 +0200676 }
Linus Walleijbefe5bd2012-02-09 19:47:48 +0100677 seq_printf(s, " ]");
Linus Walleij2744e8a2011-05-02 20:50:54 +0200678}
679
680static int pinmux_functions_open(struct inode *inode, struct file *file)
681{
682 return single_open(file, pinmux_functions_show, inode->i_private);
683}
684
685static 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 Walleij2744e8a2011-05-02 20:50:54 +0200690static 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
697static 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 Walleij2744e8a2011-05-02 20:50:54 +0200704void 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 Walleij2744e8a2011-05-02 20:50:54 +0200711}
712
713#endif /* CONFIG_DEBUG_FS */