ARM: fix ioremap/iounmap for !CONFIG_MMU
[linux-2.6/libata-dev.git] / drivers / pinctrl / pinmux.c
blob7c3193f7a04430bcc8076dfd18aafc05983e915b
1 /*
2 * Core driver for the pin muxing portions of the pin control subsystem
4 * Copyright (C) 2011 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
8 * Author: Linus Walleij <linus.walleij@linaro.org>
10 * License terms: GNU General Public License (GPL) version 2
12 #define pr_fmt(fmt) "pinmux core: " fmt
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>
24 #include <linux/string.h>
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"
32 /* List of pinmuxes */
33 static DEFINE_MUTEX(pinmux_list_mutex);
34 static LIST_HEAD(pinmux_list);
36 /* Global pinmux maps */
37 static struct pinmux_map *pinmux_maps;
38 static unsigned pinmux_maps_num;
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
45 struct pinmux_group {
46 struct list_head node;
47 unsigned group_selector;
50 /**
51 * struct pinmux - per-device pinmux state holder
52 * @node: global list node
53 * @dev: the device using this pinmux
54 * @usecount: the number of active users of this mux setting, used to keep
55 * track of nested use cases
56 * @pctldev: pin control device handling this pinmux
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
65 struct pinmux {
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;
75 /**
76 * struct pinmux_hog - a list item to stash mux hogs
77 * @node: pinmux hog list node
78 * @map: map entry responsible for this hogging
79 * @pmx: the pinmux hogged by this item
81 struct pinmux_hog {
82 struct list_head node;
83 struct pinmux_map const *map;
84 struct pinmux *pmx;
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
93 * @gpio_range: the range matching the GPIO pin if this is a request for a
94 * single GPIO pin
96 static int pin_request(struct pinctrl_dev *pctldev,
97 int pin, const char *function,
98 struct pinctrl_gpio_range *gpio_range)
100 struct pin_desc *desc;
101 const struct pinmux_ops *ops = pctldev->desc->pmxops;
102 int status = -EINVAL;
104 dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, function);
106 desc = pin_desc_get(pctldev, pin);
107 if (desc == NULL) {
108 dev_err(pctldev->dev,
109 "pin is not registered so it cannot be requested\n");
110 goto out;
113 if (!function) {
114 dev_err(pctldev->dev, "no function name given\n");
115 return -EINVAL;
118 spin_lock(&desc->lock);
119 if (desc->mux_function) {
120 spin_unlock(&desc->lock);
121 dev_err(pctldev->dev,
122 "pin already requested\n");
123 goto out;
125 desc->mux_function = function;
126 spin_unlock(&desc->lock);
128 /* Let each pin increase references to this module */
129 if (!try_module_get(pctldev->owner)) {
130 dev_err(pctldev->dev,
131 "could not increase module refcount for pin %d\n",
132 pin);
133 status = -EINVAL;
134 goto out_free_pin;
138 * If there is no kind of request function for the pin we just assume
139 * we got it by default and proceed.
141 if (gpio_range && ops->gpio_request_enable)
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;
149 if (status)
150 dev_err(pctldev->dev, "->request on device %s failed for pin %d\n",
151 pctldev->desc->name, pin);
152 out_free_pin:
153 if (status) {
154 spin_lock(&desc->lock);
155 desc->mux_function = NULL;
156 spin_unlock(&desc->lock);
158 out:
159 if (status)
160 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
161 pin, function ? : "?", status);
163 return status;
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
170 * @gpio_range: the range matching the GPIO pin if this is a request for a
171 * single GPIO pin
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.
177 static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
178 struct pinctrl_gpio_range *gpio_range)
180 const struct pinmux_ops *ops = pctldev->desc->pmxops;
181 struct pin_desc *desc;
182 const char *func;
184 desc = pin_desc_get(pctldev, pin);
185 if (desc == NULL) {
186 dev_err(pctldev->dev,
187 "pin is not registered so it cannot be freed\n");
188 return NULL;
192 * If there is no kind of request function for the pin we just assume
193 * we got it by default and proceed.
195 if (gpio_range && ops->gpio_disable_free)
196 ops->gpio_disable_free(pctldev, gpio_range, pin);
197 else if (ops->free)
198 ops->free(pctldev, pin);
200 spin_lock(&desc->lock);
201 func = desc->mux_function;
202 desc->mux_function = NULL;
203 spin_unlock(&desc->lock);
204 module_put(pctldev->owner);
206 return func;
210 * pinmux_request_gpio() - request a single pin to be muxed in as GPIO
211 * @gpio: the GPIO pin number from the GPIO subsystem number space
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.
217 int pinmux_request_gpio(unsigned gpio)
219 char gpiostr[16];
220 const char *function;
221 struct pinctrl_dev *pctldev;
222 struct pinctrl_gpio_range *range;
223 int ret;
224 int pin;
226 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
227 if (ret)
228 return -EINVAL;
230 /* Convert to the pin controllers number space */
231 pin = gpio - range->base + range->pin_base;
233 /* Conjure some name stating what chip and pin this is taken by */
234 snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
236 function = kstrdup(gpiostr, GFP_KERNEL);
237 if (!function)
238 return -EINVAL;
240 ret = pin_request(pctldev, pin, function, range);
241 if (ret < 0)
242 kfree(function);
244 return ret;
246 EXPORT_SYMBOL_GPL(pinmux_request_gpio);
249 * pinmux_free_gpio() - free a single pin, currently used as GPIO
250 * @gpio: the GPIO pin number from the GPIO subsystem number space
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.
256 void pinmux_free_gpio(unsigned gpio)
258 struct pinctrl_dev *pctldev;
259 struct pinctrl_gpio_range *range;
260 int ret;
261 int pin;
262 const char *func;
264 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
265 if (ret)
266 return;
268 /* Convert to the pin controllers number space */
269 pin = gpio - range->base + range->pin_base;
271 func = pin_free(pctldev, pin, range);
272 kfree(func);
274 EXPORT_SYMBOL_GPL(pinmux_free_gpio);
276 static int pinmux_gpio_direction(unsigned gpio, bool input)
278 struct pinctrl_dev *pctldev;
279 struct pinctrl_gpio_range *range;
280 const struct pinmux_ops *ops;
281 int ret;
282 int pin;
284 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
285 if (ret)
286 return ret;
288 ops = pctldev->desc->pmxops;
290 /* Convert to the pin controllers number space */
291 pin = gpio - range->base + range->pin_base;
293 if (ops->gpio_set_direction)
294 ret = ops->gpio_set_direction(pctldev, range, pin, input);
295 else
296 ret = 0;
298 return ret;
302 * pinmux_gpio_direction_input() - request a GPIO pin to go into input mode
303 * @gpio: the GPIO pin number from the GPIO subsystem number space
305 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
306 * as part of their gpio_direction_input() semantics, platforms and individual
307 * drivers shall *NOT* touch pinmux GPIO calls.
309 int pinmux_gpio_direction_input(unsigned gpio)
311 return pinmux_gpio_direction(gpio, true);
313 EXPORT_SYMBOL_GPL(pinmux_gpio_direction_input);
316 * pinmux_gpio_direction_output() - request a GPIO pin to go into output mode
317 * @gpio: the GPIO pin number from the GPIO subsystem number space
319 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
320 * as part of their gpio_direction_output() semantics, platforms and individual
321 * drivers shall *NOT* touch pinmux GPIO calls.
323 int pinmux_gpio_direction_output(unsigned gpio)
325 return pinmux_gpio_direction(gpio, false);
327 EXPORT_SYMBOL_GPL(pinmux_gpio_direction_output);
330 * pinmux_register_mappings() - register a set of pinmux mappings
331 * @maps: the pinmux mappings table to register, this should be marked with
332 * __initdata so it can be discarded after boot, this function will
333 * perform a shallow copy for the mapping entries.
334 * @num_maps: the number of maps in the mapping table
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
339 * freed.
341 int __init pinmux_register_mappings(struct pinmux_map const *maps,
342 unsigned num_maps)
344 void *tmp_maps;
345 int i;
347 pr_debug("add %d pinmux maps\n", num_maps);
349 /* First sanity check the new mapping */
350 for (i = 0; i < num_maps; i++) {
351 if (!maps[i].name) {
352 pr_err("failed to register map %d: no map name given\n",
354 return -EINVAL;
357 if (!maps[i].ctrl_dev && !maps[i].ctrl_dev_name) {
358 pr_err("failed to register map %s (%d): no pin control device given\n",
359 maps[i].name, i);
360 return -EINVAL;
363 if (!maps[i].function) {
364 pr_err("failed to register map %s (%d): no function ID given\n",
365 maps[i].name, i);
366 return -EINVAL;
369 if (!maps[i].dev && !maps[i].dev_name)
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);
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.
383 if (!pinmux_maps_num) {
384 /* On first call, just copy them */
385 tmp_maps = kmemdup(maps,
386 sizeof(struct pinmux_map) * num_maps,
387 GFP_KERNEL);
388 if (!tmp_maps)
389 return -ENOMEM;
390 } else {
391 /* Subsequent calls, reallocate array to new size */
392 size_t oldsize = sizeof(struct pinmux_map) * pinmux_maps_num;
393 size_t newsize = sizeof(struct pinmux_map) * num_maps;
395 tmp_maps = krealloc(pinmux_maps, oldsize + newsize, GFP_KERNEL);
396 if (!tmp_maps)
397 return -ENOMEM;
398 memcpy((tmp_maps + oldsize), maps, newsize);
401 pinmux_maps = tmp_maps;
402 pinmux_maps_num += num_maps;
403 return 0;
407 * acquire_pins() - acquire all the pins for a certain function on a pinmux
408 * @pctldev: the device to take the pins on
409 * @func_selector: the function selector to acquire the pins for
410 * @group_selector: the group selector containing the pins to acquire
412 static int acquire_pins(struct pinctrl_dev *pctldev,
413 unsigned func_selector,
414 unsigned group_selector)
416 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
417 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
418 const char *func = pmxops->get_function_name(pctldev,
419 func_selector);
420 const unsigned *pins;
421 unsigned num_pins;
422 int ret;
423 int i;
425 ret = pctlops->get_group_pins(pctldev, group_selector,
426 &pins, &num_pins);
427 if (ret)
428 return ret;
430 dev_dbg(pctldev->dev, "requesting the %u pins from group %u\n",
431 num_pins, group_selector);
433 /* Try to allocate all pins in this group, one by one */
434 for (i = 0; i < num_pins; i++) {
435 ret = pin_request(pctldev, pins[i], func, NULL);
436 if (ret) {
437 dev_err(pctldev->dev,
438 "could not get pin %d for function %s on device %s - conflicting mux mappings?\n",
439 pins[i], func ? : "(undefined)",
440 pinctrl_dev_get_name(pctldev));
441 /* On error release all taken pins */
442 i--; /* this pin just failed */
443 for (; i >= 0; i--)
444 pin_free(pctldev, pins[i], NULL);
445 return -ENODEV;
448 return 0;
452 * release_pins() - release pins taken by earlier acquirement
453 * @pctldev: the device to free the pins on
454 * @group_selector: the group selector containing the pins to free
456 static void release_pins(struct pinctrl_dev *pctldev,
457 unsigned group_selector)
459 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
460 const unsigned *pins;
461 unsigned num_pins;
462 int ret;
463 int i;
465 ret = pctlops->get_group_pins(pctldev, group_selector,
466 &pins, &num_pins);
467 if (ret) {
468 dev_err(pctldev->dev, "could not get pins to release for group selector %d\n",
469 group_selector);
470 return;
472 for (i = 0; i < num_pins; i++)
473 pin_free(pctldev, pins[i], NULL);
477 * pinmux_check_pin_group() - check function and pin group combo
478 * @pctldev: device to check the pin group vs function for
479 * @func_selector: the function selector to check the pin group for, we have
480 * already looked this up in the calling function
481 * @pin_group: the pin group to match to the function
483 * This function will check that the pinmux driver can supply the
484 * selected pin group for a certain function, returns the group selector if
485 * the group and function selector will work fine together, else returns
486 * negative
488 static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
489 unsigned func_selector,
490 const char *pin_group)
492 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
493 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
494 int ret;
497 * If the driver does not support different pin groups for the
498 * functions, we only support group 0, and assume this exists.
500 if (!pctlops || !pctlops->list_groups)
501 return 0;
504 * Passing NULL (no specific group) will select the first and
505 * hopefully only group of pins available for this function.
507 if (!pin_group) {
508 char const * const *groups;
509 unsigned num_groups;
511 ret = pmxops->get_function_groups(pctldev, func_selector,
512 &groups, &num_groups);
513 if (ret)
514 return ret;
515 if (num_groups < 1)
516 return -EINVAL;
517 ret = pinctrl_get_group_selector(pctldev, groups[0]);
518 if (ret < 0) {
519 dev_err(pctldev->dev,
520 "function %s wants group %s but the pin controller does not seem to have that group\n",
521 pmxops->get_function_name(pctldev, func_selector),
522 groups[0]);
523 return ret;
526 if (num_groups > 1)
527 dev_dbg(pctldev->dev,
528 "function %s support more than one group, default-selecting first group %s (%d)\n",
529 pmxops->get_function_name(pctldev, func_selector),
530 groups[0],
531 ret);
533 return ret;
536 dev_dbg(pctldev->dev,
537 "check if we have pin group %s on controller %s\n",
538 pin_group, pinctrl_dev_get_name(pctldev));
540 ret = pinctrl_get_group_selector(pctldev, pin_group);
541 if (ret < 0) {
542 dev_dbg(pctldev->dev,
543 "%s does not support pin group %s with function %s\n",
544 pinctrl_dev_get_name(pctldev),
545 pin_group,
546 pmxops->get_function_name(pctldev, func_selector));
548 return ret;
552 * pinmux_search_function() - check pin control driver for a certain function
553 * @pctldev: device to check for function and position
554 * @map: function map containing the function and position to look for
555 * @func_selector: returns the applicable function selector if found
556 * @group_selector: returns the applicable group selector if found
558 * This will search the pinmux driver for an applicable
559 * function with a specific pin group, returns 0 if these can be mapped
560 * negative otherwise
562 static int pinmux_search_function(struct pinctrl_dev *pctldev,
563 struct pinmux_map const *map,
564 unsigned *func_selector,
565 unsigned *group_selector)
567 const struct pinmux_ops *ops = pctldev->desc->pmxops;
568 unsigned selector = 0;
570 /* See if this pctldev has this function */
571 while (ops->list_functions(pctldev, selector) >= 0) {
572 const char *fname = ops->get_function_name(pctldev,
573 selector);
574 int ret;
576 if (!strcmp(map->function, fname)) {
577 /* Found the function, check pin group */
578 ret = pinmux_check_pin_group(pctldev, selector,
579 map->group);
580 if (ret < 0)
581 return ret;
583 /* This function and group selector can be used */
584 *func_selector = selector;
585 *group_selector = ret;
586 return 0;
589 selector++;
592 pr_err("%s does not support function %s\n",
593 pinctrl_dev_get_name(pctldev), map->function);
594 return -EINVAL;
598 * pinmux_enable_muxmap() - enable a map entry for a certain pinmux
600 static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
601 struct pinmux *pmx,
602 struct device *dev,
603 const char *devname,
604 struct pinmux_map const *map)
606 unsigned func_selector;
607 unsigned group_selector;
608 struct pinmux_group *grp;
609 int ret;
612 * Note that we're not locking the pinmux mutex here, because
613 * this is only called at pinmux initialization time when it
614 * has not been added to any list and thus is not reachable
615 * by anyone else.
618 if (pmx->pctldev && pmx->pctldev != pctldev) {
619 dev_err(pctldev->dev,
620 "different pin control devices given for device %s, function %s\n",
621 devname, map->function);
622 return -EINVAL;
624 pmx->dev = dev;
625 pmx->pctldev = pctldev;
627 /* Now go into the driver and try to match a function and group */
628 ret = pinmux_search_function(pctldev, map, &func_selector,
629 &group_selector);
630 if (ret < 0)
631 return ret;
634 * If the function selector is already set, it needs to be identical,
635 * we support several groups with one function but not several
636 * functions with one or several groups in the same pinmux.
638 if (pmx->func_selector != UINT_MAX &&
639 pmx->func_selector != func_selector) {
640 dev_err(pctldev->dev,
641 "dual function defines in the map for device %s\n",
642 devname);
643 return -EINVAL;
645 pmx->func_selector = func_selector;
647 /* Now add this group selector, we may have many of them */
648 grp = kmalloc(sizeof(struct pinmux_group), GFP_KERNEL);
649 if (!grp)
650 return -ENOMEM;
651 grp->group_selector = group_selector;
652 ret = acquire_pins(pctldev, func_selector, group_selector);
653 if (ret) {
654 kfree(grp);
655 return ret;
657 list_add(&grp->node, &pmx->groups);
659 return 0;
662 static void pinmux_free_groups(struct pinmux *pmx)
664 struct list_head *node, *tmp;
666 list_for_each_safe(node, tmp, &pmx->groups) {
667 struct pinmux_group *grp =
668 list_entry(node, struct pinmux_group, node);
669 /* Release all pins taken by this group */
670 release_pins(pmx->pctldev, grp->group_selector);
671 list_del(node);
672 kfree(grp);
677 * pinmux_get() - retrieves the pinmux for a certain device
678 * @dev: the device to get the pinmux for
679 * @name: an optional specific mux mapping name or NULL, the name is only
680 * needed if you want to have more than one mapping per device, or if you
681 * need an anonymous pinmux (not tied to any specific device)
683 struct pinmux *pinmux_get(struct device *dev, const char *name)
685 struct pinmux_map const *map = NULL;
686 struct pinctrl_dev *pctldev = NULL;
687 const char *devname = NULL;
688 struct pinmux *pmx;
689 bool found_map;
690 unsigned num_maps = 0;
691 int ret = -ENODEV;
692 int i;
694 /* We must have dev or ID or both */
695 if (!dev && !name)
696 return ERR_PTR(-EINVAL);
698 if (dev)
699 devname = dev_name(dev);
701 pr_debug("get mux %s for device %s\n", name,
702 devname ? devname : "(none)");
705 * create the state cookie holder struct pinmux for each
706 * mapping, this is what consumers will get when requesting
707 * a pinmux handle with pinmux_get()
709 pmx = kzalloc(sizeof(struct pinmux), GFP_KERNEL);
710 if (pmx == NULL)
711 return ERR_PTR(-ENOMEM);
712 mutex_init(&pmx->mutex);
713 pmx->func_selector = UINT_MAX;
714 INIT_LIST_HEAD(&pmx->groups);
716 /* Iterate over the pinmux maps to locate the right ones */
717 for (i = 0; i < pinmux_maps_num; i++) {
718 map = &pinmux_maps[i];
719 found_map = false;
722 * First, try to find the pctldev given in the map
724 pctldev = get_pinctrl_dev_from_dev(map->ctrl_dev,
725 map->ctrl_dev_name);
726 if (!pctldev) {
727 const char *devname = NULL;
729 if (map->ctrl_dev)
730 devname = dev_name(map->ctrl_dev);
731 else if (map->ctrl_dev_name)
732 devname = map->ctrl_dev_name;
734 pr_warning("could not find a pinctrl device for pinmux function %s, fishy, they shall all have one\n",
735 map->function);
736 pr_warning("given pinctrl device name: %s",
737 devname ? devname : "UNDEFINED");
739 /* Continue to check the other mappings anyway... */
740 continue;
743 pr_debug("in map, found pctldev %s to handle function %s",
744 dev_name(pctldev->dev), map->function);
748 * If we're looking for a specific named map, this must match,
749 * else we loop and look for the next.
751 if (name != NULL) {
752 if (map->name == NULL)
753 continue;
754 if (strcmp(map->name, name))
755 continue;
759 * This is for the case where no device name is given, we
760 * already know that the function name matches from above
761 * code.
763 if (!map->dev_name && (name != NULL))
764 found_map = true;
766 /* If the mapping has a device set up it must match */
767 if (map->dev_name &&
768 (!devname || !strcmp(map->dev_name, devname)))
769 /* MATCH! */
770 found_map = true;
772 /* If this map is applicable, then apply it */
773 if (found_map) {
774 ret = pinmux_enable_muxmap(pctldev, pmx, dev,
775 devname, map);
776 if (ret) {
777 pinmux_free_groups(pmx);
778 kfree(pmx);
779 return ERR_PTR(ret);
781 num_maps++;
786 /* We should have atleast one map, right */
787 if (!num_maps) {
788 pr_err("could not find any mux maps for device %s, ID %s\n",
789 devname ? devname : "(anonymous)",
790 name ? name : "(undefined)");
791 kfree(pmx);
792 return ERR_PTR(-EINVAL);
795 pr_debug("found %u mux maps for device %s, UD %s\n",
796 num_maps,
797 devname ? devname : "(anonymous)",
798 name ? name : "(undefined)");
800 /* Add the pinmux to the global list */
801 mutex_lock(&pinmux_list_mutex);
802 list_add(&pmx->node, &pinmux_list);
803 mutex_unlock(&pinmux_list_mutex);
805 return pmx;
807 EXPORT_SYMBOL_GPL(pinmux_get);
810 * pinmux_put() - release a previously claimed pinmux
811 * @pmx: a pinmux previously claimed by pinmux_get()
813 void pinmux_put(struct pinmux *pmx)
815 if (pmx == NULL)
816 return;
818 mutex_lock(&pmx->mutex);
819 if (pmx->usecount)
820 pr_warn("releasing pinmux with active users!\n");
821 /* Free the groups and all acquired pins */
822 pinmux_free_groups(pmx);
823 mutex_unlock(&pmx->mutex);
825 /* Remove from list */
826 mutex_lock(&pinmux_list_mutex);
827 list_del(&pmx->node);
828 mutex_unlock(&pinmux_list_mutex);
830 kfree(pmx);
832 EXPORT_SYMBOL_GPL(pinmux_put);
835 * pinmux_enable() - enable a certain pinmux setting
836 * @pmx: the pinmux to enable, previously claimed by pinmux_get()
838 int pinmux_enable(struct pinmux *pmx)
840 int ret = 0;
842 if (pmx == NULL)
843 return -EINVAL;
844 mutex_lock(&pmx->mutex);
845 if (pmx->usecount++ == 0) {
846 struct pinctrl_dev *pctldev = pmx->pctldev;
847 const struct pinmux_ops *ops = pctldev->desc->pmxops;
848 struct pinmux_group *grp;
850 list_for_each_entry(grp, &pmx->groups, node) {
851 ret = ops->enable(pctldev, pmx->func_selector,
852 grp->group_selector);
853 if (ret) {
855 * TODO: call disable() on all groups we called
856 * enable() on to this point?
858 pmx->usecount--;
859 break;
863 mutex_unlock(&pmx->mutex);
864 return ret;
866 EXPORT_SYMBOL_GPL(pinmux_enable);
869 * pinmux_disable() - disable a certain pinmux setting
870 * @pmx: the pinmux to disable, previously claimed by pinmux_get()
872 void pinmux_disable(struct pinmux *pmx)
874 if (pmx == NULL)
875 return;
877 mutex_lock(&pmx->mutex);
878 if (--pmx->usecount == 0) {
879 struct pinctrl_dev *pctldev = pmx->pctldev;
880 const struct pinmux_ops *ops = pctldev->desc->pmxops;
881 struct pinmux_group *grp;
883 list_for_each_entry(grp, &pmx->groups, node) {
884 ops->disable(pctldev, pmx->func_selector,
885 grp->group_selector);
888 mutex_unlock(&pmx->mutex);
890 EXPORT_SYMBOL_GPL(pinmux_disable);
892 int pinmux_check_ops(struct pinctrl_dev *pctldev)
894 const struct pinmux_ops *ops = pctldev->desc->pmxops;
895 unsigned selector = 0;
897 /* Check that we implement required operations */
898 if (!ops->list_functions ||
899 !ops->get_function_name ||
900 !ops->get_function_groups ||
901 !ops->enable ||
902 !ops->disable)
903 return -EINVAL;
905 /* Check that all functions registered have names */
906 while (ops->list_functions(pctldev, selector) >= 0) {
907 const char *fname = ops->get_function_name(pctldev,
908 selector);
909 if (!fname) {
910 pr_err("pinmux ops has no name for function%u\n",
911 selector);
912 return -EINVAL;
914 selector++;
917 return 0;
920 /* Hog a single map entry and add to the hoglist */
921 static int pinmux_hog_map(struct pinctrl_dev *pctldev,
922 struct pinmux_map const *map)
924 struct pinmux_hog *hog;
925 struct pinmux *pmx;
926 int ret;
928 if (map->dev || map->dev_name) {
930 * TODO: the day we have device tree support, we can
931 * traverse the device tree and hog to specific device nodes
932 * without any problems, so then we can hog pinmuxes for
933 * all devices that just want a static pin mux at this point.
935 dev_err(pctldev->dev, "map %s wants to hog a non-system pinmux, this is not going to work\n",
936 map->name);
937 return -EINVAL;
940 hog = kzalloc(sizeof(struct pinmux_hog), GFP_KERNEL);
941 if (!hog)
942 return -ENOMEM;
944 pmx = pinmux_get(NULL, map->name);
945 if (IS_ERR(pmx)) {
946 kfree(hog);
947 dev_err(pctldev->dev,
948 "could not get the %s pinmux mapping for hogging\n",
949 map->name);
950 return PTR_ERR(pmx);
953 ret = pinmux_enable(pmx);
954 if (ret) {
955 pinmux_put(pmx);
956 kfree(hog);
957 dev_err(pctldev->dev,
958 "could not enable the %s pinmux mapping for hogging\n",
959 map->name);
960 return ret;
963 hog->map = map;
964 hog->pmx = pmx;
966 dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name,
967 map->function);
968 mutex_lock(&pctldev->pinmux_hogs_lock);
969 list_add(&hog->node, &pctldev->pinmux_hogs);
970 mutex_unlock(&pctldev->pinmux_hogs_lock);
972 return 0;
976 * pinmux_hog_maps() - hog specific map entries on controller device
977 * @pctldev: the pin control device to hog entries on
979 * When the pin controllers are registered, there may be some specific pinmux
980 * map entries that need to be hogged, i.e. get+enabled until the system shuts
981 * down.
983 int pinmux_hog_maps(struct pinctrl_dev *pctldev)
985 struct device *dev = pctldev->dev;
986 const char *devname = dev_name(dev);
987 int ret;
988 int i;
990 INIT_LIST_HEAD(&pctldev->pinmux_hogs);
991 mutex_init(&pctldev->pinmux_hogs_lock);
993 for (i = 0; i < pinmux_maps_num; i++) {
994 struct pinmux_map const *map = &pinmux_maps[i];
996 if (!map->hog_on_boot)
997 continue;
999 if ((map->ctrl_dev == dev) ||
1000 (map->ctrl_dev_name &&
1001 !strcmp(map->ctrl_dev_name, devname))) {
1002 /* OK time to hog! */
1003 ret = pinmux_hog_map(pctldev, map);
1004 if (ret)
1005 return ret;
1008 return 0;
1012 * pinmux_unhog_maps() - unhog specific map entries on controller device
1013 * @pctldev: the pin control device to unhog entries on
1015 void pinmux_unhog_maps(struct pinctrl_dev *pctldev)
1017 struct list_head *node, *tmp;
1019 mutex_lock(&pctldev->pinmux_hogs_lock);
1020 list_for_each_safe(node, tmp, &pctldev->pinmux_hogs) {
1021 struct pinmux_hog *hog =
1022 list_entry(node, struct pinmux_hog, node);
1023 pinmux_disable(hog->pmx);
1024 pinmux_put(hog->pmx);
1025 list_del(node);
1026 kfree(hog);
1028 mutex_unlock(&pctldev->pinmux_hogs_lock);
1031 #ifdef CONFIG_DEBUG_FS
1033 /* Called from pincontrol core */
1034 static int pinmux_functions_show(struct seq_file *s, void *what)
1036 struct pinctrl_dev *pctldev = s->private;
1037 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
1038 unsigned func_selector = 0;
1040 while (pmxops->list_functions(pctldev, func_selector) >= 0) {
1041 const char *func = pmxops->get_function_name(pctldev,
1042 func_selector);
1043 const char * const *groups;
1044 unsigned num_groups;
1045 int ret;
1046 int i;
1048 ret = pmxops->get_function_groups(pctldev, func_selector,
1049 &groups, &num_groups);
1050 if (ret)
1051 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
1052 func);
1054 seq_printf(s, "function: %s, groups = [ ", func);
1055 for (i = 0; i < num_groups; i++)
1056 seq_printf(s, "%s ", groups[i]);
1057 seq_puts(s, "]\n");
1059 func_selector++;
1063 return 0;
1066 static int pinmux_pins_show(struct seq_file *s, void *what)
1068 struct pinctrl_dev *pctldev = s->private;
1069 unsigned i, pin;
1071 seq_puts(s, "Pinmux settings per pin\n");
1072 seq_puts(s, "Format: pin (name): pinmuxfunction\n");
1074 /* The pin number can be retrived from the pin controller descriptor */
1075 for (i = 0; i < pctldev->desc->npins; i++) {
1077 struct pin_desc *desc;
1079 pin = pctldev->desc->pins[i].number;
1080 desc = pin_desc_get(pctldev, pin);
1081 /* Skip if we cannot search the pin */
1082 if (desc == NULL)
1083 continue;
1085 seq_printf(s, "pin %d (%s): %s\n", pin,
1086 desc->name ? desc->name : "unnamed",
1087 desc->mux_function ? desc->mux_function
1088 : "UNCLAIMED");
1091 return 0;
1094 static int pinmux_hogs_show(struct seq_file *s, void *what)
1096 struct pinctrl_dev *pctldev = s->private;
1097 struct pinmux_hog *hog;
1099 seq_puts(s, "Pinmux map hogs held by device\n");
1101 list_for_each_entry(hog, &pctldev->pinmux_hogs, node)
1102 seq_printf(s, "%s\n", hog->map->name);
1104 return 0;
1107 static int pinmux_show(struct seq_file *s, void *what)
1109 struct pinmux *pmx;
1111 seq_puts(s, "Requested pinmuxes and their maps:\n");
1112 list_for_each_entry(pmx, &pinmux_list, node) {
1113 struct pinctrl_dev *pctldev = pmx->pctldev;
1114 const struct pinmux_ops *pmxops;
1115 const struct pinctrl_ops *pctlops;
1116 struct pinmux_group *grp;
1118 if (!pctldev) {
1119 seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
1120 continue;
1123 pmxops = pctldev->desc->pmxops;
1124 pctlops = pctldev->desc->pctlops;
1126 seq_printf(s, "device: %s function: %s (%u),",
1127 pinctrl_dev_get_name(pmx->pctldev),
1128 pmxops->get_function_name(pctldev,
1129 pmx->func_selector),
1130 pmx->func_selector);
1132 seq_printf(s, " groups: [");
1133 list_for_each_entry(grp, &pmx->groups, node) {
1134 seq_printf(s, " %s (%u)",
1135 pctlops->get_group_name(pctldev,
1136 grp->group_selector),
1137 grp->group_selector);
1139 seq_printf(s, " ]");
1141 seq_printf(s, " users: %u map-> %s\n",
1142 pmx->usecount,
1143 pmx->dev ? dev_name(pmx->dev) : "(system)");
1146 return 0;
1149 static int pinmux_maps_show(struct seq_file *s, void *what)
1151 int i;
1153 seq_puts(s, "Pinmux maps:\n");
1155 for (i = 0; i < pinmux_maps_num; i++) {
1156 struct pinmux_map const *map = &pinmux_maps[i];
1158 seq_printf(s, "%s:\n", map->name);
1159 if (map->dev || map->dev_name)
1160 seq_printf(s, " device: %s\n",
1161 map->dev ? dev_name(map->dev) :
1162 map->dev_name);
1163 else
1164 seq_printf(s, " SYSTEM MUX\n");
1165 seq_printf(s, " controlling device %s\n",
1166 map->ctrl_dev ? dev_name(map->ctrl_dev) :
1167 map->ctrl_dev_name);
1168 seq_printf(s, " function: %s\n", map->function);
1169 seq_printf(s, " group: %s\n", map->group ? map->group :
1170 "(default)");
1172 return 0;
1175 static int pinmux_functions_open(struct inode *inode, struct file *file)
1177 return single_open(file, pinmux_functions_show, inode->i_private);
1180 static int pinmux_pins_open(struct inode *inode, struct file *file)
1182 return single_open(file, pinmux_pins_show, inode->i_private);
1185 static int pinmux_hogs_open(struct inode *inode, struct file *file)
1187 return single_open(file, pinmux_hogs_show, inode->i_private);
1190 static int pinmux_open(struct inode *inode, struct file *file)
1192 return single_open(file, pinmux_show, NULL);
1195 static int pinmux_maps_open(struct inode *inode, struct file *file)
1197 return single_open(file, pinmux_maps_show, NULL);
1200 static const struct file_operations pinmux_functions_ops = {
1201 .open = pinmux_functions_open,
1202 .read = seq_read,
1203 .llseek = seq_lseek,
1204 .release = single_release,
1207 static const struct file_operations pinmux_pins_ops = {
1208 .open = pinmux_pins_open,
1209 .read = seq_read,
1210 .llseek = seq_lseek,
1211 .release = single_release,
1214 static const struct file_operations pinmux_hogs_ops = {
1215 .open = pinmux_hogs_open,
1216 .read = seq_read,
1217 .llseek = seq_lseek,
1218 .release = single_release,
1221 static const struct file_operations pinmux_ops = {
1222 .open = pinmux_open,
1223 .read = seq_read,
1224 .llseek = seq_lseek,
1225 .release = single_release,
1228 static const struct file_operations pinmux_maps_ops = {
1229 .open = pinmux_maps_open,
1230 .read = seq_read,
1231 .llseek = seq_lseek,
1232 .release = single_release,
1235 void pinmux_init_device_debugfs(struct dentry *devroot,
1236 struct pinctrl_dev *pctldev)
1238 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
1239 devroot, pctldev, &pinmux_functions_ops);
1240 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
1241 devroot, pctldev, &pinmux_pins_ops);
1242 debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
1243 devroot, pctldev, &pinmux_hogs_ops);
1246 void pinmux_init_debugfs(struct dentry *subsys_root)
1248 debugfs_create_file("pinmuxes", S_IFREG | S_IRUGO,
1249 subsys_root, NULL, &pinmux_ops);
1250 debugfs_create_file("pinmux-maps", S_IFREG | S_IRUGO,
1251 subsys_root, NULL, &pinmux_maps_ops);
1254 #endif /* CONFIG_DEBUG_FS */