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/export.h>
18 #include <linux/init.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/err.h>
22 #include <linux/list.h>
23 #include <linux/sysfs.h>
24 #include <linux/debugfs.h>
25 #include <linux/seq_file.h>
26 #include <linux/pinctrl/consumer.h>
27 #include <linux/pinctrl/pinctrl.h>
28 #include <linux/pinctrl/machine.h>
30 #include "devicetree.h"
35 * struct pinctrl_maps - a list item containing part of the mapping table
36 * @node: mapping table list node
37 * @maps: array of mapping table entries
38 * @num_maps: the number of entries in @maps
41 struct list_head node;
42 struct pinctrl_map const *maps;
46 static bool pinctrl_dummy_state;
48 /* Mutex taken by all entry points */
49 DEFINE_MUTEX(pinctrl_mutex);
51 /* Global list of pin control devices (struct pinctrl_dev) */
52 LIST_HEAD(pinctrldev_list);
54 /* List of pin controller handles (struct pinctrl) */
55 static LIST_HEAD(pinctrl_list);
57 /* List of pinctrl maps (struct pinctrl_maps) */
58 static LIST_HEAD(pinctrl_maps);
60 #define for_each_maps(_maps_node_, _i_, _map_) \
61 list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
62 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
63 _i_ < _maps_node_->num_maps; \
64 _i_++, _map_ = &_maps_node_->maps[_i_])
67 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
69 * Usually this function is called by platforms without pinctrl driver support
70 * but run with some shared drivers using pinctrl APIs.
71 * After calling this function, the pinctrl core will return successfully
72 * with creating a dummy state for the driver to keep going smoothly.
74 void pinctrl_provide_dummies(void)
76 pinctrl_dummy_state = true;
79 const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
81 /* We're not allowed to register devices without name */
82 return pctldev->desc->name;
84 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
86 void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
88 return pctldev->driver_data;
90 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
93 * get_pinctrl_dev_from_devname() - look up pin controller device
94 * @devname: the name of a device instance, as returned by dev_name()
96 * Looks up a pin control device matching a certain device name or pure device
97 * pointer, the pure device pointer will take precedence.
99 struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
101 struct pinctrl_dev *pctldev = NULL;
107 list_for_each_entry(pctldev, &pinctrldev_list, node) {
108 if (!strcmp(dev_name(pctldev->dev), devname)) {
109 /* Matched on device name */
115 return found ? pctldev : NULL;
119 * pin_get_from_name() - look up a pin number from a name
120 * @pctldev: the pin control device to lookup the pin on
121 * @name: the name of the pin to look up
123 int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
127 /* The pin number can be retrived from the pin controller descriptor */
128 for (i = 0; i < pctldev->desc->npins; i++) {
129 struct pin_desc *desc;
131 pin = pctldev->desc->pins[i].number;
132 desc = pin_desc_get(pctldev, pin);
133 /* Pin space may be sparse */
136 if (desc->name && !strcmp(name, desc->name))
144 * pin_get_name_from_id() - look up a pin name from a pin id
145 * @pctldev: the pin control device to lookup the pin on
146 * @name: the name of the pin to look up
148 const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
150 const struct pin_desc *desc;
152 desc = pin_desc_get(pctldev, pin);
154 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
163 * pin_is_valid() - check if pin exists on controller
164 * @pctldev: the pin control device to check the pin on
165 * @pin: pin to check, use the local pin controller index number
167 * This tells us whether a certain pin exist on a certain pin controller or
168 * not. Pin lists may be sparse, so some pins may not exist.
170 bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
172 struct pin_desc *pindesc;
177 mutex_lock(&pinctrl_mutex);
178 pindesc = pin_desc_get(pctldev, pin);
179 mutex_unlock(&pinctrl_mutex);
181 return pindesc != NULL;
183 EXPORT_SYMBOL_GPL(pin_is_valid);
185 /* Deletes a range of pin descriptors */
186 static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
187 const struct pinctrl_pin_desc *pins,
192 for (i = 0; i < num_pins; i++) {
193 struct pin_desc *pindesc;
195 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
197 if (pindesc != NULL) {
198 radix_tree_delete(&pctldev->pin_desc_tree,
200 if (pindesc->dynamic_name)
201 kfree(pindesc->name);
207 static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
208 unsigned number, const char *name)
210 struct pin_desc *pindesc;
212 pindesc = pin_desc_get(pctldev, number);
213 if (pindesc != NULL) {
214 pr_err("pin %d already registered on %s\n", number,
215 pctldev->desc->name);
219 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
220 if (pindesc == NULL) {
221 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
226 pindesc->pctldev = pctldev;
228 /* Copy basic pin info */
230 pindesc->name = name;
232 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
233 if (pindesc->name == NULL) {
237 pindesc->dynamic_name = true;
240 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
241 pr_debug("registered pin %d (%s) on %s\n",
242 number, pindesc->name, pctldev->desc->name);
246 static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
247 struct pinctrl_pin_desc const *pins,
253 for (i = 0; i < num_descs; i++) {
254 ret = pinctrl_register_one_pin(pctldev,
255 pins[i].number, pins[i].name);
264 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
265 * @pctldev: pin controller device to check
266 * @gpio: gpio pin to check taken from the global GPIO pin space
268 * Tries to match a GPIO pin number to the ranges handled by a certain pin
269 * controller, return the range or NULL
271 static struct pinctrl_gpio_range *
272 pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
274 struct pinctrl_gpio_range *range = NULL;
276 /* Loop over the ranges */
277 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
278 /* Check if we're in the valid range */
279 if (gpio >= range->base &&
280 gpio < range->base + range->npins) {
289 * pinctrl_get_device_gpio_range() - find device for GPIO range
290 * @gpio: the pin to locate the pin controller for
291 * @outdev: the pin control device if found
292 * @outrange: the GPIO range if found
294 * Find the pin controller handling a certain GPIO pin from the pinspace of
295 * the GPIO subsystem, return the device and the matching GPIO range. Returns
296 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
297 * may still have not been registered.
299 static int pinctrl_get_device_gpio_range(unsigned gpio,
300 struct pinctrl_dev **outdev,
301 struct pinctrl_gpio_range **outrange)
303 struct pinctrl_dev *pctldev = NULL;
305 /* Loop over the pin controllers */
306 list_for_each_entry(pctldev, &pinctrldev_list, node) {
307 struct pinctrl_gpio_range *range;
309 range = pinctrl_match_gpio_range(pctldev, gpio);
317 return -EPROBE_DEFER;
321 * pinctrl_add_gpio_range() - register a GPIO range for a controller
322 * @pctldev: pin controller device to add the range to
323 * @range: the GPIO range to add
325 * This adds a range of GPIOs to be handled by a certain pin controller. Call
326 * this to register handled ranges after registering your pin controller.
328 void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
329 struct pinctrl_gpio_range *range)
331 mutex_lock(&pinctrl_mutex);
332 list_add_tail(&range->node, &pctldev->gpio_ranges);
333 mutex_unlock(&pinctrl_mutex);
335 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
337 void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
338 struct pinctrl_gpio_range *ranges,
343 for (i = 0; i < nranges; i++)
344 pinctrl_add_gpio_range(pctldev, &ranges[i]);
346 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
349 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
350 * @pctldev: pin controller device to remove the range from
351 * @range: the GPIO range to remove
353 void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
354 struct pinctrl_gpio_range *range)
356 mutex_lock(&pinctrl_mutex);
357 list_del(&range->node);
358 mutex_unlock(&pinctrl_mutex);
360 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
363 * pinctrl_get_group_selector() - returns the group selector for a group
364 * @pctldev: the pin controller handling the group
365 * @pin_group: the pin group to look up
367 int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
368 const char *pin_group)
370 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
371 unsigned ngroups = pctlops->get_groups_count(pctldev);
372 unsigned group_selector = 0;
374 while (group_selector < ngroups) {
375 const char *gname = pctlops->get_group_name(pctldev,
377 if (!strcmp(gname, pin_group)) {
378 dev_dbg(pctldev->dev,
379 "found group selector %u for %s\n",
382 return group_selector;
388 dev_err(pctldev->dev, "does not have pin group %s\n",
395 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
396 * @gpio: the GPIO pin number from the GPIO subsystem number space
398 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
399 * as part of their gpio_request() semantics, platforms and individual drivers
400 * shall *NOT* request GPIO pins to be muxed in.
402 int pinctrl_request_gpio(unsigned gpio)
404 struct pinctrl_dev *pctldev;
405 struct pinctrl_gpio_range *range;
409 mutex_lock(&pinctrl_mutex);
411 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
413 mutex_unlock(&pinctrl_mutex);
417 /* Convert to the pin controllers number space */
418 pin = gpio - range->base + range->pin_base;
420 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
422 mutex_unlock(&pinctrl_mutex);
425 EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
428 * pinctrl_free_gpio() - free control on a single pin, currently used 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_free() semantics, platforms and individual drivers
433 * shall *NOT* request GPIO pins to be muxed out.
435 void pinctrl_free_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 pinmux_free_gpio(pctldev, pin, range);
455 mutex_unlock(&pinctrl_mutex);
457 EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
459 static int pinctrl_gpio_direction(unsigned gpio, bool input)
461 struct pinctrl_dev *pctldev;
462 struct pinctrl_gpio_range *range;
466 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
470 /* Convert to the pin controllers number space */
471 pin = gpio - range->base + range->pin_base;
473 return pinmux_gpio_direction(pctldev, range, pin, input);
477 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
478 * @gpio: the GPIO pin number from the GPIO subsystem number space
480 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
481 * as part of their gpio_direction_input() semantics, platforms and individual
482 * drivers shall *NOT* touch pin control GPIO calls.
484 int pinctrl_gpio_direction_input(unsigned gpio)
487 mutex_lock(&pinctrl_mutex);
488 ret = pinctrl_gpio_direction(gpio, true);
489 mutex_unlock(&pinctrl_mutex);
492 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
495 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
496 * @gpio: the GPIO pin number from the GPIO subsystem number space
498 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
499 * as part of their gpio_direction_output() semantics, platforms and individual
500 * drivers shall *NOT* touch pin control GPIO calls.
502 int pinctrl_gpio_direction_output(unsigned gpio)
505 mutex_lock(&pinctrl_mutex);
506 ret = pinctrl_gpio_direction(gpio, false);
507 mutex_unlock(&pinctrl_mutex);
510 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
512 static struct pinctrl_state *find_state(struct pinctrl *p,
515 struct pinctrl_state *state;
517 list_for_each_entry(state, &p->states, node)
518 if (!strcmp(state->name, name))
524 static struct pinctrl_state *create_state(struct pinctrl *p,
527 struct pinctrl_state *state;
529 state = kzalloc(sizeof(*state), GFP_KERNEL);
532 "failed to alloc struct pinctrl_state\n");
533 return ERR_PTR(-ENOMEM);
537 INIT_LIST_HEAD(&state->settings);
539 list_add_tail(&state->node, &p->states);
544 static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
546 struct pinctrl_state *state;
547 struct pinctrl_setting *setting;
550 state = find_state(p, map->name);
552 state = create_state(p, map->name);
554 return PTR_ERR(state);
556 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
559 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
560 if (setting == NULL) {
562 "failed to alloc struct pinctrl_setting\n");
566 setting->type = map->type;
568 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
569 if (setting->pctldev == NULL) {
570 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
574 * OK let us guess that the driver is not there yet, and
575 * let's defer obtaining this pinctrl handle to later...
577 return -EPROBE_DEFER;
580 setting->dev_name = map->dev_name;
583 case PIN_MAP_TYPE_MUX_GROUP:
584 ret = pinmux_map_to_setting(map, setting);
586 case PIN_MAP_TYPE_CONFIGS_PIN:
587 case PIN_MAP_TYPE_CONFIGS_GROUP:
588 ret = pinconf_map_to_setting(map, setting);
599 list_add_tail(&setting->node, &state->settings);
604 static struct pinctrl *find_pinctrl(struct device *dev)
608 list_for_each_entry(p, &pinctrl_list, node)
615 static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
617 static struct pinctrl *create_pinctrl(struct device *dev)
621 struct pinctrl_maps *maps_node;
623 struct pinctrl_map const *map;
627 * create the state cookie holder struct pinctrl for each
628 * mapping, this is what consumers will get when requesting
629 * a pin control handle with pinctrl_get()
631 p = kzalloc(sizeof(*p), GFP_KERNEL);
633 dev_err(dev, "failed to alloc struct pinctrl\n");
634 return ERR_PTR(-ENOMEM);
637 INIT_LIST_HEAD(&p->states);
638 INIT_LIST_HEAD(&p->dt_maps);
640 ret = pinctrl_dt_to_map(p);
646 devname = dev_name(dev);
648 /* Iterate over the pin control maps to locate the right ones */
649 for_each_maps(maps_node, i, map) {
650 /* Map must be for this device */
651 if (strcmp(map->dev_name, devname))
654 ret = add_setting(p, map);
656 pinctrl_put_locked(p, false);
661 /* Add the pinmux to the global list */
662 list_add_tail(&p->node, &pinctrl_list);
667 static struct pinctrl *pinctrl_get_locked(struct device *dev)
672 return ERR_PTR(-EINVAL);
674 p = find_pinctrl(dev);
676 return ERR_PTR(-EBUSY);
678 return create_pinctrl(dev);
682 * pinctrl_get() - retrieves the pinctrl handle for a device
683 * @dev: the device to obtain the handle for
685 struct pinctrl *pinctrl_get(struct device *dev)
689 mutex_lock(&pinctrl_mutex);
690 p = pinctrl_get_locked(dev);
691 mutex_unlock(&pinctrl_mutex);
695 EXPORT_SYMBOL_GPL(pinctrl_get);
697 static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
699 struct pinctrl_state *state, *n1;
700 struct pinctrl_setting *setting, *n2;
702 list_for_each_entry_safe(state, n1, &p->states, node) {
703 list_for_each_entry_safe(setting, n2, &state->settings, node) {
704 switch (setting->type) {
705 case PIN_MAP_TYPE_MUX_GROUP:
706 if (state == p->state)
707 pinmux_disable_setting(setting);
708 pinmux_free_setting(setting);
710 case PIN_MAP_TYPE_CONFIGS_PIN:
711 case PIN_MAP_TYPE_CONFIGS_GROUP:
712 pinconf_free_setting(setting);
717 list_del(&setting->node);
720 list_del(&state->node);
724 pinctrl_dt_free_maps(p);
732 * pinctrl_put() - release a previously claimed pinctrl handle
733 * @p: the pinctrl handle to release
735 void pinctrl_put(struct pinctrl *p)
737 mutex_lock(&pinctrl_mutex);
738 pinctrl_put_locked(p, true);
739 mutex_unlock(&pinctrl_mutex);
741 EXPORT_SYMBOL_GPL(pinctrl_put);
743 static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
746 struct pinctrl_state *state;
748 state = find_state(p, name);
750 if (pinctrl_dummy_state) {
751 /* create dummy state */
752 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
754 state = create_state(p, name);
756 state = ERR_PTR(-ENODEV);
763 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
764 * @p: the pinctrl handle to retrieve the state from
765 * @name: the state name to retrieve
767 struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
769 struct pinctrl_state *s;
771 mutex_lock(&pinctrl_mutex);
772 s = pinctrl_lookup_state_locked(p, name);
773 mutex_unlock(&pinctrl_mutex);
777 EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
779 static int pinctrl_select_state_locked(struct pinctrl *p,
780 struct pinctrl_state *state)
782 struct pinctrl_setting *setting, *setting2;
785 if (p->state == state)
790 * The set of groups with a mux configuration in the old state
791 * may not be identical to the set of groups with a mux setting
792 * in the new state. While this might be unusual, it's entirely
793 * possible for the "user"-supplied mapping table to be written
794 * that way. For each group that was configured in the old state
795 * but not in the new state, this code puts that group into a
796 * safe/disabled state.
798 list_for_each_entry(setting, &p->state->settings, node) {
800 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
802 list_for_each_entry(setting2, &state->settings, node) {
803 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
805 if (setting2->data.mux.group ==
806 setting->data.mux.group) {
812 pinmux_disable_setting(setting);
818 /* Apply all the settings for the new state */
819 list_for_each_entry(setting, &state->settings, node) {
820 switch (setting->type) {
821 case PIN_MAP_TYPE_MUX_GROUP:
822 ret = pinmux_enable_setting(setting);
824 case PIN_MAP_TYPE_CONFIGS_PIN:
825 case PIN_MAP_TYPE_CONFIGS_GROUP:
826 ret = pinconf_apply_setting(setting);
833 /* FIXME: Difficult to return to prev state */
842 * pinctrl_select() - select/activate/program a pinctrl state to HW
843 * @p: the pinctrl handle for the device that requests configuratio
844 * @state: the state handle to select/activate/program
846 int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
850 mutex_lock(&pinctrl_mutex);
851 ret = pinctrl_select_state_locked(p, state);
852 mutex_unlock(&pinctrl_mutex);
856 EXPORT_SYMBOL_GPL(pinctrl_select_state);
858 static void devm_pinctrl_release(struct device *dev, void *res)
860 pinctrl_put(*(struct pinctrl **)res);
864 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
865 * @dev: the device to obtain the handle for
867 * If there is a need to explicitly destroy the returned struct pinctrl,
868 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
870 struct pinctrl *devm_pinctrl_get(struct device *dev)
872 struct pinctrl **ptr, *p;
874 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
876 return ERR_PTR(-ENOMEM);
878 p = pinctrl_get(dev);
881 devres_add(dev, ptr);
888 EXPORT_SYMBOL_GPL(devm_pinctrl_get);
890 static int devm_pinctrl_match(struct device *dev, void *res, void *data)
892 struct pinctrl **p = res;
898 * devm_pinctrl_put() - Resource managed pinctrl_put()
899 * @p: the pinctrl handle to release
901 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
902 * this function will not need to be called and the resource management
903 * code will ensure that the resource is freed.
905 void devm_pinctrl_put(struct pinctrl *p)
907 WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
908 devm_pinctrl_match, p));
911 EXPORT_SYMBOL_GPL(devm_pinctrl_put);
913 int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
914 bool dup, bool locked)
917 struct pinctrl_maps *maps_node;
919 pr_debug("add %d pinmux maps\n", num_maps);
921 /* First sanity check the new mapping */
922 for (i = 0; i < num_maps; i++) {
923 if (!maps[i].dev_name) {
924 pr_err("failed to register map %s (%d): no device given\n",
930 pr_err("failed to register map %d: no map name given\n",
935 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
936 !maps[i].ctrl_dev_name) {
937 pr_err("failed to register map %s (%d): no pin control device given\n",
942 switch (maps[i].type) {
943 case PIN_MAP_TYPE_DUMMY_STATE:
945 case PIN_MAP_TYPE_MUX_GROUP:
946 ret = pinmux_validate_map(&maps[i], i);
950 case PIN_MAP_TYPE_CONFIGS_PIN:
951 case PIN_MAP_TYPE_CONFIGS_GROUP:
952 ret = pinconf_validate_map(&maps[i], i);
957 pr_err("failed to register map %s (%d): invalid type given\n",
963 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
965 pr_err("failed to alloc struct pinctrl_maps\n");
969 maps_node->num_maps = num_maps;
971 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
973 if (!maps_node->maps) {
974 pr_err("failed to duplicate mapping table\n");
979 maps_node->maps = maps;
983 mutex_lock(&pinctrl_mutex);
984 list_add_tail(&maps_node->node, &pinctrl_maps);
986 mutex_unlock(&pinctrl_mutex);
992 * pinctrl_register_mappings() - register a set of pin controller mappings
993 * @maps: the pincontrol mappings table to register. This should probably be
994 * marked with __initdata so it can be discarded after boot. This
995 * function will perform a shallow copy for the mapping entries.
996 * @num_maps: the number of maps in the mapping table
998 int pinctrl_register_mappings(struct pinctrl_map const *maps,
1001 return pinctrl_register_map(maps, num_maps, true, false);
1004 void pinctrl_unregister_map(struct pinctrl_map const *map)
1006 struct pinctrl_maps *maps_node;
1008 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1009 if (maps_node->maps == map) {
1010 list_del(&maps_node->node);
1016 #ifdef CONFIG_DEBUG_FS
1018 static int pinctrl_pins_show(struct seq_file *s, void *what)
1020 struct pinctrl_dev *pctldev = s->private;
1021 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1024 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
1026 mutex_lock(&pinctrl_mutex);
1028 /* The pin number can be retrived from the pin controller descriptor */
1029 for (i = 0; i < pctldev->desc->npins; i++) {
1030 struct pin_desc *desc;
1032 pin = pctldev->desc->pins[i].number;
1033 desc = pin_desc_get(pctldev, pin);
1034 /* Pin space may be sparse */
1038 seq_printf(s, "pin %d (%s) ", pin,
1039 desc->name ? desc->name : "unnamed");
1041 /* Driver-specific info per pin */
1042 if (ops->pin_dbg_show)
1043 ops->pin_dbg_show(pctldev, s, pin);
1048 mutex_unlock(&pinctrl_mutex);
1053 static int pinctrl_groups_show(struct seq_file *s, void *what)
1055 struct pinctrl_dev *pctldev = s->private;
1056 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1057 unsigned ngroups, selector = 0;
1059 ngroups = ops->get_groups_count(pctldev);
1060 mutex_lock(&pinctrl_mutex);
1062 seq_puts(s, "registered pin groups:\n");
1063 while (selector < ngroups) {
1064 const unsigned *pins;
1066 const char *gname = ops->get_group_name(pctldev, selector);
1071 ret = ops->get_group_pins(pctldev, selector,
1074 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1077 seq_printf(s, "group: %s\n", gname);
1078 for (i = 0; i < num_pins; i++) {
1079 pname = pin_get_name(pctldev, pins[i]);
1080 if (WARN_ON(!pname)) {
1081 mutex_unlock(&pinctrl_mutex);
1084 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1091 mutex_unlock(&pinctrl_mutex);
1096 static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1098 struct pinctrl_dev *pctldev = s->private;
1099 struct pinctrl_gpio_range *range = NULL;
1101 seq_puts(s, "GPIO ranges handled:\n");
1103 mutex_lock(&pinctrl_mutex);
1105 /* Loop over the ranges */
1106 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1107 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1108 range->id, range->name,
1109 range->base, (range->base + range->npins - 1),
1111 (range->pin_base + range->npins - 1));
1114 mutex_unlock(&pinctrl_mutex);
1119 static int pinctrl_devices_show(struct seq_file *s, void *what)
1121 struct pinctrl_dev *pctldev;
1123 seq_puts(s, "name [pinmux] [pinconf]\n");
1125 mutex_lock(&pinctrl_mutex);
1127 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1128 seq_printf(s, "%s ", pctldev->desc->name);
1129 if (pctldev->desc->pmxops)
1130 seq_puts(s, "yes ");
1133 if (pctldev->desc->confops)
1140 mutex_unlock(&pinctrl_mutex);
1145 static inline const char *map_type(enum pinctrl_map_type type)
1147 static const char * const names[] = {
1155 if (type >= ARRAY_SIZE(names))
1161 static int pinctrl_maps_show(struct seq_file *s, void *what)
1163 struct pinctrl_maps *maps_node;
1165 struct pinctrl_map const *map;
1167 seq_puts(s, "Pinctrl maps:\n");
1169 mutex_lock(&pinctrl_mutex);
1171 for_each_maps(maps_node, i, map) {
1172 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1173 map->dev_name, map->name, map_type(map->type),
1176 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1177 seq_printf(s, "controlling device %s\n",
1178 map->ctrl_dev_name);
1180 switch (map->type) {
1181 case PIN_MAP_TYPE_MUX_GROUP:
1182 pinmux_show_map(s, map);
1184 case PIN_MAP_TYPE_CONFIGS_PIN:
1185 case PIN_MAP_TYPE_CONFIGS_GROUP:
1186 pinconf_show_map(s, map);
1192 seq_printf(s, "\n");
1195 mutex_unlock(&pinctrl_mutex);
1200 static int pinctrl_show(struct seq_file *s, void *what)
1203 struct pinctrl_state *state;
1204 struct pinctrl_setting *setting;
1206 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
1208 mutex_lock(&pinctrl_mutex);
1210 list_for_each_entry(p, &pinctrl_list, node) {
1211 seq_printf(s, "device: %s current state: %s\n",
1213 p->state ? p->state->name : "none");
1215 list_for_each_entry(state, &p->states, node) {
1216 seq_printf(s, " state: %s\n", state->name);
1218 list_for_each_entry(setting, &state->settings, node) {
1219 struct pinctrl_dev *pctldev = setting->pctldev;
1221 seq_printf(s, " type: %s controller %s ",
1222 map_type(setting->type),
1223 pinctrl_dev_get_name(pctldev));
1225 switch (setting->type) {
1226 case PIN_MAP_TYPE_MUX_GROUP:
1227 pinmux_show_setting(s, setting);
1229 case PIN_MAP_TYPE_CONFIGS_PIN:
1230 case PIN_MAP_TYPE_CONFIGS_GROUP:
1231 pinconf_show_setting(s, setting);
1240 mutex_unlock(&pinctrl_mutex);
1245 static int pinctrl_pins_open(struct inode *inode, struct file *file)
1247 return single_open(file, pinctrl_pins_show, inode->i_private);
1250 static int pinctrl_groups_open(struct inode *inode, struct file *file)
1252 return single_open(file, pinctrl_groups_show, inode->i_private);
1255 static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1257 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1260 static int pinctrl_devices_open(struct inode *inode, struct file *file)
1262 return single_open(file, pinctrl_devices_show, NULL);
1265 static int pinctrl_maps_open(struct inode *inode, struct file *file)
1267 return single_open(file, pinctrl_maps_show, NULL);
1270 static int pinctrl_open(struct inode *inode, struct file *file)
1272 return single_open(file, pinctrl_show, NULL);
1275 static const struct file_operations pinctrl_pins_ops = {
1276 .open = pinctrl_pins_open,
1278 .llseek = seq_lseek,
1279 .release = single_release,
1282 static const struct file_operations pinctrl_groups_ops = {
1283 .open = pinctrl_groups_open,
1285 .llseek = seq_lseek,
1286 .release = single_release,
1289 static const struct file_operations pinctrl_gpioranges_ops = {
1290 .open = pinctrl_gpioranges_open,
1292 .llseek = seq_lseek,
1293 .release = single_release,
1296 static const struct file_operations pinctrl_devices_ops = {
1297 .open = pinctrl_devices_open,
1299 .llseek = seq_lseek,
1300 .release = single_release,
1303 static const struct file_operations pinctrl_maps_ops = {
1304 .open = pinctrl_maps_open,
1306 .llseek = seq_lseek,
1307 .release = single_release,
1310 static const struct file_operations pinctrl_ops = {
1311 .open = pinctrl_open,
1313 .llseek = seq_lseek,
1314 .release = single_release,
1317 static struct dentry *debugfs_root;
1319 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1321 struct dentry *device_root;
1323 device_root = debugfs_create_dir(dev_name(pctldev->dev),
1325 pctldev->device_root = device_root;
1327 if (IS_ERR(device_root) || !device_root) {
1328 pr_warn("failed to create debugfs directory for %s\n",
1329 dev_name(pctldev->dev));
1332 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1333 device_root, pctldev, &pinctrl_pins_ops);
1334 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1335 device_root, pctldev, &pinctrl_groups_ops);
1336 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1337 device_root, pctldev, &pinctrl_gpioranges_ops);
1338 pinmux_init_device_debugfs(device_root, pctldev);
1339 pinconf_init_device_debugfs(device_root, pctldev);
1342 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1344 debugfs_remove_recursive(pctldev->device_root);
1347 static void pinctrl_init_debugfs(void)
1349 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1350 if (IS_ERR(debugfs_root) || !debugfs_root) {
1351 pr_warn("failed to create debugfs directory\n");
1352 debugfs_root = NULL;
1356 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1357 debugfs_root, NULL, &pinctrl_devices_ops);
1358 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1359 debugfs_root, NULL, &pinctrl_maps_ops);
1360 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1361 debugfs_root, NULL, &pinctrl_ops);
1364 #else /* CONFIG_DEBUG_FS */
1366 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1370 static void pinctrl_init_debugfs(void)
1374 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1380 static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1382 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1385 !ops->get_groups_count ||
1386 !ops->get_group_name ||
1387 !ops->get_group_pins)
1390 if (ops->dt_node_to_map && !ops->dt_free_map)
1397 * pinctrl_register() - register a pin controller device
1398 * @pctldesc: descriptor for this pin controller
1399 * @dev: parent device for this pin controller
1400 * @driver_data: private pin controller data for this pin controller
1402 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1403 struct device *dev, void *driver_data)
1405 struct pinctrl_dev *pctldev;
1410 if (!pctldesc->name)
1413 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
1414 if (pctldev == NULL) {
1415 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
1419 /* Initialize pin control device struct */
1420 pctldev->owner = pctldesc->owner;
1421 pctldev->desc = pctldesc;
1422 pctldev->driver_data = driver_data;
1423 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
1424 INIT_LIST_HEAD(&pctldev->gpio_ranges);
1427 /* check core ops for sanity */
1428 if (pinctrl_check_ops(pctldev)) {
1429 dev_err(dev, "pinctrl ops lacks necessary functions\n");
1433 /* If we're implementing pinmuxing, check the ops for sanity */
1434 if (pctldesc->pmxops) {
1435 if (pinmux_check_ops(pctldev))
1439 /* If we're implementing pinconfig, check the ops for sanity */
1440 if (pctldesc->confops) {
1441 if (pinconf_check_ops(pctldev))
1445 /* Register all the pins */
1446 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
1447 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1449 dev_err(dev, "error during pin registration\n");
1450 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1455 mutex_lock(&pinctrl_mutex);
1457 list_add_tail(&pctldev->node, &pinctrldev_list);
1459 pctldev->p = pinctrl_get_locked(pctldev->dev);
1460 if (!IS_ERR(pctldev->p)) {
1461 struct pinctrl_state *s =
1462 pinctrl_lookup_state_locked(pctldev->p,
1463 PINCTRL_STATE_DEFAULT);
1465 dev_dbg(dev, "failed to lookup the default state\n");
1467 if (pinctrl_select_state_locked(pctldev->p, s))
1469 "failed to select default state\n");
1473 mutex_unlock(&pinctrl_mutex);
1475 pinctrl_init_device_debugfs(pctldev);
1483 EXPORT_SYMBOL_GPL(pinctrl_register);
1486 * pinctrl_unregister() - unregister pinmux
1487 * @pctldev: pin controller to unregister
1489 * Called by pinmux drivers to unregister a pinmux.
1491 void pinctrl_unregister(struct pinctrl_dev *pctldev)
1493 struct pinctrl_gpio_range *range, *n;
1494 if (pctldev == NULL)
1497 pinctrl_remove_device_debugfs(pctldev);
1499 mutex_lock(&pinctrl_mutex);
1501 if (!IS_ERR(pctldev->p))
1502 pinctrl_put_locked(pctldev->p, true);
1504 /* TODO: check that no pinmuxes are still active? */
1505 list_del(&pctldev->node);
1506 /* Destroy descriptor tree */
1507 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1508 pctldev->desc->npins);
1509 /* remove gpio ranges map */
1510 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1511 list_del(&range->node);
1515 mutex_unlock(&pinctrl_mutex);
1517 EXPORT_SYMBOL_GPL(pinctrl_unregister);
1519 static int __init pinctrl_init(void)
1521 pr_info("initialized pinctrl subsystem\n");
1522 pinctrl_init_debugfs();
1526 /* init early since many drivers really need to initialized pinmux early */
1527 core_initcall(pinctrl_init);