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>
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;
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
46 struct list_head node;
47 unsigned group_selector;
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 * @pins: an array of discrete physical pins used in this mapping, taken
57 * from the global pin enumeration space (copied from pinmux map)
58 * @num_pins: the number of pins in this mapping array, i.e. the number of
59 * elements in .pins so we can iterate over that array (copied from
61 * @pctldev: pin control device handling this pinmux
62 * @func_selector: the function selector for the pinmux device handling
64 * @groups: the group selectors for the pinmux device and
65 * selector combination handling this pinmux, this is a list that
66 * will be traversed on all pinmux operations such as
67 * get/put/enable/disable
68 * @mutex: a lock for the pinmux state holder
71 struct list_head node;
74 struct pinctrl_dev *pctldev;
75 unsigned func_selector;
76 struct list_head groups;
81 * struct pinmux_hog - a list item to stash mux hogs
82 * @node: pinmux hog list node
83 * @map: map entry responsible for this hogging
84 * @pmx: the pinmux hogged by this item
87 struct list_head node;
88 struct pinmux_map const *map;
93 * pin_request() - request a single pin to be muxed in, typically for GPIO
94 * @pin: the pin number in the global pin space
95 * @function: a functional name to give to this pin, passed to the driver
96 * so it knows what function to mux in, e.g. the string "gpioNN"
97 * means that you want to mux in the pin for use as GPIO number NN
98 * @gpio_range: the range matching the GPIO pin if this is a request for a
101 static int pin_request(struct pinctrl_dev *pctldev,
102 int pin, const char *function,
103 struct pinctrl_gpio_range *gpio_range)
105 struct pin_desc *desc;
106 const struct pinmux_ops *ops = pctldev->desc->pmxops;
107 int status = -EINVAL;
109 dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, function);
111 desc = pin_desc_get(pctldev, pin);
113 dev_err(pctldev->dev,
114 "pin is not registered so it cannot be requested\n");
119 dev_err(pctldev->dev, "no function name given\n");
123 spin_lock(&desc->lock);
124 if (desc->mux_function) {
125 spin_unlock(&desc->lock);
126 dev_err(pctldev->dev,
127 "pin already requested\n");
130 desc->mux_function = function;
131 spin_unlock(&desc->lock);
133 /* Let each pin increase references to this module */
134 if (!try_module_get(pctldev->owner)) {
135 dev_err(pctldev->dev,
136 "could not increase module refcount for pin %d\n",
143 * If there is no kind of request function for the pin we just assume
144 * we got it by default and proceed.
146 if (gpio_range && ops->gpio_request_enable)
147 /* This requests and enables a single GPIO pin */
148 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
149 else if (ops->request)
150 status = ops->request(pctldev, pin);
155 dev_err(pctldev->dev, "->request on device %s failed for pin %d\n",
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, this should be marked with
337 * __initdata so it can be discarded after boot, this function will
338 * perform a shallow copy for the mapping entries.
339 * @num_maps: the number of maps in the mapping table
341 * Only call this once during initialization of your machine, the function is
342 * tagged as __init and won't be callable after init has completed. The map
343 * passed into this function will be owned by the pinmux core and cannot be
346 int __init pinmux_register_mappings(struct pinmux_map const *maps,
352 pr_debug("add %d pinmux maps\n", num_maps);
354 /* First sanity check the new mapping */
355 for (i = 0; i < num_maps; i++) {
357 pr_err("failed to register map %d: no map name given\n",
362 if (!maps[i].ctrl_dev && !maps[i].ctrl_dev_name) {
363 pr_err("failed to register map %s (%d): no pin control device given\n",
368 if (!maps[i].function) {
369 pr_err("failed to register map %s (%d): no function ID given\n",
374 if (!maps[i].dev && !maps[i].dev_name)
375 pr_debug("add system map %s function %s with no device\n",
379 pr_debug("register map %s, function %s\n",
385 * Make a copy of the map array - string pointers will end up in the
386 * kernel const section anyway so these do not need to be deep copied.
388 if (!pinmux_maps_num) {
389 /* On first call, just copy them */
390 tmp_maps = kmemdup(maps,
391 sizeof(struct pinmux_map) * num_maps,
396 /* Subsequent calls, reallocate array to new size */
397 size_t oldsize = sizeof(struct pinmux_map) * pinmux_maps_num;
398 size_t newsize = sizeof(struct pinmux_map) * num_maps;
400 tmp_maps = krealloc(pinmux_maps, oldsize + newsize, GFP_KERNEL);
403 memcpy((tmp_maps + oldsize), maps, newsize);
406 pinmux_maps = tmp_maps;
407 pinmux_maps_num += num_maps;
412 * acquire_pins() - acquire all the pins for a certain funcion on a pinmux
413 * @pctldev: the device to take the pins on
414 * @func_selector: the function selector to acquire the pins for
415 * @group_selector: the group selector containing the pins to acquire
417 static int acquire_pins(struct pinctrl_dev *pctldev,
418 unsigned func_selector,
419 unsigned group_selector)
421 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
422 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
423 const char *func = pmxops->get_function_name(pctldev,
425 const unsigned *pins;
430 ret = pctlops->get_group_pins(pctldev, group_selector,
435 dev_dbg(pctldev->dev, "requesting the %u pins from group %u\n",
436 num_pins, group_selector);
438 /* Try to allocate all pins in this group, one by one */
439 for (i = 0; i < num_pins; i++) {
440 ret = pin_request(pctldev, pins[i], func, NULL);
442 dev_err(pctldev->dev,
443 "could not get pin %d for function %s on device %s - conflicting mux mappings?\n",
444 pins[i], func ? : "(undefined)",
445 pinctrl_dev_get_name(pctldev));
446 /* On error release all taken pins */
447 i--; /* this pin just failed */
449 pin_free(pctldev, pins[i], NULL);
457 * release_pins() - release pins taken by earlier acquirement
458 * @pctldev: the device to free the pinx on
459 * @group_selector: the group selector containing the pins to free
461 static void release_pins(struct pinctrl_dev *pctldev,
462 unsigned group_selector)
464 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
465 const unsigned *pins;
470 ret = pctlops->get_group_pins(pctldev, group_selector,
473 dev_err(pctldev->dev, "could not get pins to release for group selector %d\n",
477 for (i = 0; i < num_pins; i++)
478 pin_free(pctldev, pins[i], NULL);
482 * pinmux_check_pin_group() - check function and pin group combo
483 * @pctldev: device to check the pin group vs function for
484 * @func_selector: the function selector to check the pin group for, we have
485 * already looked this up in the calling function
486 * @pin_group: the pin group to match to the function
488 * This function will check that the pinmux driver can supply the
489 * selected pin group for a certain function, returns the group selector if
490 * the group and function selector will work fine together, else returns
493 static int pinmux_check_pin_group(struct pinctrl_dev *pctldev,
494 unsigned func_selector,
495 const char *pin_group)
497 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
498 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
502 * If the driver does not support different pin groups for the
503 * functions, we only support group 0, and assume this exists.
505 if (!pctlops || !pctlops->list_groups)
509 * Passing NULL (no specific group) will select the first and
510 * hopefully only group of pins available for this function.
513 char const * const *groups;
516 ret = pmxops->get_function_groups(pctldev, func_selector,
517 &groups, &num_groups);
522 ret = pinctrl_get_group_selector(pctldev, groups[0]);
524 dev_err(pctldev->dev,
525 "function %s wants group %s but the pin controller does not seem to have that group\n",
526 pmxops->get_function_name(pctldev, func_selector),
532 dev_dbg(pctldev->dev,
533 "function %s support more than one group, default-selecting first group %s (%d)\n",
534 pmxops->get_function_name(pctldev, func_selector),
541 dev_dbg(pctldev->dev,
542 "check if we have pin group %s on controller %s\n",
543 pin_group, pinctrl_dev_get_name(pctldev));
545 ret = pinctrl_get_group_selector(pctldev, pin_group);
547 dev_dbg(pctldev->dev,
548 "%s does not support pin group %s with function %s\n",
549 pinctrl_dev_get_name(pctldev),
551 pmxops->get_function_name(pctldev, func_selector));
557 * pinmux_search_function() - check pin control driver for a certain function
558 * @pctldev: device to check for function and position
559 * @map: function map containing the function and position to look for
560 * @func_selector: returns the applicable function selector if found
561 * @group_selector: returns the applicable group selector if found
563 * This will search the pinmux driver for an applicable
564 * function with a specific pin group, returns 0 if these can be mapped
567 static int pinmux_search_function(struct pinctrl_dev *pctldev,
568 struct pinmux_map const *map,
569 unsigned *func_selector,
570 unsigned *group_selector)
572 const struct pinmux_ops *ops = pctldev->desc->pmxops;
573 unsigned selector = 0;
575 /* See if this pctldev has this function */
576 while (ops->list_functions(pctldev, selector) >= 0) {
577 const char *fname = ops->get_function_name(pctldev,
581 if (!strcmp(map->function, fname)) {
582 /* Found the function, check pin group */
583 ret = pinmux_check_pin_group(pctldev, selector,
588 /* This function and group selector can be used */
589 *func_selector = selector;
590 *group_selector = ret;
597 pr_err("%s does not support function %s\n",
598 pinctrl_dev_get_name(pctldev), map->function);
603 * pinmux_enable_muxmap() - enable a map entry for a certain pinmux
605 static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
609 struct pinmux_map const *map)
611 unsigned func_selector;
612 unsigned group_selector;
613 struct pinmux_group *grp;
617 * Note that we're not locking the pinmux mutex here, because
618 * this is only called at pinmux initialization time when it
619 * has not been added to any list and thus is not reachable
623 if (pmx->pctldev && pmx->pctldev != pctldev) {
624 dev_err(pctldev->dev,
625 "different pin control devices given for device %s, function %s\n",
626 devname, map->function);
630 pmx->pctldev = pctldev;
632 /* Now go into the driver and try to match a function and group */
633 ret = pinmux_search_function(pctldev, map, &func_selector,
639 * If the function selector is already set, it needs to be identical,
640 * we support several groups with one function but not several
641 * functions with one or several groups in the same pinmux.
643 if (pmx->func_selector != UINT_MAX &&
644 pmx->func_selector != func_selector) {
645 dev_err(pctldev->dev,
646 "dual function defines in the map for device %s\n",
650 pmx->func_selector = func_selector;
652 /* Now add this group selector, we may have many of them */
653 grp = kmalloc(sizeof(struct pinmux_group), GFP_KERNEL);
656 grp->group_selector = group_selector;
657 ret = acquire_pins(pctldev, func_selector, group_selector);
662 list_add(&grp->node, &pmx->groups);
667 static void pinmux_free_groups(struct pinmux *pmx)
669 struct list_head *node, *tmp;
671 list_for_each_safe(node, tmp, &pmx->groups) {
672 struct pinmux_group *grp =
673 list_entry(node, struct pinmux_group, node);
674 /* Release all pins taken by this group */
675 release_pins(pmx->pctldev, grp->group_selector);
682 * pinmux_get() - retrieves the pinmux for a certain device
683 * @dev: the device to get the pinmux for
684 * @name: an optional specific mux mapping name or NULL, the name is only
685 * needed if you want to have more than one mapping per device, or if you
686 * need an anonymous pinmux (not tied to any specific device)
688 struct pinmux *pinmux_get(struct device *dev, const char *name)
690 struct pinmux_map const *map = NULL;
691 struct pinctrl_dev *pctldev = NULL;
692 const char *devname = NULL;
695 unsigned num_maps = 0;
699 /* We must have dev or ID or both */
701 return ERR_PTR(-EINVAL);
704 devname = dev_name(dev);
706 pr_debug("get mux %s for device %s\n", name,
707 devname ? devname : "(none)");
710 * create the state cookie holder struct pinmux for each
711 * mapping, this is what consumers will get when requesting
712 * a pinmux handle with pinmux_get()
714 pmx = kzalloc(sizeof(struct pinmux), GFP_KERNEL);
716 return ERR_PTR(-ENOMEM);
717 mutex_init(&pmx->mutex);
718 pmx->func_selector = UINT_MAX;
719 INIT_LIST_HEAD(&pmx->groups);
721 /* Iterate over the pinmux maps to locate the right ones */
722 for (i = 0; i < pinmux_maps_num; i++) {
723 map = &pinmux_maps[i];
727 * First, try to find the pctldev given in the map
729 pctldev = get_pinctrl_dev_from_dev(map->ctrl_dev,
732 const char *devname = NULL;
735 devname = dev_name(map->ctrl_dev);
736 else if (map->ctrl_dev_name)
737 devname = map->ctrl_dev_name;
739 pr_warning("could not find a pinctrl device for pinmux function %s, fishy, they shall all have one\n",
741 pr_warning("given pinctrl device name: %s",
742 devname ? devname : "UNDEFINED");
744 /* Continue to check the other mappings anyway... */
748 pr_debug("in map, found pctldev %s to handle function %s",
749 dev_name(pctldev->dev), map->function);
753 * If we're looking for a specific named map, this must match,
754 * else we loop and look for the next.
757 if (map->name == NULL)
759 if (strcmp(map->name, name))
764 * This is for the case where no device name is given, we
765 * already know that the function name matches from above
768 if (!map->dev_name && (name != NULL))
771 /* If the mapping has a device set up it must match */
773 (!devname || !strcmp(map->dev_name, devname)))
777 /* If this map is applicable, then apply it */
779 ret = pinmux_enable_muxmap(pctldev, pmx, dev,
782 pinmux_free_groups(pmx);
791 /* We should have atleast one map, right */
793 pr_err("could not find any mux maps for device %s, ID %s\n",
794 devname ? devname : "(anonymous)",
795 name ? name : "(undefined)");
797 return ERR_PTR(-EINVAL);
800 pr_debug("found %u mux maps for device %s, UD %s\n",
802 devname ? devname : "(anonymous)",
803 name ? name : "(undefined)");
805 /* Add the pinmux to the global list */
806 mutex_lock(&pinmux_list_mutex);
807 list_add(&pmx->node, &pinmux_list);
808 mutex_unlock(&pinmux_list_mutex);
812 EXPORT_SYMBOL_GPL(pinmux_get);
815 * pinmux_put() - release a previously claimed pinmux
816 * @pmx: a pinmux previously claimed by pinmux_get()
818 void pinmux_put(struct pinmux *pmx)
823 mutex_lock(&pmx->mutex);
825 pr_warn("releasing pinmux with active users!\n");
826 /* Free the groups and all acquired pins */
827 pinmux_free_groups(pmx);
828 mutex_unlock(&pmx->mutex);
830 /* Remove from list */
831 mutex_lock(&pinmux_list_mutex);
832 list_del(&pmx->node);
833 mutex_unlock(&pinmux_list_mutex);
837 EXPORT_SYMBOL_GPL(pinmux_put);
840 * pinmux_enable() - enable a certain pinmux setting
841 * @pmx: the pinmux to enable, previously claimed by pinmux_get()
843 int pinmux_enable(struct pinmux *pmx)
849 mutex_lock(&pmx->mutex);
850 if (pmx->usecount++ == 0) {
851 struct pinctrl_dev *pctldev = pmx->pctldev;
852 const struct pinmux_ops *ops = pctldev->desc->pmxops;
853 struct pinmux_group *grp;
855 list_for_each_entry(grp, &pmx->groups, node) {
856 ret = ops->enable(pctldev, pmx->func_selector,
857 grp->group_selector);
860 * TODO: call disable() on all groups we called
861 * enable() on to this point?
868 mutex_unlock(&pmx->mutex);
871 EXPORT_SYMBOL_GPL(pinmux_enable);
874 * pinmux_disable() - disable a certain pinmux setting
875 * @pmx: the pinmux to disable, previously claimed by pinmux_get()
877 void pinmux_disable(struct pinmux *pmx)
882 mutex_lock(&pmx->mutex);
883 if (--pmx->usecount == 0) {
884 struct pinctrl_dev *pctldev = pmx->pctldev;
885 const struct pinmux_ops *ops = pctldev->desc->pmxops;
886 struct pinmux_group *grp;
888 list_for_each_entry(grp, &pmx->groups, node) {
889 ops->disable(pctldev, pmx->func_selector,
890 grp->group_selector);
893 mutex_unlock(&pmx->mutex);
895 EXPORT_SYMBOL_GPL(pinmux_disable);
897 int pinmux_check_ops(const struct pinmux_ops *ops)
899 /* Check that we implement required operations */
900 if (!ops->list_functions ||
901 !ops->get_function_name ||
902 !ops->get_function_groups ||
910 /* Hog a single map entry and add to the hoglist */
911 static int pinmux_hog_map(struct pinctrl_dev *pctldev,
912 struct pinmux_map const *map)
914 struct pinmux_hog *hog;
918 if (map->dev || map->dev_name) {
920 * TODO: the day we have device tree support, we can
921 * traverse the device tree and hog to specific device nodes
922 * without any problems, so then we can hog pinmuxes for
923 * all devices that just want a static pin mux at this point.
925 dev_err(pctldev->dev, "map %s wants to hog a non-system pinmux, this is not going to work\n",
930 hog = kzalloc(sizeof(struct pinmux_hog), GFP_KERNEL);
934 pmx = pinmux_get(NULL, map->name);
937 dev_err(pctldev->dev,
938 "could not get the %s pinmux mapping for hogging\n",
943 ret = pinmux_enable(pmx);
947 dev_err(pctldev->dev,
948 "could not enable the %s pinmux mapping for hogging\n",
956 dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name,
958 mutex_lock(&pctldev->pinmux_hogs_lock);
959 list_add(&hog->node, &pctldev->pinmux_hogs);
960 mutex_unlock(&pctldev->pinmux_hogs_lock);
966 * pinmux_hog_maps() - hog specific map entries on controller device
967 * @pctldev: the pin control device to hog entries on
969 * When the pin controllers are registered, there may be some specific pinmux
970 * map entries that need to be hogged, i.e. get+enabled until the system shuts
973 int pinmux_hog_maps(struct pinctrl_dev *pctldev)
975 struct device *dev = pctldev->dev;
976 const char *devname = dev_name(dev);
980 INIT_LIST_HEAD(&pctldev->pinmux_hogs);
981 mutex_init(&pctldev->pinmux_hogs_lock);
983 for (i = 0; i < pinmux_maps_num; i++) {
984 struct pinmux_map const *map = &pinmux_maps[i];
986 if (((map->ctrl_dev == dev) ||
987 !strcmp(map->ctrl_dev_name, devname)) &&
989 /* OK time to hog! */
990 ret = pinmux_hog_map(pctldev, map);
999 * pinmux_unhog_maps() - unhog specific map entries on controller device
1000 * @pctldev: the pin control device to unhog entries on
1002 void pinmux_unhog_maps(struct pinctrl_dev *pctldev)
1004 struct list_head *node, *tmp;
1006 mutex_lock(&pctldev->pinmux_hogs_lock);
1007 list_for_each_safe(node, tmp, &pctldev->pinmux_hogs) {
1008 struct pinmux_hog *hog =
1009 list_entry(node, struct pinmux_hog, node);
1010 pinmux_disable(hog->pmx);
1011 pinmux_put(hog->pmx);
1015 mutex_unlock(&pctldev->pinmux_hogs_lock);
1018 #ifdef CONFIG_DEBUG_FS
1020 /* Called from pincontrol core */
1021 static int pinmux_functions_show(struct seq_file *s, void *what)
1023 struct pinctrl_dev *pctldev = s->private;
1024 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
1025 unsigned func_selector = 0;
1027 while (pmxops->list_functions(pctldev, func_selector) >= 0) {
1028 const char *func = pmxops->get_function_name(pctldev,
1030 const char * const *groups;
1031 unsigned num_groups;
1035 ret = pmxops->get_function_groups(pctldev, func_selector,
1036 &groups, &num_groups);
1038 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
1041 seq_printf(s, "function: %s, groups = [ ", func);
1042 for (i = 0; i < num_groups; i++)
1043 seq_printf(s, "%s ", groups[i]);
1053 static int pinmux_pins_show(struct seq_file *s, void *what)
1055 struct pinctrl_dev *pctldev = s->private;
1058 seq_puts(s, "Pinmux settings per pin\n");
1059 seq_puts(s, "Format: pin (name): pinmuxfunction\n");
1061 /* The pin number can be retrived from the pin controller descriptor */
1062 for (i = 0; i < pctldev->desc->npins; i++) {
1064 struct pin_desc *desc;
1066 pin = pctldev->desc->pins[i].number;
1067 desc = pin_desc_get(pctldev, pin);
1068 /* Skip if we cannot search the pin */
1072 seq_printf(s, "pin %d (%s): %s\n", pin,
1073 desc->name ? desc->name : "unnamed",
1074 desc->mux_function ? desc->mux_function
1081 static int pinmux_hogs_show(struct seq_file *s, void *what)
1083 struct pinctrl_dev *pctldev = s->private;
1084 struct pinmux_hog *hog;
1086 seq_puts(s, "Pinmux map hogs held by device\n");
1088 list_for_each_entry(hog, &pctldev->pinmux_hogs, node)
1089 seq_printf(s, "%s\n", hog->map->name);
1094 static int pinmux_show(struct seq_file *s, void *what)
1098 seq_puts(s, "Requested pinmuxes and their maps:\n");
1099 list_for_each_entry(pmx, &pinmux_list, node) {
1100 struct pinctrl_dev *pctldev = pmx->pctldev;
1101 const struct pinmux_ops *pmxops;
1102 const struct pinctrl_ops *pctlops;
1103 struct pinmux_group *grp;
1106 seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
1110 pmxops = pctldev->desc->pmxops;
1111 pctlops = pctldev->desc->pctlops;
1113 seq_printf(s, "device: %s function: %s (%u),",
1114 pinctrl_dev_get_name(pmx->pctldev),
1115 pmxops->get_function_name(pctldev,
1116 pmx->func_selector),
1117 pmx->func_selector);
1119 seq_printf(s, " groups: [");
1120 list_for_each_entry(grp, &pmx->groups, node) {
1121 seq_printf(s, " %s (%u)",
1122 pctlops->get_group_name(pctldev,
1123 grp->group_selector),
1124 grp->group_selector);
1126 seq_printf(s, " ]");
1128 seq_printf(s, " users: %u map-> %s\n",
1130 pmx->dev ? dev_name(pmx->dev) : "(system)");
1136 static int pinmux_maps_show(struct seq_file *s, void *what)
1140 seq_puts(s, "Pinmux maps:\n");
1142 for (i = 0; i < pinmux_maps_num; i++) {
1143 struct pinmux_map const *map = &pinmux_maps[i];
1145 seq_printf(s, "%s:\n", map->name);
1146 if (map->dev || map->dev_name)
1147 seq_printf(s, " device: %s\n",
1148 map->dev ? dev_name(map->dev) :
1151 seq_printf(s, " SYSTEM MUX\n");
1152 seq_printf(s, " controlling device %s\n",
1153 map->ctrl_dev ? dev_name(map->ctrl_dev) :
1154 map->ctrl_dev_name);
1155 seq_printf(s, " function: %s\n", map->function);
1156 seq_printf(s, " group: %s\n", map->group ? map->group :
1162 static int pinmux_functions_open(struct inode *inode, struct file *file)
1164 return single_open(file, pinmux_functions_show, inode->i_private);
1167 static int pinmux_pins_open(struct inode *inode, struct file *file)
1169 return single_open(file, pinmux_pins_show, inode->i_private);
1172 static int pinmux_hogs_open(struct inode *inode, struct file *file)
1174 return single_open(file, pinmux_hogs_show, inode->i_private);
1177 static int pinmux_open(struct inode *inode, struct file *file)
1179 return single_open(file, pinmux_show, NULL);
1182 static int pinmux_maps_open(struct inode *inode, struct file *file)
1184 return single_open(file, pinmux_maps_show, NULL);
1187 static const struct file_operations pinmux_functions_ops = {
1188 .open = pinmux_functions_open,
1190 .llseek = seq_lseek,
1191 .release = single_release,
1194 static const struct file_operations pinmux_pins_ops = {
1195 .open = pinmux_pins_open,
1197 .llseek = seq_lseek,
1198 .release = single_release,
1201 static const struct file_operations pinmux_hogs_ops = {
1202 .open = pinmux_hogs_open,
1204 .llseek = seq_lseek,
1205 .release = single_release,
1208 static const struct file_operations pinmux_ops = {
1209 .open = pinmux_open,
1211 .llseek = seq_lseek,
1212 .release = single_release,
1215 static const struct file_operations pinmux_maps_ops = {
1216 .open = pinmux_maps_open,
1218 .llseek = seq_lseek,
1219 .release = single_release,
1222 void pinmux_init_device_debugfs(struct dentry *devroot,
1223 struct pinctrl_dev *pctldev)
1225 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO,
1226 devroot, pctldev, &pinmux_functions_ops);
1227 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
1228 devroot, pctldev, &pinmux_pins_ops);
1229 debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
1230 devroot, pctldev, &pinmux_hogs_ops);
1233 void pinmux_init_debugfs(struct dentry *subsys_root)
1235 debugfs_create_file("pinmuxes", S_IFREG | S_IRUGO,
1236 subsys_root, NULL, &pinmux_ops);
1237 debugfs_create_file("pinmux-maps", S_IFREG | S_IRUGO,
1238 subsys_root, NULL, &pinmux_maps_ops);
1241 #endif /* CONFIG_DEBUG_FS */