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/sysfs.h>
25 #include <linux/debugfs.h>
26 #include <linux/seq_file.h>
27 #include <linux/pinctrl/machine.h>
28 #include <linux/pinctrl/pinmux.h>
31 /* List of pinmuxes */
32 static DEFINE_MUTEX(pinmux_list_mutex);
33 static LIST_HEAD(pinmux_list);
35 /* Global pinmux maps, we allow one set only */
36 static struct pinmux_map const *pinmux_maps;
37 static unsigned pinmux_maps_num;
40 * struct pinmux_group - group list item for pinmux groups
41 * @node: pinmux group list node
42 * @group_selector: the group selector for this group
45 struct list_head node;
46 unsigned group_selector;
50 * struct pinmux - per-device pinmux state holder
51 * @node: global list node
52 * @dev: the device using this pinmux
53 * @usecount: the number of active users of this mux setting, used to keep
54 * track of nested use cases
55 * @pins: an array of discrete physical pins used in this mapping, taken
56 * from the global pin enumeration space (copied from pinmux map)
57 * @num_pins: the number of pins in this mapping array, i.e. the number of
58 * elements in .pins so we can iterate over that array (copied from
60 * @pctldev: pin control device handling this pinmux
61 * @func_selector: the function selector for the pinmux device handling
63 * @groups: the group selectors for the pinmux device and
64 * selector combination handling this pinmux, this is a list that
65 * will be traversed on all pinmux operations such as
66 * get/put/enable/disable
67 * @mutex: a lock for the pinmux state holder
70 struct list_head node;
73 struct pinctrl_dev *pctldev;
74 unsigned func_selector;
75 struct list_head groups;
80 * struct pinmux_hog - a list item to stash mux hogs
81 * @node: pinmux hog list node
82 * @map: map entry responsible for this hogging
83 * @pmx: the pinmux hogged by this item
86 struct list_head node;
87 struct pinmux_map const *map;
92 * pin_request() - request a single pin to be muxed in, typically for GPIO
93 * @pin: the pin number in the global pin space
94 * @function: a functional name to give to this pin, passed to the driver
95 * so it knows what function to mux in, e.g. the string "gpioNN"
96 * means that you want to mux in the pin for use as GPIO number NN
97 * @gpio_range: the range matching the GPIO pin if this is a request for a
100 static int pin_request(struct pinctrl_dev *pctldev,
101 int pin, const char *function,
102 struct pinctrl_gpio_range *gpio_range)
104 struct pin_desc *desc;
105 const struct pinmux_ops *ops = pctldev->desc->pmxops;
106 int status = -EINVAL;
108 dev_dbg(&pctldev->dev, "request pin %d for %s\n", pin, function);
110 desc = pin_desc_get(pctldev, pin);
112 dev_err(&pctldev->dev,
113 "pin is not registered so it cannot be requested\n");
118 dev_err(&pctldev->dev, "no function name given\n");
122 spin_lock(&desc->lock);
123 if (desc->mux_function) {
124 spin_unlock(&desc->lock);
125 dev_err(&pctldev->dev,
126 "pin already requested\n");
129 desc->mux_function = function;
130 spin_unlock(&desc->lock);
132 /* Let each pin increase references to this module */
133 if (!try_module_get(pctldev->owner)) {
134 dev_err(&pctldev->dev,
135 "could not increase module refcount for pin %d\n",
142 * If there is no kind of request function for the pin we just assume
143 * we got it by default and proceed.
145 if (gpio_range && ops->gpio_request_enable)
146 /* This requests and enables a single GPIO pin */
147 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
148 else if (ops->request)
149 status = ops->request(pctldev, pin);
154 dev_err(&pctldev->dev, "->request on device %s failed "
156 pctldev->desc->name, pin);
159 spin_lock(&desc->lock);
160 desc->mux_function = NULL;
161 spin_unlock(&desc->lock);
165 dev_err(&pctldev->dev, "pin-%d (%s) status %d\n",
166 pin, function ? : "?", status);
172 * pin_free() - release a single muxed in pin so something else can be muxed
173 * @pctldev: pin controller device handling this pin
174 * @pin: the pin to free
175 * @gpio_range: the range matching the GPIO pin if this is a request for a
178 * This function returns a pointer to the function name in use. This is used
179 * for callers that dynamically allocate a function name so it can be freed
180 * once the pin is free. This is done for GPIO request functions.
182 static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
183 struct pinctrl_gpio_range *gpio_range)
185 const struct pinmux_ops *ops = pctldev->desc->pmxops;
186 struct pin_desc *desc;
189 desc = pin_desc_get(pctldev, pin);
191 dev_err(&pctldev->dev,
192 "pin is not registered so it cannot be freed\n");
197 * If there is no kind of request function for the pin we just assume
198 * we got it by default and proceed.
200 if (gpio_range && ops->gpio_disable_free)
201 ops->gpio_disable_free(pctldev, gpio_range, pin);
203 ops->free(pctldev, pin);
205 spin_lock(&desc->lock);
206 func = desc->mux_function;
207 desc->mux_function = NULL;
208 spin_unlock(&desc->lock);
209 module_put(pctldev->owner);
215 * pinmux_request_gpio() - request a single pin to be muxed in as GPIO
216 * @gpio: the GPIO pin number from the GPIO subsystem number space
218 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
219 * as part of their gpio_request() semantics, platforms and individual drivers
220 * shall *NOT* request GPIO pins to be muxed in.
222 int pinmux_request_gpio(unsigned gpio)
225 const char *function;
226 struct pinctrl_dev *pctldev;
227 struct pinctrl_gpio_range *range;
231 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
235 /* Convert to the pin controllers number space */
236 pin = gpio - range->base + range->pin_base;
238 /* Conjure some name stating what chip and pin this is taken by */
239 snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
241 function = kstrdup(gpiostr, GFP_KERNEL);
245 ret = pin_request(pctldev, pin, function, range);
251 EXPORT_SYMBOL_GPL(pinmux_request_gpio);
254 * pinmux_free_gpio() - free a single pin, currently used as GPIO
255 * @gpio: the GPIO pin number from the GPIO subsystem number space
257 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
258 * as part of their gpio_free() semantics, platforms and individual drivers
259 * shall *NOT* request GPIO pins to be muxed out.
261 void pinmux_free_gpio(unsigned gpio)
263 struct pinctrl_dev *pctldev;
264 struct pinctrl_gpio_range *range;
269 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
273 /* Convert to the pin controllers number space */
274 pin = gpio - range->base + range->pin_base;
276 func = pin_free(pctldev, pin, range);
279 EXPORT_SYMBOL_GPL(pinmux_free_gpio);
281 static int pinmux_gpio_direction(unsigned gpio, bool input)
283 struct pinctrl_dev *pctldev;
284 struct pinctrl_gpio_range *range;
285 const struct pinmux_ops *ops;
289 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
293 ops = pctldev->desc->pmxops;
295 /* Convert to the pin controllers number space */
296 pin = gpio - range->base + range->pin_base;
298 if (ops->gpio_set_direction)
299 ret = ops->gpio_set_direction(pctldev, range, pin, input);
307 * pinmux_gpio_direction_input() - request a GPIO pin to go into input mode
308 * @gpio: the GPIO pin number from the GPIO subsystem number space
310 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
311 * as part of their gpio_direction_input() semantics, platforms and individual
312 * drivers shall *NOT* touch pinmux GPIO calls.
314 int pinmux_gpio_direction_input(unsigned gpio)
316 return pinmux_gpio_direction(gpio, true);
318 EXPORT_SYMBOL_GPL(pinmux_gpio_direction_input);
321 * pinmux_gpio_direction_output() - request a GPIO pin to go into output mode
322 * @gpio: the GPIO pin number from the GPIO subsystem number space
324 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
325 * as part of their gpio_direction_output() semantics, platforms and individual
326 * drivers shall *NOT* touch pinmux GPIO calls.
328 int pinmux_gpio_direction_output(unsigned gpio)
330 return pinmux_gpio_direction(gpio, false);
332 EXPORT_SYMBOL_GPL(pinmux_gpio_direction_output);
335 * pinmux_register_mappings() - register a set of pinmux mappings
336 * @maps: the pinmux mappings table to register
337 * @num_maps: the number of maps in the mapping table
339 * Only call this once during initialization of your machine, the function is
340 * tagged as __init and won't be callable after init has completed. The map
341 * passed into this function will be owned by the pinmux core and cannot be
344 int __init pinmux_register_mappings(struct pinmux_map const *maps,
349 if (pinmux_maps != NULL) {
350 pr_err("pinmux mappings already registered, you can only "
351 "register one set of maps\n");
355 pr_debug("add %d pinmux maps\n", num_maps);
356 for (i = 0; i < num_maps; i++) {
357 /* Sanity check the mapping */
359 pr_err("failed to register map %d: "
360 "no map name given\n", i);
363 if (!maps[i].ctrl_dev && !maps[i].ctrl_dev_name) {
364 pr_err("failed to register map %s (%d): "
365 "no pin control device given\n",
369 if (!maps[i].function) {
370 pr_err("failed to register map %s (%d): "
371 "no function ID given\n", maps[i].name, i);
375 if (!maps[i].dev && !maps[i].dev_name)
376 pr_debug("add system map %s function %s with no device\n",
380 pr_debug("register map %s, function %s\n",
386 pinmux_maps_num = num_maps;
392 * acquire_pins() - acquire all the pins for a certain funcion on a pinmux
393 * @pctldev: the device to take the pins on
394 * @func_selector: the function selector to acquire the pins for
395 * @group_selector: the group selector containing the pins to acquire
397 static int acquire_pins(struct pinctrl_dev *pctldev,
398 unsigned func_selector,
399 unsigned group_selector)
401 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
402 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
403 const char *func = pmxops->get_function_name(pctldev,
405 const unsigned *pins;
410 ret = pctlops->get_group_pins(pctldev, group_selector,
415 dev_dbg(&pctldev->dev, "requesting the %u pins from group %u\n",
416 num_pins, group_selector);
418 /* Try to allocate all pins in this group, one by one */
419 for (i = 0; i < num_pins; i++) {
420 ret = pin_request(pctldev, pins[i], func, NULL);
422 dev_err(&pctldev->dev,
423 "could not get pin %d for function %s "
424 "on device %s - conflicting mux mappings?\n",
425 pins[i], func ? : "(undefined)",
426 pinctrl_dev_get_name(pctldev));
427 /* On error release all taken pins */
428 i--; /* this pin just failed */
430 pin_free(pctldev, pins[i], NULL);
438 * release_pins() - release pins taken by earlier acquirement
439 * @pctldev: the device to free the pinx on
440 * @group_selector: the group selector containing the pins to free
442 static void release_pins(struct pinctrl_dev *pctldev,
443 unsigned group_selector)
445 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
446 const unsigned *pins;
451 ret = pctlops->get_group_pins(pctldev, group_selector,
454 dev_err(&pctldev->dev, "could not get pins to release for "
455 "group selector %d\n",
459 for (i = 0; i < num_pins; i++)
460 pin_free(pctldev, pins[i], NULL);
464 * pinmux_check_pin_group() - check function and pin group combo
465 * @pctldev: device to check the pin group vs function for
466 * @func_selector: the function selector to check the pin group for, we have
467 * already looked this up in the calling function
468 * @pin_group: the pin group to match to the function
470 * This function will check that the pinmux driver can supply the
471 * selected pin group for a certain function, returns the group selector if
472 * the group and function selector will work fine together, else returns
475 static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
476 unsigned func_selector,
477 const char *pin_group)
479 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
480 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
484 * If the driver does not support different pin groups for the
485 * functions, we only support group 0, and assume this exists.
487 if (!pctlops || !pctlops->list_groups)
491 * Passing NULL (no specific group) will select the first and
492 * hopefully only group of pins available for this function.
495 char const * const *groups;
498 ret = pmxops->get_function_groups(pctldev, func_selector,
499 &groups, &num_groups);
504 ret = pinctrl_get_group_selector(pctldev, groups[0]);
506 dev_err(&pctldev->dev,
507 "function %s wants group %s but the pin "
508 "controller does not seem to have that group\n",
509 pmxops->get_function_name(pctldev, func_selector),
515 dev_dbg(&pctldev->dev,
516 "function %s support more than one group, "
517 "default-selecting first group %s (%d)\n",
518 pmxops->get_function_name(pctldev, func_selector),
525 dev_dbg(&pctldev->dev,
526 "check if we have pin group %s on controller %s\n",
527 pin_group, pinctrl_dev_get_name(pctldev));
529 ret = pinctrl_get_group_selector(pctldev, pin_group);
531 dev_dbg(&pctldev->dev,
532 "%s does not support pin group %s with function %s\n",
533 pinctrl_dev_get_name(pctldev),
535 pmxops->get_function_name(pctldev, func_selector));
541 * pinmux_search_function() - check pin control driver for a certain function
542 * @pctldev: device to check for function and position
543 * @map: function map containing the function and position to look for
544 * @func_selector: returns the applicable function selector if found
545 * @group_selector: returns the applicable group selector if found
547 * This will search the pinmux driver for an applicable
548 * function with a specific pin group, returns 0 if these can be mapped
551 static int pinmux_search_function(struct pinctrl_dev *pctldev,
552 struct pinmux_map const *map,
553 unsigned *func_selector,
554 unsigned *group_selector)
556 const struct pinmux_ops *ops = pctldev->desc->pmxops;
557 unsigned selector = 0;
559 /* See if this pctldev has this function */
560 while (ops->list_functions(pctldev, selector) >= 0) {
561 const char *fname = ops->get_function_name(pctldev,
565 if (!strcmp(map->function, fname)) {
566 /* Found the function, check pin group */
567 ret = pinmux_check_pin_group(pctldev, selector,
572 /* This function and group selector can be used */
573 *func_selector = selector;
574 *group_selector = ret;
581 pr_err("%s does not support function %s\n",
582 pinctrl_dev_get_name(pctldev), map->function);
587 * pinmux_enable_muxmap() - enable a map entry for a certain pinmux
589 static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
593 struct pinmux_map const *map)
595 unsigned func_selector;
596 unsigned group_selector;
597 struct pinmux_group *grp;
601 * Note that we're not locking the pinmux mutex here, because
602 * this is only called at pinmux initialization time when it
603 * has not been added to any list and thus is not reachable
607 if (pmx->pctldev && pmx->pctldev != pctldev) {
608 dev_err(&pctldev->dev,
609 "different pin control devices given for device %s, "
616 pmx->pctldev = pctldev;
618 /* Now go into the driver and try to match a function and group */
619 ret = pinmux_search_function(pctldev, map, &func_selector,
625 * If the function selector is already set, it needs to be identical,
626 * we support several groups with one function but not several
627 * functions with one or several groups in the same pinmux.
629 if (pmx->func_selector != UINT_MAX &&
630 pmx->func_selector != func_selector) {
631 dev_err(&pctldev->dev,
632 "dual function defines in the map for device %s\n",
636 pmx->func_selector = func_selector;
638 /* Now add this group selector, we may have many of them */
639 grp = kmalloc(sizeof(struct pinmux_group), GFP_KERNEL);
642 grp->group_selector = group_selector;
643 ret = acquire_pins(pctldev, func_selector, group_selector);
648 list_add(&grp->node, &pmx->groups);
653 static void pinmux_free_groups(struct pinmux *pmx)
655 struct list_head *node, *tmp;
657 list_for_each_safe(node, tmp, &pmx->groups) {
658 struct pinmux_group *grp =
659 list_entry(node, struct pinmux_group, node);
660 /* Release all pins taken by this group */
661 release_pins(pmx->pctldev, grp->group_selector);
668 * pinmux_get() - retrieves the pinmux for a certain device
669 * @dev: the device to get the pinmux for
670 * @name: an optional specific mux mapping name or NULL, the name is only
671 * needed if you want to have more than one mapping per device, or if you
672 * need an anonymous pinmux (not tied to any specific device)
674 struct pinmux *pinmux_get(struct device *dev, const char *name)
677 struct pinmux_map const *map = NULL;
678 struct pinctrl_dev *pctldev = NULL;
679 const char *devname = NULL;
682 unsigned num_maps = 0;
686 /* We must have dev or ID or both */
688 return ERR_PTR(-EINVAL);
691 devname = dev_name(dev);
693 pr_debug("get mux %s for device %s\n", name,
694 devname ? devname : "(none)");
697 * create the state cookie holder struct pinmux for each
698 * mapping, this is what consumers will get when requesting
699 * a pinmux handle with pinmux_get()
701 pmx = kzalloc(sizeof(struct pinmux), GFP_KERNEL);
703 return ERR_PTR(-ENOMEM);
704 mutex_init(&pmx->mutex);
705 pmx->func_selector = UINT_MAX;
706 INIT_LIST_HEAD(&pmx->groups);
708 /* Iterate over the pinmux maps to locate the right ones */
709 for (i = 0; i < pinmux_maps_num; i++) {
710 map = &pinmux_maps[i];
714 * First, try to find the pctldev given in the map
716 pctldev = get_pinctrl_dev_from_dev(map->ctrl_dev,
719 const char *devname = NULL;
722 devname = dev_name(map->ctrl_dev);
723 else if (map->ctrl_dev_name)
724 devname = map->ctrl_dev_name;
726 pr_warning("could not find a pinctrl device for pinmux "
727 "function %s, fishy, they shall all have one\n",
729 pr_warning("given pinctrl device name: %s",
730 devname ? devname : "UNDEFINED");
732 /* Continue to check the other mappings anyway... */
736 pr_debug("in map, found pctldev %s to handle function %s",
737 dev_name(&pctldev->dev), map->function);
741 * If we're looking for a specific named map, this must match,
742 * else we loop and look for the next.
745 if (map->name == NULL)
747 if (strcmp(map->name, name))
752 * This is for the case where no device name is given, we
753 * already know that the function name matches from above
756 if (!map->dev_name && (name != NULL))
759 /* If the mapping has a device set up it must match */
761 (!devname || !strcmp(map->dev_name, devname)))
765 /* If this map is applicable, then apply it */
767 ret = pinmux_enable_muxmap(pctldev, pmx, dev,
770 pinmux_free_groups(pmx);
779 /* We should have atleast one map, right */
781 pr_err("could not find any mux maps for device %s, ID %s\n",
782 devname ? devname : "(anonymous)",
783 name ? name : "(undefined)");
785 return ERR_PTR(-EINVAL);
788 pr_debug("found %u mux maps for device %s, UD %s\n",
790 devname ? devname : "(anonymous)",
791 name ? name : "(undefined)");
793 /* Add the pinmux to the global list */
794 mutex_lock(&pinmux_list_mutex);
795 list_add(&pmx->node, &pinmux_list);
796 mutex_unlock(&pinmux_list_mutex);
800 EXPORT_SYMBOL_GPL(pinmux_get);
803 * pinmux_put() - release a previously claimed pinmux
804 * @pmx: a pinmux previously claimed by pinmux_get()
806 void pinmux_put(struct pinmux *pmx)
811 mutex_lock(&pmx->mutex);
813 pr_warn("releasing pinmux with active users!\n");
814 /* Free the groups and all acquired pins */
815 pinmux_free_groups(pmx);
816 mutex_unlock(&pmx->mutex);
818 /* Remove from list */
819 mutex_lock(&pinmux_list_mutex);
820 list_del(&pmx->node);
821 mutex_unlock(&pinmux_list_mutex);
825 EXPORT_SYMBOL_GPL(pinmux_put);
828 * pinmux_enable() - enable a certain pinmux setting
829 * @pmx: the pinmux to enable, previously claimed by pinmux_get()
831 int pinmux_enable(struct pinmux *pmx)
837 mutex_lock(&pmx->mutex);
838 if (pmx->usecount++ == 0) {
839 struct pinctrl_dev *pctldev = pmx->pctldev;
840 const struct pinmux_ops *ops = pctldev->desc->pmxops;
841 struct pinmux_group *grp;
843 list_for_each_entry(grp, &pmx->groups, node) {
844 ret = ops->enable(pctldev, pmx->func_selector,
845 grp->group_selector);
848 * TODO: call disable() on all groups we called
849 * enable() on to this point?
856 mutex_unlock(&pmx->mutex);
859 EXPORT_SYMBOL_GPL(pinmux_enable);
862 * pinmux_disable() - disable a certain pinmux setting
863 * @pmx: the pinmux to disable, previously claimed by pinmux_get()
865 void pinmux_disable(struct pinmux *pmx)
870 mutex_lock(&pmx->mutex);
871 if (--pmx->usecount == 0) {
872 struct pinctrl_dev *pctldev = pmx->pctldev;
873 const struct pinmux_ops *ops = pctldev->desc->pmxops;
874 struct pinmux_group *grp;
876 list_for_each_entry(grp, &pmx->groups, node) {
877 ops->disable(pctldev, pmx->func_selector,
878 grp->group_selector);
881 mutex_unlock(&pmx->mutex);
883 EXPORT_SYMBOL_GPL(pinmux_disable);
885 int pinmux_check_ops(const struct pinmux_ops *ops)
887 /* Check that we implement required operations */
888 if (!ops->list_functions ||
889 !ops->get_function_name ||
890 !ops->get_function_groups ||
898 /* Hog a single map entry and add to the hoglist */
899 static int pinmux_hog_map(struct pinctrl_dev *pctldev,
900 struct pinmux_map const *map)
902 struct pinmux_hog *hog;
906 if (map->dev || map->dev_name) {
908 * TODO: the day we have device tree support, we can
909 * traverse the device tree and hog to specific device nodes
910 * without any problems, so then we can hog pinmuxes for
911 * all devices that just want a static pin mux at this point.
913 dev_err(&pctldev->dev, "map %s wants to hog a non-system "
914 "pinmux, this is not going to work\n", map->name);
918 hog = kzalloc(sizeof(struct pinmux_hog), GFP_KERNEL);
922 pmx = pinmux_get(NULL, map->name);
925 dev_err(&pctldev->dev,
926 "could not get the %s pinmux mapping for hogging\n",
931 ret = pinmux_enable(pmx);
935 dev_err(&pctldev->dev,
936 "could not enable the %s pinmux mapping for hogging\n",
944 dev_info(&pctldev->dev, "hogged map %s, function %s\n", map->name,
946 mutex_lock(&pctldev->pinmux_hogs_lock);
947 list_add(&hog->node, &pctldev->pinmux_hogs);
948 mutex_unlock(&pctldev->pinmux_hogs_lock);
954 * pinmux_hog_maps() - hog specific map entries on controller device
955 * @pctldev: the pin control device to hog entries on
957 * When the pin controllers are registered, there may be some specific pinmux
958 * map entries that need to be hogged, i.e. get+enabled until the system shuts
961 int pinmux_hog_maps(struct pinctrl_dev *pctldev)
963 struct device *dev = &pctldev->dev;
964 const char *devname = dev_name(dev);
968 INIT_LIST_HEAD(&pctldev->pinmux_hogs);
969 mutex_init(&pctldev->pinmux_hogs_lock);
971 for (i = 0; i < pinmux_maps_num; i++) {
972 struct pinmux_map const *map = &pinmux_maps[i];
974 if (((map->ctrl_dev == dev) ||
975 !strcmp(map->ctrl_dev_name, devname)) &&
977 /* OK time to hog! */
978 ret = pinmux_hog_map(pctldev, map);
987 * pinmux_unhog_maps() - unhog specific map entries on controller device
988 * @pctldev: the pin control device to unhog entries on
990 void pinmux_unhog_maps(struct pinctrl_dev *pctldev)
992 struct list_head *node, *tmp;
994 mutex_lock(&pctldev->pinmux_hogs_lock);
995 list_for_each_safe(node, tmp, &pctldev->pinmux_hogs) {
996 struct pinmux_hog *hog =
997 list_entry(node, struct pinmux_hog, node);
998 pinmux_disable(hog->pmx);
999 pinmux_put(hog->pmx);
1003 mutex_unlock(&pctldev->pinmux_hogs_lock);
1006 #ifdef CONFIG_DEBUG_FS
1008 /* Called from pincontrol core */
1009 static int pinmux_functions_show(struct seq_file *s, void *what)
1011 struct pinctrl_dev *pctldev = s->private;
1012 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
1013 unsigned func_selector = 0;
1015 while (pmxops->list_functions(pctldev, func_selector) >= 0) {
1016 const char *func = pmxops->get_function_name(pctldev,
1018 const char * const *groups;
1019 unsigned num_groups;
1023 ret = pmxops->get_function_groups(pctldev, func_selector,
1024 &groups, &num_groups);
1026 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
1029 seq_printf(s, "function: %s, groups = [ ", func);
1030 for (i = 0; i < num_groups; i++)
1031 seq_printf(s, "%s ", groups[i]);
1041 static int pinmux_pins_show(struct seq_file *s, void *what)
1043 struct pinctrl_dev *pctldev = s->private;
1046 seq_puts(s, "Pinmux settings per pin\n");
1047 seq_puts(s, "Format: pin (name): pinmuxfunction\n");
1049 /* The highest pin number need to be included in the loop, thus <= */
1050 for (pin = 0; pin <= pctldev->desc->maxpin; pin++) {
1052 struct pin_desc *desc;
1054 desc = pin_desc_get(pctldev, pin);
1055 /* Pin space may be sparse */
1059 seq_printf(s, "pin %d (%s): %s\n", pin,
1060 desc->name ? desc->name : "unnamed",
1061 desc->mux_function ? desc->mux_function
1068 static int pinmux_hogs_show(struct seq_file *s, void *what)
1070 struct pinctrl_dev *pctldev = s->private;
1071 struct pinmux_hog *hog;
1073 seq_puts(s, "Pinmux map hogs held by device\n");
1075 list_for_each_entry(hog, &pctldev->pinmux_hogs, node)
1076 seq_printf(s, "%s\n", hog->map->name);
1081 static int pinmux_show(struct seq_file *s, void *what)
1085 seq_puts(s, "Requested pinmuxes and their maps:\n");
1086 list_for_each_entry(pmx, &pinmux_list, node) {
1087 struct pinctrl_dev *pctldev = pmx->pctldev;
1088 const struct pinmux_ops *pmxops;
1089 const struct pinctrl_ops *pctlops;
1090 struct pinmux_group *grp;
1093 seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
1097 pmxops = pctldev->desc->pmxops;
1098 pctlops = pctldev->desc->pctlops;
1100 seq_printf(s, "device: %s function: %s (%u),",
1101 pinctrl_dev_get_name(pmx->pctldev),
1102 pmxops->get_function_name(pctldev, pmx->func_selector),
1103 pmx->func_selector);
1105 seq_printf(s, " groups: [");
1106 list_for_each_entry(grp, &pmx->groups, node) {
1107 seq_printf(s, " %s (%u)",
1108 pctlops->get_group_name(pctldev, grp->group_selector),
1109 grp->group_selector);
1111 seq_printf(s, " ]");
1113 seq_printf(s, " users: %u map-> %s\n",
1115 pmx->dev ? dev_name(pmx->dev) : "(system)");
1121 static int pinmux_maps_show(struct seq_file *s, void *what)
1125 seq_puts(s, "Pinmux maps:\n");
1127 for (i = 0; i < pinmux_maps_num; i++) {
1128 struct pinmux_map const *map = &pinmux_maps[i];
1130 seq_printf(s, "%s:\n", map->name);
1131 if (map->dev || map->dev_name)
1132 seq_printf(s, " device: %s\n",
1133 map->dev ? dev_name(map->dev) :
1136 seq_printf(s, " SYSTEM MUX\n");
1137 seq_printf(s, " controlling device %s\n",
1138 map->ctrl_dev ? dev_name(map->ctrl_dev) :
1139 map->ctrl_dev_name);
1140 seq_printf(s, " function: %s\n", map->function);
1141 seq_printf(s, " group: %s\n", map->group ? map->group :
1147 static int pinmux_functions_open(struct inode *inode, struct file *file)
1149 return single_open(file, pinmux_functions_show, inode->i_private);
1152 static int pinmux_pins_open(struct inode *inode, struct file *file)
1154 return single_open(file, pinmux_pins_show, inode->i_private);
1157 static int pinmux_hogs_open(struct inode *inode, struct file *file)
1159 return single_open(file, pinmux_hogs_show, inode->i_private);
1162 static int pinmux_open(struct inode *inode, struct file *file)
1164 return single_open(file, pinmux_show, NULL);
1167 static int pinmux_maps_open(struct inode *inode, struct file *file)
1169 return single_open(file, pinmux_maps_show, NULL);
1172 static const struct file_operations pinmux_functions_ops = {
1173 .open = pinmux_functions_open,
1175 .llseek = seq_lseek,
1176 .release = single_release,
1179 static const struct file_operations pinmux_pins_ops = {
1180 .open = pinmux_pins_open,
1182 .llseek = seq_lseek,
1183 .release = single_release,
1186 static const struct file_operations pinmux_hogs_ops = {
1187 .open = pinmux_hogs_open,
1189 .llseek = seq_lseek,
1190 .release = single_release,
1193 static const struct file_operations pinmux_ops = {
1194 .open = pinmux_open,
1196 .llseek = seq_lseek,
1197 .release = single_release,
1200 static const struct file_operations pinmux_maps_ops = {
1201 .open = pinmux_maps_open,
1203 .llseek = seq_lseek,
1204 .release = single_release,
1207 void pinmux_init_device_debugfs(struct dentry *devroot,
1208 struct pinctrl_dev *pctldev)
1210 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
1211 devroot, pctldev, &pinmux_functions_ops);
1212 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
1213 devroot, pctldev, &pinmux_pins_ops);
1214 debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
1215 devroot, pctldev, &pinmux_hogs_ops);
1218 void pinmux_init_debugfs(struct dentry *subsys_root)
1220 debugfs_create_file("pinmuxes", S_IFREG | S_IRUGO,
1221 subsys_root, NULL, &pinmux_ops);
1222 debugfs_create_file("pinmux-maps", S_IFREG | S_IRUGO,
1223 subsys_root, NULL, &pinmux_maps_ops);
1226 #endif /* CONFIG_DEBUG_FS */