2 * Core driver for the pin control subsystem
4 * Copyright (C) 2011-2012 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 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
12 * License terms: GNU General Public License (GPL) version 2
14 #define pr_fmt(fmt) "pinctrl core: " fmt
16 #include <linux/kernel.h>
17 #include <linux/kref.h>
18 #include <linux/export.h>
19 #include <linux/init.h>
20 #include <linux/device.h>
21 #include <linux/slab.h>
22 #include <linux/err.h>
23 #include <linux/list.h>
24 #include <linux/sysfs.h>
25 #include <linux/debugfs.h>
26 #include <linux/seq_file.h>
27 #include <linux/pinctrl/consumer.h>
28 #include <linux/pinctrl/pinctrl.h>
29 #include <linux/pinctrl/machine.h>
31 #include "devicetree.h"
36 static bool pinctrl_dummy_state;
38 /* Mutex taken by all entry points */
39 DEFINE_MUTEX(pinctrl_mutex);
41 /* Global list of pin control devices (struct pinctrl_dev) */
42 LIST_HEAD(pinctrldev_list);
44 /* List of pin controller handles (struct pinctrl) */
45 static LIST_HEAD(pinctrl_list);
47 /* List of pinctrl maps (struct pinctrl_maps) */
48 LIST_HEAD(pinctrl_maps);
52 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
54 * Usually this function is called by platforms without pinctrl driver support
55 * but run with some shared drivers using pinctrl APIs.
56 * After calling this function, the pinctrl core will return successfully
57 * with creating a dummy state for the driver to keep going smoothly.
59 void pinctrl_provide_dummies(void)
61 pinctrl_dummy_state = true;
64 const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
66 /* We're not allowed to register devices without name */
67 return pctldev->desc->name;
69 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
71 const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
73 return dev_name(pctldev->dev);
75 EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
77 void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
79 return pctldev->driver_data;
81 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
84 * get_pinctrl_dev_from_devname() - look up pin controller device
85 * @devname: the name of a device instance, as returned by dev_name()
87 * Looks up a pin control device matching a certain device name or pure device
88 * pointer, the pure device pointer will take precedence.
90 struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
92 struct pinctrl_dev *pctldev = NULL;
98 list_for_each_entry(pctldev, &pinctrldev_list, node) {
99 if (!strcmp(dev_name(pctldev->dev), devname)) {
100 /* Matched on device name */
106 return found ? pctldev : NULL;
110 * pin_get_from_name() - look up a pin number from a name
111 * @pctldev: the pin control device to lookup the pin on
112 * @name: the name of the pin to look up
114 int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
118 /* The pin number can be retrived from the pin controller descriptor */
119 for (i = 0; i < pctldev->desc->npins; i++) {
120 struct pin_desc *desc;
122 pin = pctldev->desc->pins[i].number;
123 desc = pin_desc_get(pctldev, pin);
124 /* Pin space may be sparse */
127 if (desc->name && !strcmp(name, desc->name))
135 * pin_get_name_from_id() - look up a pin name from a pin id
136 * @pctldev: the pin control device to lookup the pin on
137 * @name: the name of the pin to look up
139 const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
141 const struct pin_desc *desc;
143 desc = pin_desc_get(pctldev, pin);
145 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
154 * pin_is_valid() - check if pin exists on controller
155 * @pctldev: the pin control device to check the pin on
156 * @pin: pin to check, use the local pin controller index number
158 * This tells us whether a certain pin exist on a certain pin controller or
159 * not. Pin lists may be sparse, so some pins may not exist.
161 bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
163 struct pin_desc *pindesc;
168 mutex_lock(&pinctrl_mutex);
169 pindesc = pin_desc_get(pctldev, pin);
170 mutex_unlock(&pinctrl_mutex);
172 return pindesc != NULL;
174 EXPORT_SYMBOL_GPL(pin_is_valid);
176 /* Deletes a range of pin descriptors */
177 static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
178 const struct pinctrl_pin_desc *pins,
183 for (i = 0; i < num_pins; i++) {
184 struct pin_desc *pindesc;
186 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
188 if (pindesc != NULL) {
189 radix_tree_delete(&pctldev->pin_desc_tree,
191 if (pindesc->dynamic_name)
192 kfree(pindesc->name);
198 static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
199 unsigned number, const char *name)
201 struct pin_desc *pindesc;
203 pindesc = pin_desc_get(pctldev, number);
204 if (pindesc != NULL) {
205 pr_err("pin %d already registered on %s\n", number,
206 pctldev->desc->name);
210 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
211 if (pindesc == NULL) {
212 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
217 pindesc->pctldev = pctldev;
219 /* Copy basic pin info */
221 pindesc->name = name;
223 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
224 if (pindesc->name == NULL) {
228 pindesc->dynamic_name = true;
231 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
232 pr_debug("registered pin %d (%s) on %s\n",
233 number, pindesc->name, pctldev->desc->name);
237 static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
238 struct pinctrl_pin_desc const *pins,
244 for (i = 0; i < num_descs; i++) {
245 ret = pinctrl_register_one_pin(pctldev,
246 pins[i].number, pins[i].name);
255 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
256 * @pctldev: pin controller device to check
257 * @gpio: gpio pin to check taken from the global GPIO pin space
259 * Tries to match a GPIO pin number to the ranges handled by a certain pin
260 * controller, return the range or NULL
262 static struct pinctrl_gpio_range *
263 pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
265 struct pinctrl_gpio_range *range = NULL;
267 /* Loop over the ranges */
268 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
269 /* Check if we're in the valid range */
270 if (gpio >= range->base &&
271 gpio < range->base + range->npins) {
280 * pinctrl_get_device_gpio_range() - find device for GPIO range
281 * @gpio: the pin to locate the pin controller for
282 * @outdev: the pin control device if found
283 * @outrange: the GPIO range if found
285 * Find the pin controller handling a certain GPIO pin from the pinspace of
286 * the GPIO subsystem, return the device and the matching GPIO range. Returns
287 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
288 * may still have not been registered.
290 static int pinctrl_get_device_gpio_range(unsigned gpio,
291 struct pinctrl_dev **outdev,
292 struct pinctrl_gpio_range **outrange)
294 struct pinctrl_dev *pctldev = NULL;
296 /* Loop over the pin controllers */
297 list_for_each_entry(pctldev, &pinctrldev_list, node) {
298 struct pinctrl_gpio_range *range;
300 range = pinctrl_match_gpio_range(pctldev, gpio);
308 return -EPROBE_DEFER;
312 * pinctrl_add_gpio_range() - register a GPIO range for a controller
313 * @pctldev: pin controller device to add the range to
314 * @range: the GPIO range to add
316 * This adds a range of GPIOs to be handled by a certain pin controller. Call
317 * this to register handled ranges after registering your pin controller.
319 void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
320 struct pinctrl_gpio_range *range)
322 mutex_lock(&pinctrl_mutex);
323 list_add_tail(&range->node, &pctldev->gpio_ranges);
324 mutex_unlock(&pinctrl_mutex);
326 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
328 void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
329 struct pinctrl_gpio_range *ranges,
334 for (i = 0; i < nranges; i++)
335 pinctrl_add_gpio_range(pctldev, &ranges[i]);
337 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
339 struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
340 struct pinctrl_gpio_range *range)
342 struct pinctrl_dev *pctldev = get_pinctrl_dev_from_devname(devname);
345 * If we can't find this device, let's assume that is because
346 * it has not probed yet, so the driver trying to register this
347 * range need to defer probing.
350 return ERR_PTR(-EPROBE_DEFER);
352 pinctrl_add_gpio_range(pctldev, range);
355 EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
358 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
359 * @pctldev: the pin controller device to look in
360 * @pin: a controller-local number to find the range for
362 struct pinctrl_gpio_range *
363 pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
366 struct pinctrl_gpio_range *range = NULL;
368 /* Loop over the ranges */
369 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
370 /* Check if we're in the valid range */
371 if (pin >= range->pin_base &&
372 pin < range->pin_base + range->npins) {
379 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
382 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
383 * @pctldev: pin controller device to remove the range from
384 * @range: the GPIO range to remove
386 void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
387 struct pinctrl_gpio_range *range)
389 mutex_lock(&pinctrl_mutex);
390 list_del(&range->node);
391 mutex_unlock(&pinctrl_mutex);
393 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
396 * pinctrl_get_group_selector() - returns the group selector for a group
397 * @pctldev: the pin controller handling the group
398 * @pin_group: the pin group to look up
400 int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
401 const char *pin_group)
403 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
404 unsigned ngroups = pctlops->get_groups_count(pctldev);
405 unsigned group_selector = 0;
407 while (group_selector < ngroups) {
408 const char *gname = pctlops->get_group_name(pctldev,
410 if (!strcmp(gname, pin_group)) {
411 dev_dbg(pctldev->dev,
412 "found group selector %u for %s\n",
415 return group_selector;
421 dev_err(pctldev->dev, "does not have pin group %s\n",
428 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
429 * @gpio: the GPIO pin number from the GPIO subsystem number space
431 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
432 * as part of their gpio_request() semantics, platforms and individual drivers
433 * shall *NOT* request GPIO pins to be muxed in.
435 int pinctrl_request_gpio(unsigned gpio)
437 struct pinctrl_dev *pctldev;
438 struct pinctrl_gpio_range *range;
442 mutex_lock(&pinctrl_mutex);
444 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
446 mutex_unlock(&pinctrl_mutex);
450 /* Convert to the pin controllers number space */
451 pin = gpio - range->base + range->pin_base;
453 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
455 mutex_unlock(&pinctrl_mutex);
458 EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
461 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
462 * @gpio: the GPIO pin number from the GPIO subsystem number space
464 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
465 * as part of their gpio_free() semantics, platforms and individual drivers
466 * shall *NOT* request GPIO pins to be muxed out.
468 void pinctrl_free_gpio(unsigned gpio)
470 struct pinctrl_dev *pctldev;
471 struct pinctrl_gpio_range *range;
475 mutex_lock(&pinctrl_mutex);
477 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
479 mutex_unlock(&pinctrl_mutex);
483 /* Convert to the pin controllers number space */
484 pin = gpio - range->base + range->pin_base;
486 pinmux_free_gpio(pctldev, pin, range);
488 mutex_unlock(&pinctrl_mutex);
490 EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
492 static int pinctrl_gpio_direction(unsigned gpio, bool input)
494 struct pinctrl_dev *pctldev;
495 struct pinctrl_gpio_range *range;
499 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
503 /* Convert to the pin controllers number space */
504 pin = gpio - range->base + range->pin_base;
506 return pinmux_gpio_direction(pctldev, range, pin, input);
510 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
511 * @gpio: the GPIO pin number from the GPIO subsystem number space
513 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
514 * as part of their gpio_direction_input() semantics, platforms and individual
515 * drivers shall *NOT* touch pin control GPIO calls.
517 int pinctrl_gpio_direction_input(unsigned gpio)
520 mutex_lock(&pinctrl_mutex);
521 ret = pinctrl_gpio_direction(gpio, true);
522 mutex_unlock(&pinctrl_mutex);
525 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
528 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
529 * @gpio: the GPIO pin number from the GPIO subsystem number space
531 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
532 * as part of their gpio_direction_output() semantics, platforms and individual
533 * drivers shall *NOT* touch pin control GPIO calls.
535 int pinctrl_gpio_direction_output(unsigned gpio)
538 mutex_lock(&pinctrl_mutex);
539 ret = pinctrl_gpio_direction(gpio, false);
540 mutex_unlock(&pinctrl_mutex);
543 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
545 static struct pinctrl_state *find_state(struct pinctrl *p,
548 struct pinctrl_state *state;
550 list_for_each_entry(state, &p->states, node)
551 if (!strcmp(state->name, name))
557 static struct pinctrl_state *create_state(struct pinctrl *p,
560 struct pinctrl_state *state;
562 state = kzalloc(sizeof(*state), GFP_KERNEL);
565 "failed to alloc struct pinctrl_state\n");
566 return ERR_PTR(-ENOMEM);
570 INIT_LIST_HEAD(&state->settings);
572 list_add_tail(&state->node, &p->states);
577 static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
579 struct pinctrl_state *state;
580 struct pinctrl_setting *setting;
583 state = find_state(p, map->name);
585 state = create_state(p, map->name);
587 return PTR_ERR(state);
589 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
592 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
593 if (setting == NULL) {
595 "failed to alloc struct pinctrl_setting\n");
599 setting->type = map->type;
601 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
602 if (setting->pctldev == NULL) {
604 /* Do not defer probing of hogs (circular loop) */
605 if (!strcmp(map->ctrl_dev_name, map->dev_name))
608 * OK let us guess that the driver is not there yet, and
609 * let's defer obtaining this pinctrl handle to later...
611 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
613 return -EPROBE_DEFER;
616 setting->dev_name = map->dev_name;
619 case PIN_MAP_TYPE_MUX_GROUP:
620 ret = pinmux_map_to_setting(map, setting);
622 case PIN_MAP_TYPE_CONFIGS_PIN:
623 case PIN_MAP_TYPE_CONFIGS_GROUP:
624 ret = pinconf_map_to_setting(map, setting);
635 list_add_tail(&setting->node, &state->settings);
640 static struct pinctrl *find_pinctrl(struct device *dev)
644 list_for_each_entry(p, &pinctrl_list, node)
651 static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
653 static struct pinctrl *create_pinctrl(struct device *dev)
657 struct pinctrl_maps *maps_node;
659 struct pinctrl_map const *map;
663 * create the state cookie holder struct pinctrl for each
664 * mapping, this is what consumers will get when requesting
665 * a pin control handle with pinctrl_get()
667 p = kzalloc(sizeof(*p), GFP_KERNEL);
669 dev_err(dev, "failed to alloc struct pinctrl\n");
670 return ERR_PTR(-ENOMEM);
673 INIT_LIST_HEAD(&p->states);
674 INIT_LIST_HEAD(&p->dt_maps);
676 ret = pinctrl_dt_to_map(p);
682 devname = dev_name(dev);
684 /* Iterate over the pin control maps to locate the right ones */
685 for_each_maps(maps_node, i, map) {
686 /* Map must be for this device */
687 if (strcmp(map->dev_name, devname))
690 ret = add_setting(p, map);
692 * At this point the adding of a setting may:
694 * - Defer, if the pinctrl device is not yet available
695 * - Fail, if the pinctrl device is not yet available,
696 * AND the setting is a hog. We cannot defer that, since
697 * the hog will kick in immediately after the device
700 * If the error returned was not -EPROBE_DEFER then we
701 * accumulate the errors to see if we end up with
702 * an -EPROBE_DEFER later, as that is the worst case.
704 if (ret == -EPROBE_DEFER) {
705 pinctrl_put_locked(p, false);
710 /* If some other error than deferral occured, return here */
711 pinctrl_put_locked(p, false);
715 kref_init(&p->users);
717 /* Add the pinctrl handle to the global list */
718 list_add_tail(&p->node, &pinctrl_list);
723 static struct pinctrl *pinctrl_get_locked(struct device *dev)
728 return ERR_PTR(-EINVAL);
731 * See if somebody else (such as the device core) has already
732 * obtained a handle to the pinctrl for this device. In that case,
733 * return another pointer to it.
735 p = find_pinctrl(dev);
737 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
742 return create_pinctrl(dev);
746 * pinctrl_get() - retrieves the pinctrl handle for a device
747 * @dev: the device to obtain the handle for
749 struct pinctrl *pinctrl_get(struct device *dev)
753 mutex_lock(&pinctrl_mutex);
754 p = pinctrl_get_locked(dev);
755 mutex_unlock(&pinctrl_mutex);
759 EXPORT_SYMBOL_GPL(pinctrl_get);
761 static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
763 struct pinctrl_state *state, *n1;
764 struct pinctrl_setting *setting, *n2;
766 list_for_each_entry_safe(state, n1, &p->states, node) {
767 list_for_each_entry_safe(setting, n2, &state->settings, node) {
768 switch (setting->type) {
769 case PIN_MAP_TYPE_MUX_GROUP:
770 if (state == p->state)
771 pinmux_disable_setting(setting);
772 pinmux_free_setting(setting);
774 case PIN_MAP_TYPE_CONFIGS_PIN:
775 case PIN_MAP_TYPE_CONFIGS_GROUP:
776 pinconf_free_setting(setting);
781 list_del(&setting->node);
784 list_del(&state->node);
788 pinctrl_dt_free_maps(p);
796 * pinctrl_release() - release the pinctrl handle
797 * @kref: the kref in the pinctrl being released
799 static void pinctrl_release(struct kref *kref)
801 struct pinctrl *p = container_of(kref, struct pinctrl, users);
803 pinctrl_put_locked(p, true);
807 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
808 * @p: the pinctrl handle to release
810 void pinctrl_put(struct pinctrl *p)
812 mutex_lock(&pinctrl_mutex);
813 kref_put(&p->users, pinctrl_release);
814 mutex_unlock(&pinctrl_mutex);
816 EXPORT_SYMBOL_GPL(pinctrl_put);
818 static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
821 struct pinctrl_state *state;
823 state = find_state(p, name);
825 if (pinctrl_dummy_state) {
826 /* create dummy state */
827 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
829 state = create_state(p, name);
831 state = ERR_PTR(-ENODEV);
838 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
839 * @p: the pinctrl handle to retrieve the state from
840 * @name: the state name to retrieve
842 struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
844 struct pinctrl_state *s;
846 mutex_lock(&pinctrl_mutex);
847 s = pinctrl_lookup_state_locked(p, name);
848 mutex_unlock(&pinctrl_mutex);
852 EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
854 static int pinctrl_select_state_locked(struct pinctrl *p,
855 struct pinctrl_state *state)
857 struct pinctrl_setting *setting, *setting2;
860 if (p->state == state)
865 * The set of groups with a mux configuration in the old state
866 * may not be identical to the set of groups with a mux setting
867 * in the new state. While this might be unusual, it's entirely
868 * possible for the "user"-supplied mapping table to be written
869 * that way. For each group that was configured in the old state
870 * but not in the new state, this code puts that group into a
871 * safe/disabled state.
873 list_for_each_entry(setting, &p->state->settings, node) {
875 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
877 list_for_each_entry(setting2, &state->settings, node) {
878 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
880 if (setting2->data.mux.group ==
881 setting->data.mux.group) {
887 pinmux_disable_setting(setting);
893 /* Apply all the settings for the new state */
894 list_for_each_entry(setting, &state->settings, node) {
895 switch (setting->type) {
896 case PIN_MAP_TYPE_MUX_GROUP:
897 ret = pinmux_enable_setting(setting);
899 case PIN_MAP_TYPE_CONFIGS_PIN:
900 case PIN_MAP_TYPE_CONFIGS_GROUP:
901 ret = pinconf_apply_setting(setting);
908 /* FIXME: Difficult to return to prev state */
917 * pinctrl_select() - select/activate/program a pinctrl state to HW
918 * @p: the pinctrl handle for the device that requests configuratio
919 * @state: the state handle to select/activate/program
921 int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
925 mutex_lock(&pinctrl_mutex);
926 ret = pinctrl_select_state_locked(p, state);
927 mutex_unlock(&pinctrl_mutex);
931 EXPORT_SYMBOL_GPL(pinctrl_select_state);
933 static void devm_pinctrl_release(struct device *dev, void *res)
935 pinctrl_put(*(struct pinctrl **)res);
939 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
940 * @dev: the device to obtain the handle for
942 * If there is a need to explicitly destroy the returned struct pinctrl,
943 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
945 struct pinctrl *devm_pinctrl_get(struct device *dev)
947 struct pinctrl **ptr, *p;
949 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
951 return ERR_PTR(-ENOMEM);
953 p = pinctrl_get(dev);
956 devres_add(dev, ptr);
963 EXPORT_SYMBOL_GPL(devm_pinctrl_get);
965 static int devm_pinctrl_match(struct device *dev, void *res, void *data)
967 struct pinctrl **p = res;
973 * devm_pinctrl_put() - Resource managed pinctrl_put()
974 * @p: the pinctrl handle to release
976 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
977 * this function will not need to be called and the resource management
978 * code will ensure that the resource is freed.
980 void devm_pinctrl_put(struct pinctrl *p)
982 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
983 devm_pinctrl_match, p));
985 EXPORT_SYMBOL_GPL(devm_pinctrl_put);
987 int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
988 bool dup, bool locked)
991 struct pinctrl_maps *maps_node;
993 pr_debug("add %d pinmux maps\n", num_maps);
995 /* First sanity check the new mapping */
996 for (i = 0; i < num_maps; i++) {
997 if (!maps[i].dev_name) {
998 pr_err("failed to register map %s (%d): no device given\n",
1003 if (!maps[i].name) {
1004 pr_err("failed to register map %d: no map name given\n",
1009 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1010 !maps[i].ctrl_dev_name) {
1011 pr_err("failed to register map %s (%d): no pin control device given\n",
1016 switch (maps[i].type) {
1017 case PIN_MAP_TYPE_DUMMY_STATE:
1019 case PIN_MAP_TYPE_MUX_GROUP:
1020 ret = pinmux_validate_map(&maps[i], i);
1024 case PIN_MAP_TYPE_CONFIGS_PIN:
1025 case PIN_MAP_TYPE_CONFIGS_GROUP:
1026 ret = pinconf_validate_map(&maps[i], i);
1031 pr_err("failed to register map %s (%d): invalid type given\n",
1037 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1039 pr_err("failed to alloc struct pinctrl_maps\n");
1043 maps_node->num_maps = num_maps;
1045 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1047 if (!maps_node->maps) {
1048 pr_err("failed to duplicate mapping table\n");
1053 maps_node->maps = maps;
1057 mutex_lock(&pinctrl_mutex);
1058 list_add_tail(&maps_node->node, &pinctrl_maps);
1060 mutex_unlock(&pinctrl_mutex);
1066 * pinctrl_register_mappings() - register a set of pin controller mappings
1067 * @maps: the pincontrol mappings table to register. This should probably be
1068 * marked with __initdata so it can be discarded after boot. This
1069 * function will perform a shallow copy for the mapping entries.
1070 * @num_maps: the number of maps in the mapping table
1072 int pinctrl_register_mappings(struct pinctrl_map const *maps,
1075 return pinctrl_register_map(maps, num_maps, true, false);
1078 void pinctrl_unregister_map(struct pinctrl_map const *map)
1080 struct pinctrl_maps *maps_node;
1082 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1083 if (maps_node->maps == map) {
1084 list_del(&maps_node->node);
1091 * pinctrl_force_sleep() - turn a given controller device into sleep state
1092 * @pctldev: pin controller device
1094 int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1096 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1097 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1100 EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1103 * pinctrl_force_default() - turn a given controller device into default state
1104 * @pctldev: pin controller device
1106 int pinctrl_force_default(struct pinctrl_dev *pctldev)
1108 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1109 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1112 EXPORT_SYMBOL_GPL(pinctrl_force_default);
1114 #ifdef CONFIG_DEBUG_FS
1116 static int pinctrl_pins_show(struct seq_file *s, void *what)
1118 struct pinctrl_dev *pctldev = s->private;
1119 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1122 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
1124 mutex_lock(&pinctrl_mutex);
1126 /* The pin number can be retrived from the pin controller descriptor */
1127 for (i = 0; i < pctldev->desc->npins; i++) {
1128 struct pin_desc *desc;
1130 pin = pctldev->desc->pins[i].number;
1131 desc = pin_desc_get(pctldev, pin);
1132 /* Pin space may be sparse */
1136 seq_printf(s, "pin %d (%s) ", pin,
1137 desc->name ? desc->name : "unnamed");
1139 /* Driver-specific info per pin */
1140 if (ops->pin_dbg_show)
1141 ops->pin_dbg_show(pctldev, s, pin);
1146 mutex_unlock(&pinctrl_mutex);
1151 static int pinctrl_groups_show(struct seq_file *s, void *what)
1153 struct pinctrl_dev *pctldev = s->private;
1154 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1155 unsigned ngroups, selector = 0;
1157 ngroups = ops->get_groups_count(pctldev);
1158 mutex_lock(&pinctrl_mutex);
1160 seq_puts(s, "registered pin groups:\n");
1161 while (selector < ngroups) {
1162 const unsigned *pins;
1164 const char *gname = ops->get_group_name(pctldev, selector);
1169 ret = ops->get_group_pins(pctldev, selector,
1172 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1175 seq_printf(s, "group: %s\n", gname);
1176 for (i = 0; i < num_pins; i++) {
1177 pname = pin_get_name(pctldev, pins[i]);
1178 if (WARN_ON(!pname)) {
1179 mutex_unlock(&pinctrl_mutex);
1182 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1189 mutex_unlock(&pinctrl_mutex);
1194 static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1196 struct pinctrl_dev *pctldev = s->private;
1197 struct pinctrl_gpio_range *range = NULL;
1199 seq_puts(s, "GPIO ranges handled:\n");
1201 mutex_lock(&pinctrl_mutex);
1203 /* Loop over the ranges */
1204 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1205 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1206 range->id, range->name,
1207 range->base, (range->base + range->npins - 1),
1209 (range->pin_base + range->npins - 1));
1212 mutex_unlock(&pinctrl_mutex);
1217 static int pinctrl_devices_show(struct seq_file *s, void *what)
1219 struct pinctrl_dev *pctldev;
1221 seq_puts(s, "name [pinmux] [pinconf]\n");
1223 mutex_lock(&pinctrl_mutex);
1225 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1226 seq_printf(s, "%s ", pctldev->desc->name);
1227 if (pctldev->desc->pmxops)
1228 seq_puts(s, "yes ");
1231 if (pctldev->desc->confops)
1238 mutex_unlock(&pinctrl_mutex);
1243 static inline const char *map_type(enum pinctrl_map_type type)
1245 static const char * const names[] = {
1253 if (type >= ARRAY_SIZE(names))
1259 static int pinctrl_maps_show(struct seq_file *s, void *what)
1261 struct pinctrl_maps *maps_node;
1263 struct pinctrl_map const *map;
1265 seq_puts(s, "Pinctrl maps:\n");
1267 mutex_lock(&pinctrl_mutex);
1269 for_each_maps(maps_node, i, map) {
1270 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1271 map->dev_name, map->name, map_type(map->type),
1274 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1275 seq_printf(s, "controlling device %s\n",
1276 map->ctrl_dev_name);
1278 switch (map->type) {
1279 case PIN_MAP_TYPE_MUX_GROUP:
1280 pinmux_show_map(s, map);
1282 case PIN_MAP_TYPE_CONFIGS_PIN:
1283 case PIN_MAP_TYPE_CONFIGS_GROUP:
1284 pinconf_show_map(s, map);
1290 seq_printf(s, "\n");
1293 mutex_unlock(&pinctrl_mutex);
1298 static int pinctrl_show(struct seq_file *s, void *what)
1301 struct pinctrl_state *state;
1302 struct pinctrl_setting *setting;
1304 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
1306 mutex_lock(&pinctrl_mutex);
1308 list_for_each_entry(p, &pinctrl_list, node) {
1309 seq_printf(s, "device: %s current state: %s\n",
1311 p->state ? p->state->name : "none");
1313 list_for_each_entry(state, &p->states, node) {
1314 seq_printf(s, " state: %s\n", state->name);
1316 list_for_each_entry(setting, &state->settings, node) {
1317 struct pinctrl_dev *pctldev = setting->pctldev;
1319 seq_printf(s, " type: %s controller %s ",
1320 map_type(setting->type),
1321 pinctrl_dev_get_name(pctldev));
1323 switch (setting->type) {
1324 case PIN_MAP_TYPE_MUX_GROUP:
1325 pinmux_show_setting(s, setting);
1327 case PIN_MAP_TYPE_CONFIGS_PIN:
1328 case PIN_MAP_TYPE_CONFIGS_GROUP:
1329 pinconf_show_setting(s, setting);
1338 mutex_unlock(&pinctrl_mutex);
1343 static int pinctrl_pins_open(struct inode *inode, struct file *file)
1345 return single_open(file, pinctrl_pins_show, inode->i_private);
1348 static int pinctrl_groups_open(struct inode *inode, struct file *file)
1350 return single_open(file, pinctrl_groups_show, inode->i_private);
1353 static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1355 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1358 static int pinctrl_devices_open(struct inode *inode, struct file *file)
1360 return single_open(file, pinctrl_devices_show, NULL);
1363 static int pinctrl_maps_open(struct inode *inode, struct file *file)
1365 return single_open(file, pinctrl_maps_show, NULL);
1368 static int pinctrl_open(struct inode *inode, struct file *file)
1370 return single_open(file, pinctrl_show, NULL);
1373 static const struct file_operations pinctrl_pins_ops = {
1374 .open = pinctrl_pins_open,
1376 .llseek = seq_lseek,
1377 .release = single_release,
1380 static const struct file_operations pinctrl_groups_ops = {
1381 .open = pinctrl_groups_open,
1383 .llseek = seq_lseek,
1384 .release = single_release,
1387 static const struct file_operations pinctrl_gpioranges_ops = {
1388 .open = pinctrl_gpioranges_open,
1390 .llseek = seq_lseek,
1391 .release = single_release,
1394 static const struct file_operations pinctrl_devices_ops = {
1395 .open = pinctrl_devices_open,
1397 .llseek = seq_lseek,
1398 .release = single_release,
1401 static const struct file_operations pinctrl_maps_ops = {
1402 .open = pinctrl_maps_open,
1404 .llseek = seq_lseek,
1405 .release = single_release,
1408 static const struct file_operations pinctrl_ops = {
1409 .open = pinctrl_open,
1411 .llseek = seq_lseek,
1412 .release = single_release,
1415 static struct dentry *debugfs_root;
1417 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1419 struct dentry *device_root;
1421 device_root = debugfs_create_dir(dev_name(pctldev->dev),
1423 pctldev->device_root = device_root;
1425 if (IS_ERR(device_root) || !device_root) {
1426 pr_warn("failed to create debugfs directory for %s\n",
1427 dev_name(pctldev->dev));
1430 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1431 device_root, pctldev, &pinctrl_pins_ops);
1432 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1433 device_root, pctldev, &pinctrl_groups_ops);
1434 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1435 device_root, pctldev, &pinctrl_gpioranges_ops);
1436 pinmux_init_device_debugfs(device_root, pctldev);
1437 pinconf_init_device_debugfs(device_root, pctldev);
1440 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1442 debugfs_remove_recursive(pctldev->device_root);
1445 static void pinctrl_init_debugfs(void)
1447 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1448 if (IS_ERR(debugfs_root) || !debugfs_root) {
1449 pr_warn("failed to create debugfs directory\n");
1450 debugfs_root = NULL;
1454 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1455 debugfs_root, NULL, &pinctrl_devices_ops);
1456 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1457 debugfs_root, NULL, &pinctrl_maps_ops);
1458 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1459 debugfs_root, NULL, &pinctrl_ops);
1462 #else /* CONFIG_DEBUG_FS */
1464 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1468 static void pinctrl_init_debugfs(void)
1472 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1478 static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1480 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1483 !ops->get_groups_count ||
1484 !ops->get_group_name ||
1485 !ops->get_group_pins)
1488 if (ops->dt_node_to_map && !ops->dt_free_map)
1495 * pinctrl_register() - register a pin controller device
1496 * @pctldesc: descriptor for this pin controller
1497 * @dev: parent device for this pin controller
1498 * @driver_data: private pin controller data for this pin controller
1500 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1501 struct device *dev, void *driver_data)
1503 struct pinctrl_dev *pctldev;
1508 if (!pctldesc->name)
1511 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
1512 if (pctldev == NULL) {
1513 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
1517 /* Initialize pin control device struct */
1518 pctldev->owner = pctldesc->owner;
1519 pctldev->desc = pctldesc;
1520 pctldev->driver_data = driver_data;
1521 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
1522 INIT_LIST_HEAD(&pctldev->gpio_ranges);
1525 /* check core ops for sanity */
1526 if (pinctrl_check_ops(pctldev)) {
1527 dev_err(dev, "pinctrl ops lacks necessary functions\n");
1531 /* If we're implementing pinmuxing, check the ops for sanity */
1532 if (pctldesc->pmxops) {
1533 if (pinmux_check_ops(pctldev))
1537 /* If we're implementing pinconfig, check the ops for sanity */
1538 if (pctldesc->confops) {
1539 if (pinconf_check_ops(pctldev))
1543 /* Register all the pins */
1544 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
1545 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1547 dev_err(dev, "error during pin registration\n");
1548 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1553 mutex_lock(&pinctrl_mutex);
1555 list_add_tail(&pctldev->node, &pinctrldev_list);
1557 pctldev->p = pinctrl_get_locked(pctldev->dev);
1558 if (!IS_ERR(pctldev->p)) {
1559 pctldev->hog_default =
1560 pinctrl_lookup_state_locked(pctldev->p,
1561 PINCTRL_STATE_DEFAULT);
1562 if (IS_ERR(pctldev->hog_default)) {
1563 dev_dbg(dev, "failed to lookup the default state\n");
1565 if (pinctrl_select_state_locked(pctldev->p,
1566 pctldev->hog_default))
1568 "failed to select default state\n");
1571 pctldev->hog_sleep =
1572 pinctrl_lookup_state_locked(pctldev->p,
1573 PINCTRL_STATE_SLEEP);
1574 if (IS_ERR(pctldev->hog_sleep))
1575 dev_dbg(dev, "failed to lookup the sleep state\n");
1578 mutex_unlock(&pinctrl_mutex);
1580 pinctrl_init_device_debugfs(pctldev);
1588 EXPORT_SYMBOL_GPL(pinctrl_register);
1591 * pinctrl_unregister() - unregister pinmux
1592 * @pctldev: pin controller to unregister
1594 * Called by pinmux drivers to unregister a pinmux.
1596 void pinctrl_unregister(struct pinctrl_dev *pctldev)
1598 struct pinctrl_gpio_range *range, *n;
1599 if (pctldev == NULL)
1602 pinctrl_remove_device_debugfs(pctldev);
1604 mutex_lock(&pinctrl_mutex);
1606 if (!IS_ERR(pctldev->p))
1607 pinctrl_put_locked(pctldev->p, true);
1609 /* TODO: check that no pinmuxes are still active? */
1610 list_del(&pctldev->node);
1611 /* Destroy descriptor tree */
1612 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1613 pctldev->desc->npins);
1614 /* remove gpio ranges map */
1615 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1616 list_del(&range->node);
1620 mutex_unlock(&pinctrl_mutex);
1622 EXPORT_SYMBOL_GPL(pinctrl_unregister);
1624 static int __init pinctrl_init(void)
1626 pr_info("initialized pinctrl subsystem\n");
1627 pinctrl_init_debugfs();
1631 /* init early since many drivers really need to initialized pinmux early */
1632 core_initcall(pinctrl_init);