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>
32 #include <asm-generic/gpio.h>
36 #include "devicetree.h"
41 static bool pinctrl_dummy_state;
43 /* Mutex taken to protect pinctrl_list */
44 DEFINE_MUTEX(pinctrl_list_mutex);
46 /* Mutex taken to protect pinctrl_maps */
47 DEFINE_MUTEX(pinctrl_maps_mutex);
49 /* Mutex taken to protect pinctrldev_list */
50 DEFINE_MUTEX(pinctrldev_list_mutex);
52 /* Global list of pin control devices (struct pinctrl_dev) */
53 static LIST_HEAD(pinctrldev_list);
55 /* List of pin controller handles (struct pinctrl) */
56 static LIST_HEAD(pinctrl_list);
58 /* List of pinctrl maps (struct pinctrl_maps) */
59 LIST_HEAD(pinctrl_maps);
63 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
65 * Usually this function is called by platforms without pinctrl driver support
66 * but run with some shared drivers using pinctrl APIs.
67 * After calling this function, the pinctrl core will return successfully
68 * with creating a dummy state for the driver to keep going smoothly.
70 void pinctrl_provide_dummies(void)
72 pinctrl_dummy_state = true;
75 const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
77 /* We're not allowed to register devices without name */
78 return pctldev->desc->name;
80 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
82 const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
84 return dev_name(pctldev->dev);
86 EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
88 void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
90 return pctldev->driver_data;
92 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
95 * get_pinctrl_dev_from_devname() - look up pin controller device
96 * @devname: the name of a device instance, as returned by dev_name()
98 * Looks up a pin control device matching a certain device name or pure device
99 * pointer, the pure device pointer will take precedence.
101 struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
103 struct pinctrl_dev *pctldev = NULL;
109 list_for_each_entry(pctldev, &pinctrldev_list, node) {
110 if (!strcmp(dev_name(pctldev->dev), devname)) {
111 /* Matched on device name */
117 return found ? pctldev : NULL;
120 struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
122 struct pinctrl_dev *pctldev;
124 mutex_lock(&pinctrldev_list_mutex);
126 list_for_each_entry(pctldev, &pinctrldev_list, node)
127 if (pctldev->dev->of_node == np) {
128 mutex_unlock(&pinctrldev_list_mutex);
132 mutex_unlock(&pinctrldev_list_mutex);
138 * pin_get_from_name() - look up a pin number from a name
139 * @pctldev: the pin control device to lookup the pin on
140 * @name: the name of the pin to look up
142 int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
146 /* The pin number can be retrived from the pin controller descriptor */
147 for (i = 0; i < pctldev->desc->npins; i++) {
148 struct pin_desc *desc;
150 pin = pctldev->desc->pins[i].number;
151 desc = pin_desc_get(pctldev, pin);
152 /* Pin space may be sparse */
155 if (desc->name && !strcmp(name, desc->name))
163 * pin_get_name_from_id() - look up a pin name from a pin id
164 * @pctldev: the pin control device to lookup the pin on
165 * @name: the name of the pin to look up
167 const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
169 const struct pin_desc *desc;
171 desc = pin_desc_get(pctldev, pin);
173 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
182 * pin_is_valid() - check if pin exists on controller
183 * @pctldev: the pin control device to check the pin on
184 * @pin: pin to check, use the local pin controller index number
186 * This tells us whether a certain pin exist on a certain pin controller or
187 * not. Pin lists may be sparse, so some pins may not exist.
189 bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
191 struct pin_desc *pindesc;
196 mutex_lock(&pctldev->mutex);
197 pindesc = pin_desc_get(pctldev, pin);
198 mutex_unlock(&pctldev->mutex);
200 return pindesc != NULL;
202 EXPORT_SYMBOL_GPL(pin_is_valid);
204 /* Deletes a range of pin descriptors */
205 static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
206 const struct pinctrl_pin_desc *pins,
211 for (i = 0; i < num_pins; i++) {
212 struct pin_desc *pindesc;
214 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
216 if (pindesc != NULL) {
217 radix_tree_delete(&pctldev->pin_desc_tree,
219 if (pindesc->dynamic_name)
220 kfree(pindesc->name);
226 static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
227 unsigned number, const char *name)
229 struct pin_desc *pindesc;
231 pindesc = pin_desc_get(pctldev, number);
232 if (pindesc != NULL) {
233 pr_err("pin %d already registered on %s\n", number,
234 pctldev->desc->name);
238 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
239 if (pindesc == NULL) {
240 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
245 pindesc->pctldev = pctldev;
247 /* Copy basic pin info */
249 pindesc->name = name;
251 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
252 if (pindesc->name == NULL) {
256 pindesc->dynamic_name = true;
259 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
260 pr_debug("registered pin %d (%s) on %s\n",
261 number, pindesc->name, pctldev->desc->name);
265 static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
266 struct pinctrl_pin_desc const *pins,
272 for (i = 0; i < num_descs; i++) {
273 ret = pinctrl_register_one_pin(pctldev,
274 pins[i].number, pins[i].name);
283 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
284 * @pctldev: pin controller device to check
285 * @gpio: gpio pin to check taken from the global GPIO pin space
287 * Tries to match a GPIO pin number to the ranges handled by a certain pin
288 * controller, return the range or NULL
290 static struct pinctrl_gpio_range *
291 pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
293 struct pinctrl_gpio_range *range = NULL;
295 mutex_lock(&pctldev->mutex);
296 /* Loop over the ranges */
297 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
298 /* Check if we're in the valid range */
299 if (gpio >= range->base &&
300 gpio < range->base + range->npins) {
301 mutex_unlock(&pctldev->mutex);
305 mutex_unlock(&pctldev->mutex);
310 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
311 * the same GPIO chip are in range
312 * @gpio: gpio pin to check taken from the global GPIO pin space
314 * This function is complement of pinctrl_match_gpio_range(). If the return
315 * value of pinctrl_match_gpio_range() is NULL, this function could be used
316 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
317 * of the same GPIO chip don't have back-end pinctrl interface.
318 * If the return value is true, it means that pinctrl device is ready & the
319 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
320 * is false, it means that pinctrl device may not be ready.
322 #ifdef CONFIG_GPIOLIB
323 static bool pinctrl_ready_for_gpio_range(unsigned gpio)
325 struct pinctrl_dev *pctldev;
326 struct pinctrl_gpio_range *range = NULL;
327 struct gpio_chip *chip = gpio_to_chip(gpio);
329 /* Loop over the pin controllers */
330 list_for_each_entry(pctldev, &pinctrldev_list, node) {
331 /* Loop over the ranges */
332 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
333 /* Check if any gpio range overlapped with gpio chip */
334 if (range->base + range->npins - 1 < chip->base ||
335 range->base > chip->base + chip->ngpio - 1)
343 static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
347 * pinctrl_get_device_gpio_range() - find device for GPIO range
348 * @gpio: the pin to locate the pin controller for
349 * @outdev: the pin control device if found
350 * @outrange: the GPIO range if found
352 * Find the pin controller handling a certain GPIO pin from the pinspace of
353 * the GPIO subsystem, return the device and the matching GPIO range. Returns
354 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
355 * may still have not been registered.
357 static int pinctrl_get_device_gpio_range(unsigned gpio,
358 struct pinctrl_dev **outdev,
359 struct pinctrl_gpio_range **outrange)
361 struct pinctrl_dev *pctldev = NULL;
363 /* Loop over the pin controllers */
364 list_for_each_entry(pctldev, &pinctrldev_list, node) {
365 struct pinctrl_gpio_range *range;
367 range = pinctrl_match_gpio_range(pctldev, gpio);
375 return -EPROBE_DEFER;
379 * pinctrl_add_gpio_range() - register a GPIO range for a controller
380 * @pctldev: pin controller device to add the range to
381 * @range: the GPIO range to add
383 * This adds a range of GPIOs to be handled by a certain pin controller. Call
384 * this to register handled ranges after registering your pin controller.
386 void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
387 struct pinctrl_gpio_range *range)
389 mutex_lock(&pctldev->mutex);
390 list_add_tail(&range->node, &pctldev->gpio_ranges);
391 mutex_unlock(&pctldev->mutex);
393 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
395 void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
396 struct pinctrl_gpio_range *ranges,
401 for (i = 0; i < nranges; i++)
402 pinctrl_add_gpio_range(pctldev, &ranges[i]);
404 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
406 struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
407 struct pinctrl_gpio_range *range)
409 struct pinctrl_dev *pctldev;
411 mutex_lock(&pinctrldev_list_mutex);
413 pctldev = get_pinctrl_dev_from_devname(devname);
416 * If we can't find this device, let's assume that is because
417 * it has not probed yet, so the driver trying to register this
418 * range need to defer probing.
421 mutex_unlock(&pinctrldev_list_mutex);
422 return ERR_PTR(-EPROBE_DEFER);
424 pinctrl_add_gpio_range(pctldev, range);
426 mutex_unlock(&pinctrldev_list_mutex);
430 EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
433 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
434 * @pctldev: the pin controller device to look in
435 * @pin: a controller-local number to find the range for
437 struct pinctrl_gpio_range *
438 pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
441 struct pinctrl_gpio_range *range = NULL;
443 mutex_lock(&pctldev->mutex);
444 /* Loop over the ranges */
445 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
446 /* Check if we're in the valid range */
447 if (pin >= range->pin_base &&
448 pin < range->pin_base + range->npins) {
449 mutex_unlock(&pctldev->mutex);
453 mutex_unlock(&pctldev->mutex);
457 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
460 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
461 * @pctldev: pin controller device to remove the range from
462 * @range: the GPIO range to remove
464 void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
465 struct pinctrl_gpio_range *range)
467 mutex_lock(&pctldev->mutex);
468 list_del(&range->node);
469 mutex_unlock(&pctldev->mutex);
471 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
474 * pinctrl_get_group_selector() - returns the group selector for a group
475 * @pctldev: the pin controller handling the group
476 * @pin_group: the pin group to look up
478 int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
479 const char *pin_group)
481 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
482 unsigned ngroups = pctlops->get_groups_count(pctldev);
483 unsigned group_selector = 0;
485 while (group_selector < ngroups) {
486 const char *gname = pctlops->get_group_name(pctldev,
488 if (!strcmp(gname, pin_group)) {
489 dev_dbg(pctldev->dev,
490 "found group selector %u for %s\n",
493 return group_selector;
499 dev_err(pctldev->dev, "does not have pin group %s\n",
506 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
507 * @gpio: the GPIO pin number from the GPIO subsystem number space
509 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
510 * as part of their gpio_request() semantics, platforms and individual drivers
511 * shall *NOT* request GPIO pins to be muxed in.
513 int pinctrl_request_gpio(unsigned gpio)
515 struct pinctrl_dev *pctldev;
516 struct pinctrl_gpio_range *range;
520 mutex_lock(&pinctrldev_list_mutex);
522 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
524 if (pinctrl_ready_for_gpio_range(gpio))
526 mutex_unlock(&pinctrldev_list_mutex);
530 /* Convert to the pin controllers number space */
531 pin = gpio - range->base + range->pin_base;
533 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
535 mutex_unlock(&pinctrldev_list_mutex);
538 EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
541 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
542 * @gpio: the GPIO pin number from the GPIO subsystem number space
544 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
545 * as part of their gpio_free() semantics, platforms and individual drivers
546 * shall *NOT* request GPIO pins to be muxed out.
548 void pinctrl_free_gpio(unsigned gpio)
550 struct pinctrl_dev *pctldev;
551 struct pinctrl_gpio_range *range;
555 mutex_lock(&pinctrldev_list_mutex);
557 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
559 mutex_unlock(&pinctrldev_list_mutex);
562 mutex_lock(&pctldev->mutex);
564 /* Convert to the pin controllers number space */
565 pin = gpio - range->base + range->pin_base;
567 pinmux_free_gpio(pctldev, pin, range);
569 mutex_unlock(&pctldev->mutex);
570 mutex_unlock(&pinctrldev_list_mutex);
572 EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
574 static int pinctrl_gpio_direction(unsigned gpio, bool input)
576 struct pinctrl_dev *pctldev;
577 struct pinctrl_gpio_range *range;
581 mutex_lock(&pinctrldev_list_mutex);
583 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
585 mutex_unlock(&pinctrldev_list_mutex);
589 mutex_lock(&pctldev->mutex);
591 /* Convert to the pin controllers number space */
592 pin = gpio - range->base + range->pin_base;
593 ret = pinmux_gpio_direction(pctldev, range, pin, input);
595 mutex_unlock(&pctldev->mutex);
596 mutex_unlock(&pinctrldev_list_mutex);
602 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
603 * @gpio: the GPIO pin number from the GPIO subsystem number space
605 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
606 * as part of their gpio_direction_input() semantics, platforms and individual
607 * drivers shall *NOT* touch pin control GPIO calls.
609 int pinctrl_gpio_direction_input(unsigned gpio)
611 return pinctrl_gpio_direction(gpio, true);
613 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
616 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
617 * @gpio: the GPIO pin number from the GPIO subsystem number space
619 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
620 * as part of their gpio_direction_output() semantics, platforms and individual
621 * drivers shall *NOT* touch pin control GPIO calls.
623 int pinctrl_gpio_direction_output(unsigned gpio)
625 return pinctrl_gpio_direction(gpio, false);
627 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
629 static struct pinctrl_state *find_state(struct pinctrl *p,
632 struct pinctrl_state *state;
634 list_for_each_entry(state, &p->states, node)
635 if (!strcmp(state->name, name))
641 static struct pinctrl_state *create_state(struct pinctrl *p,
644 struct pinctrl_state *state;
646 state = kzalloc(sizeof(*state), GFP_KERNEL);
649 "failed to alloc struct pinctrl_state\n");
650 return ERR_PTR(-ENOMEM);
654 INIT_LIST_HEAD(&state->settings);
656 list_add_tail(&state->node, &p->states);
661 static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
663 struct pinctrl_state *state;
664 struct pinctrl_setting *setting;
667 state = find_state(p, map->name);
669 state = create_state(p, map->name);
671 return PTR_ERR(state);
673 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
676 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
677 if (setting == NULL) {
679 "failed to alloc struct pinctrl_setting\n");
683 setting->type = map->type;
685 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
686 if (setting->pctldev == NULL) {
688 /* Do not defer probing of hogs (circular loop) */
689 if (!strcmp(map->ctrl_dev_name, map->dev_name))
692 * OK let us guess that the driver is not there yet, and
693 * let's defer obtaining this pinctrl handle to later...
695 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
697 return -EPROBE_DEFER;
700 setting->dev_name = map->dev_name;
703 case PIN_MAP_TYPE_MUX_GROUP:
704 ret = pinmux_map_to_setting(map, setting);
706 case PIN_MAP_TYPE_CONFIGS_PIN:
707 case PIN_MAP_TYPE_CONFIGS_GROUP:
708 ret = pinconf_map_to_setting(map, setting);
719 list_add_tail(&setting->node, &state->settings);
724 static struct pinctrl *find_pinctrl(struct device *dev)
728 mutex_lock(&pinctrl_list_mutex);
729 list_for_each_entry(p, &pinctrl_list, node)
731 mutex_unlock(&pinctrl_list_mutex);
735 mutex_unlock(&pinctrl_list_mutex);
739 static void pinctrl_free(struct pinctrl *p, bool inlist);
741 static struct pinctrl *create_pinctrl(struct device *dev)
745 struct pinctrl_maps *maps_node;
747 struct pinctrl_map const *map;
751 * create the state cookie holder struct pinctrl for each
752 * mapping, this is what consumers will get when requesting
753 * a pin control handle with pinctrl_get()
755 p = kzalloc(sizeof(*p), GFP_KERNEL);
757 dev_err(dev, "failed to alloc struct pinctrl\n");
758 return ERR_PTR(-ENOMEM);
761 INIT_LIST_HEAD(&p->states);
762 INIT_LIST_HEAD(&p->dt_maps);
764 ret = pinctrl_dt_to_map(p);
770 devname = dev_name(dev);
772 mutex_lock(&pinctrl_maps_mutex);
773 /* Iterate over the pin control maps to locate the right ones */
774 for_each_maps(maps_node, i, map) {
775 /* Map must be for this device */
776 if (strcmp(map->dev_name, devname))
779 ret = add_setting(p, map);
781 * At this point the adding of a setting may:
783 * - Defer, if the pinctrl device is not yet available
784 * - Fail, if the pinctrl device is not yet available,
785 * AND the setting is a hog. We cannot defer that, since
786 * the hog will kick in immediately after the device
789 * If the error returned was not -EPROBE_DEFER then we
790 * accumulate the errors to see if we end up with
791 * an -EPROBE_DEFER later, as that is the worst case.
793 if (ret == -EPROBE_DEFER) {
794 pinctrl_free(p, false);
795 mutex_unlock(&pinctrl_maps_mutex);
799 mutex_unlock(&pinctrl_maps_mutex);
802 /* If some other error than deferral occured, return here */
803 pinctrl_free(p, false);
807 kref_init(&p->users);
809 /* Add the pinctrl handle to the global list */
810 mutex_lock(&pinctrl_list_mutex);
811 list_add_tail(&p->node, &pinctrl_list);
812 mutex_unlock(&pinctrl_list_mutex);
818 * pinctrl_get() - retrieves the pinctrl handle for a device
819 * @dev: the device to obtain the handle for
821 struct pinctrl *pinctrl_get(struct device *dev)
826 return ERR_PTR(-EINVAL);
829 * See if somebody else (such as the device core) has already
830 * obtained a handle to the pinctrl for this device. In that case,
831 * return another pointer to it.
833 p = find_pinctrl(dev);
835 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
840 return create_pinctrl(dev);
842 EXPORT_SYMBOL_GPL(pinctrl_get);
844 static void pinctrl_free_setting(bool disable_setting,
845 struct pinctrl_setting *setting)
847 switch (setting->type) {
848 case PIN_MAP_TYPE_MUX_GROUP:
850 pinmux_disable_setting(setting);
851 pinmux_free_setting(setting);
853 case PIN_MAP_TYPE_CONFIGS_PIN:
854 case PIN_MAP_TYPE_CONFIGS_GROUP:
855 pinconf_free_setting(setting);
862 static void pinctrl_free(struct pinctrl *p, bool inlist)
864 struct pinctrl_state *state, *n1;
865 struct pinctrl_setting *setting, *n2;
867 mutex_lock(&pinctrl_list_mutex);
868 list_for_each_entry_safe(state, n1, &p->states, node) {
869 list_for_each_entry_safe(setting, n2, &state->settings, node) {
870 pinctrl_free_setting(state == p->state, setting);
871 list_del(&setting->node);
874 list_del(&state->node);
878 pinctrl_dt_free_maps(p);
883 mutex_unlock(&pinctrl_list_mutex);
887 * pinctrl_release() - release the pinctrl handle
888 * @kref: the kref in the pinctrl being released
890 static void pinctrl_release(struct kref *kref)
892 struct pinctrl *p = container_of(kref, struct pinctrl, users);
894 pinctrl_free(p, true);
898 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
899 * @p: the pinctrl handle to release
901 void pinctrl_put(struct pinctrl *p)
903 kref_put(&p->users, pinctrl_release);
905 EXPORT_SYMBOL_GPL(pinctrl_put);
908 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
909 * @p: the pinctrl handle to retrieve the state from
910 * @name: the state name to retrieve
912 struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
915 struct pinctrl_state *state;
917 state = find_state(p, name);
919 if (pinctrl_dummy_state) {
920 /* create dummy state */
921 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
923 state = create_state(p, name);
925 state = ERR_PTR(-ENODEV);
930 EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
933 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
934 * @p: the pinctrl handle for the device that requests configuration
935 * @state: the state handle to select/activate/program
937 int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
939 struct pinctrl_setting *setting, *setting2;
940 struct pinctrl_state *old_state = p->state;
943 if (p->state == state)
948 * The set of groups with a mux configuration in the old state
949 * may not be identical to the set of groups with a mux setting
950 * in the new state. While this might be unusual, it's entirely
951 * possible for the "user"-supplied mapping table to be written
952 * that way. For each group that was configured in the old state
953 * but not in the new state, this code puts that group into a
954 * safe/disabled state.
956 list_for_each_entry(setting, &p->state->settings, node) {
958 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
960 list_for_each_entry(setting2, &state->settings, node) {
961 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
963 if (setting2->data.mux.group ==
964 setting->data.mux.group) {
970 pinmux_disable_setting(setting);
976 /* Apply all the settings for the new state */
977 list_for_each_entry(setting, &state->settings, node) {
978 switch (setting->type) {
979 case PIN_MAP_TYPE_MUX_GROUP:
980 ret = pinmux_enable_setting(setting);
982 case PIN_MAP_TYPE_CONFIGS_PIN:
983 case PIN_MAP_TYPE_CONFIGS_GROUP:
984 ret = pinconf_apply_setting(setting);
992 goto unapply_new_state;
1001 dev_err(p->dev, "Error applying setting, reverse things back\n");
1003 list_for_each_entry(setting2, &state->settings, node) {
1004 if (&setting2->node == &setting->node)
1007 * All we can do here is pinmux_disable_setting.
1008 * That means that some pins are muxed differently now
1009 * than they were before applying the setting (We can't
1010 * "unmux a pin"!), but it's not a big deal since the pins
1011 * are free to be muxed by another apply_setting.
1013 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1014 pinmux_disable_setting(setting2);
1017 /* There's no infinite recursive loop here because p->state is NULL */
1019 pinctrl_select_state(p, old_state);
1023 EXPORT_SYMBOL_GPL(pinctrl_select_state);
1025 static void devm_pinctrl_release(struct device *dev, void *res)
1027 pinctrl_put(*(struct pinctrl **)res);
1031 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1032 * @dev: the device to obtain the handle for
1034 * If there is a need to explicitly destroy the returned struct pinctrl,
1035 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1037 struct pinctrl *devm_pinctrl_get(struct device *dev)
1039 struct pinctrl **ptr, *p;
1041 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1043 return ERR_PTR(-ENOMEM);
1045 p = pinctrl_get(dev);
1048 devres_add(dev, ptr);
1055 EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1057 static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1059 struct pinctrl **p = res;
1065 * devm_pinctrl_put() - Resource managed pinctrl_put()
1066 * @p: the pinctrl handle to release
1068 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1069 * this function will not need to be called and the resource management
1070 * code will ensure that the resource is freed.
1072 void devm_pinctrl_put(struct pinctrl *p)
1074 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
1075 devm_pinctrl_match, p));
1077 EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1079 int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
1080 bool dup, bool locked)
1083 struct pinctrl_maps *maps_node;
1085 pr_debug("add %d pinmux maps\n", num_maps);
1087 /* First sanity check the new mapping */
1088 for (i = 0; i < num_maps; i++) {
1089 if (!maps[i].dev_name) {
1090 pr_err("failed to register map %s (%d): no device given\n",
1095 if (!maps[i].name) {
1096 pr_err("failed to register map %d: no map name given\n",
1101 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1102 !maps[i].ctrl_dev_name) {
1103 pr_err("failed to register map %s (%d): no pin control device given\n",
1108 switch (maps[i].type) {
1109 case PIN_MAP_TYPE_DUMMY_STATE:
1111 case PIN_MAP_TYPE_MUX_GROUP:
1112 ret = pinmux_validate_map(&maps[i], i);
1116 case PIN_MAP_TYPE_CONFIGS_PIN:
1117 case PIN_MAP_TYPE_CONFIGS_GROUP:
1118 ret = pinconf_validate_map(&maps[i], i);
1123 pr_err("failed to register map %s (%d): invalid type given\n",
1129 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1131 pr_err("failed to alloc struct pinctrl_maps\n");
1135 maps_node->num_maps = num_maps;
1137 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
1139 if (!maps_node->maps) {
1140 pr_err("failed to duplicate mapping table\n");
1145 maps_node->maps = maps;
1149 mutex_lock(&pinctrl_maps_mutex);
1150 list_add_tail(&maps_node->node, &pinctrl_maps);
1152 mutex_unlock(&pinctrl_maps_mutex);
1158 * pinctrl_register_mappings() - register a set of pin controller mappings
1159 * @maps: the pincontrol mappings table to register. This should probably be
1160 * marked with __initdata so it can be discarded after boot. This
1161 * function will perform a shallow copy for the mapping entries.
1162 * @num_maps: the number of maps in the mapping table
1164 int pinctrl_register_mappings(struct pinctrl_map const *maps,
1167 return pinctrl_register_map(maps, num_maps, true, false);
1170 void pinctrl_unregister_map(struct pinctrl_map const *map)
1172 struct pinctrl_maps *maps_node;
1174 mutex_lock(&pinctrl_maps_mutex);
1175 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1176 if (maps_node->maps == map) {
1177 list_del(&maps_node->node);
1178 mutex_unlock(&pinctrl_maps_mutex);
1182 mutex_unlock(&pinctrl_maps_mutex);
1186 * pinctrl_force_sleep() - turn a given controller device into sleep state
1187 * @pctldev: pin controller device
1189 int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1191 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1192 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
1195 EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1198 * pinctrl_force_default() - turn a given controller device into default state
1199 * @pctldev: pin controller device
1201 int pinctrl_force_default(struct pinctrl_dev *pctldev)
1203 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1204 return pinctrl_select_state(pctldev->p, pctldev->hog_default);
1207 EXPORT_SYMBOL_GPL(pinctrl_force_default);
1212 * pinctrl_pm_select_state() - select pinctrl state for PM
1213 * @dev: device to select default state for
1214 * @state: state to set
1216 static int pinctrl_pm_select_state(struct device *dev,
1217 struct pinctrl_state *state)
1219 struct dev_pin_info *pins = dev->pins;
1223 return 0; /* No such state */
1224 ret = pinctrl_select_state(pins->p, state);
1226 dev_err(dev, "failed to activate pinctrl state %s\n",
1232 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1233 * @dev: device to select default state for
1235 int pinctrl_pm_select_default_state(struct device *dev)
1240 return pinctrl_pm_select_state(dev, dev->pins->default_state);
1242 EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
1245 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1246 * @dev: device to select sleep state for
1248 int pinctrl_pm_select_sleep_state(struct device *dev)
1253 return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
1255 EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
1258 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1259 * @dev: device to select idle state for
1261 int pinctrl_pm_select_idle_state(struct device *dev)
1266 return pinctrl_pm_select_state(dev, dev->pins->idle_state);
1268 EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
1271 #ifdef CONFIG_DEBUG_FS
1273 static int pinctrl_pins_show(struct seq_file *s, void *what)
1275 struct pinctrl_dev *pctldev = s->private;
1276 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1279 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
1281 mutex_lock(&pctldev->mutex);
1283 /* The pin number can be retrived from the pin controller descriptor */
1284 for (i = 0; i < pctldev->desc->npins; i++) {
1285 struct pin_desc *desc;
1287 pin = pctldev->desc->pins[i].number;
1288 desc = pin_desc_get(pctldev, pin);
1289 /* Pin space may be sparse */
1293 seq_printf(s, "pin %d (%s) ", pin,
1294 desc->name ? desc->name : "unnamed");
1296 /* Driver-specific info per pin */
1297 if (ops->pin_dbg_show)
1298 ops->pin_dbg_show(pctldev, s, pin);
1303 mutex_unlock(&pctldev->mutex);
1308 static int pinctrl_groups_show(struct seq_file *s, void *what)
1310 struct pinctrl_dev *pctldev = s->private;
1311 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1312 unsigned ngroups, selector = 0;
1314 mutex_lock(&pctldev->mutex);
1316 ngroups = ops->get_groups_count(pctldev);
1318 seq_puts(s, "registered pin groups:\n");
1319 while (selector < ngroups) {
1320 const unsigned *pins;
1322 const char *gname = ops->get_group_name(pctldev, selector);
1327 ret = ops->get_group_pins(pctldev, selector,
1330 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1333 seq_printf(s, "group: %s\n", gname);
1334 for (i = 0; i < num_pins; i++) {
1335 pname = pin_get_name(pctldev, pins[i]);
1336 if (WARN_ON(!pname)) {
1337 mutex_unlock(&pctldev->mutex);
1340 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1347 mutex_unlock(&pctldev->mutex);
1352 static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1354 struct pinctrl_dev *pctldev = s->private;
1355 struct pinctrl_gpio_range *range = NULL;
1357 seq_puts(s, "GPIO ranges handled:\n");
1359 mutex_lock(&pctldev->mutex);
1361 /* Loop over the ranges */
1362 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1363 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1364 range->id, range->name,
1365 range->base, (range->base + range->npins - 1),
1367 (range->pin_base + range->npins - 1));
1370 mutex_unlock(&pctldev->mutex);
1375 static int pinctrl_devices_show(struct seq_file *s, void *what)
1377 struct pinctrl_dev *pctldev;
1379 seq_puts(s, "name [pinmux] [pinconf]\n");
1381 mutex_lock(&pinctrldev_list_mutex);
1383 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1384 seq_printf(s, "%s ", pctldev->desc->name);
1385 if (pctldev->desc->pmxops)
1386 seq_puts(s, "yes ");
1389 if (pctldev->desc->confops)
1396 mutex_unlock(&pinctrldev_list_mutex);
1401 static inline const char *map_type(enum pinctrl_map_type type)
1403 static const char * const names[] = {
1411 if (type >= ARRAY_SIZE(names))
1417 static int pinctrl_maps_show(struct seq_file *s, void *what)
1419 struct pinctrl_maps *maps_node;
1421 struct pinctrl_map const *map;
1423 seq_puts(s, "Pinctrl maps:\n");
1425 mutex_lock(&pinctrl_maps_mutex);
1426 for_each_maps(maps_node, i, map) {
1427 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1428 map->dev_name, map->name, map_type(map->type),
1431 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1432 seq_printf(s, "controlling device %s\n",
1433 map->ctrl_dev_name);
1435 switch (map->type) {
1436 case PIN_MAP_TYPE_MUX_GROUP:
1437 pinmux_show_map(s, map);
1439 case PIN_MAP_TYPE_CONFIGS_PIN:
1440 case PIN_MAP_TYPE_CONFIGS_GROUP:
1441 pinconf_show_map(s, map);
1447 seq_printf(s, "\n");
1449 mutex_unlock(&pinctrl_maps_mutex);
1454 static int pinctrl_show(struct seq_file *s, void *what)
1457 struct pinctrl_state *state;
1458 struct pinctrl_setting *setting;
1460 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
1462 mutex_lock(&pinctrl_list_mutex);
1464 list_for_each_entry(p, &pinctrl_list, node) {
1465 seq_printf(s, "device: %s current state: %s\n",
1467 p->state ? p->state->name : "none");
1469 list_for_each_entry(state, &p->states, node) {
1470 seq_printf(s, " state: %s\n", state->name);
1472 list_for_each_entry(setting, &state->settings, node) {
1473 struct pinctrl_dev *pctldev = setting->pctldev;
1475 seq_printf(s, " type: %s controller %s ",
1476 map_type(setting->type),
1477 pinctrl_dev_get_name(pctldev));
1479 switch (setting->type) {
1480 case PIN_MAP_TYPE_MUX_GROUP:
1481 pinmux_show_setting(s, setting);
1483 case PIN_MAP_TYPE_CONFIGS_PIN:
1484 case PIN_MAP_TYPE_CONFIGS_GROUP:
1485 pinconf_show_setting(s, setting);
1494 mutex_unlock(&pinctrl_list_mutex);
1499 static int pinctrl_pins_open(struct inode *inode, struct file *file)
1501 return single_open(file, pinctrl_pins_show, inode->i_private);
1504 static int pinctrl_groups_open(struct inode *inode, struct file *file)
1506 return single_open(file, pinctrl_groups_show, inode->i_private);
1509 static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
1511 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
1514 static int pinctrl_devices_open(struct inode *inode, struct file *file)
1516 return single_open(file, pinctrl_devices_show, NULL);
1519 static int pinctrl_maps_open(struct inode *inode, struct file *file)
1521 return single_open(file, pinctrl_maps_show, NULL);
1524 static int pinctrl_open(struct inode *inode, struct file *file)
1526 return single_open(file, pinctrl_show, NULL);
1529 static const struct file_operations pinctrl_pins_ops = {
1530 .open = pinctrl_pins_open,
1532 .llseek = seq_lseek,
1533 .release = single_release,
1536 static const struct file_operations pinctrl_groups_ops = {
1537 .open = pinctrl_groups_open,
1539 .llseek = seq_lseek,
1540 .release = single_release,
1543 static const struct file_operations pinctrl_gpioranges_ops = {
1544 .open = pinctrl_gpioranges_open,
1546 .llseek = seq_lseek,
1547 .release = single_release,
1550 static const struct file_operations pinctrl_devices_ops = {
1551 .open = pinctrl_devices_open,
1553 .llseek = seq_lseek,
1554 .release = single_release,
1557 static const struct file_operations pinctrl_maps_ops = {
1558 .open = pinctrl_maps_open,
1560 .llseek = seq_lseek,
1561 .release = single_release,
1564 static const struct file_operations pinctrl_ops = {
1565 .open = pinctrl_open,
1567 .llseek = seq_lseek,
1568 .release = single_release,
1571 static struct dentry *debugfs_root;
1573 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1575 struct dentry *device_root;
1577 device_root = debugfs_create_dir(dev_name(pctldev->dev),
1579 pctldev->device_root = device_root;
1581 if (IS_ERR(device_root) || !device_root) {
1582 pr_warn("failed to create debugfs directory for %s\n",
1583 dev_name(pctldev->dev));
1586 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1587 device_root, pctldev, &pinctrl_pins_ops);
1588 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1589 device_root, pctldev, &pinctrl_groups_ops);
1590 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1591 device_root, pctldev, &pinctrl_gpioranges_ops);
1592 pinmux_init_device_debugfs(device_root, pctldev);
1593 pinconf_init_device_debugfs(device_root, pctldev);
1596 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1598 debugfs_remove_recursive(pctldev->device_root);
1601 static void pinctrl_init_debugfs(void)
1603 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1604 if (IS_ERR(debugfs_root) || !debugfs_root) {
1605 pr_warn("failed to create debugfs directory\n");
1606 debugfs_root = NULL;
1610 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1611 debugfs_root, NULL, &pinctrl_devices_ops);
1612 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1613 debugfs_root, NULL, &pinctrl_maps_ops);
1614 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1615 debugfs_root, NULL, &pinctrl_ops);
1618 #else /* CONFIG_DEBUG_FS */
1620 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1624 static void pinctrl_init_debugfs(void)
1628 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1634 static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1636 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1639 !ops->get_groups_count ||
1640 !ops->get_group_name ||
1641 !ops->get_group_pins)
1644 if (ops->dt_node_to_map && !ops->dt_free_map)
1651 * pinctrl_register() - register a pin controller device
1652 * @pctldesc: descriptor for this pin controller
1653 * @dev: parent device for this pin controller
1654 * @driver_data: private pin controller data for this pin controller
1656 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1657 struct device *dev, void *driver_data)
1659 struct pinctrl_dev *pctldev;
1664 if (!pctldesc->name)
1667 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
1668 if (pctldev == NULL) {
1669 dev_err(dev, "failed to alloc struct pinctrl_dev\n");
1673 /* Initialize pin control device struct */
1674 pctldev->owner = pctldesc->owner;
1675 pctldev->desc = pctldesc;
1676 pctldev->driver_data = driver_data;
1677 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
1678 INIT_LIST_HEAD(&pctldev->gpio_ranges);
1680 mutex_init(&pctldev->mutex);
1682 /* check core ops for sanity */
1683 if (pinctrl_check_ops(pctldev)) {
1684 dev_err(dev, "pinctrl ops lacks necessary functions\n");
1688 /* If we're implementing pinmuxing, check the ops for sanity */
1689 if (pctldesc->pmxops) {
1690 if (pinmux_check_ops(pctldev))
1694 /* If we're implementing pinconfig, check the ops for sanity */
1695 if (pctldesc->confops) {
1696 if (pinconf_check_ops(pctldev))
1700 /* Register all the pins */
1701 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
1702 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1704 dev_err(dev, "error during pin registration\n");
1705 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1710 mutex_lock(&pinctrldev_list_mutex);
1711 list_add_tail(&pctldev->node, &pinctrldev_list);
1712 mutex_unlock(&pinctrldev_list_mutex);
1714 pctldev->p = pinctrl_get(pctldev->dev);
1716 if (!IS_ERR(pctldev->p)) {
1717 pctldev->hog_default =
1718 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
1719 if (IS_ERR(pctldev->hog_default)) {
1720 dev_dbg(dev, "failed to lookup the default state\n");
1722 if (pinctrl_select_state(pctldev->p,
1723 pctldev->hog_default))
1725 "failed to select default state\n");
1728 pctldev->hog_sleep =
1729 pinctrl_lookup_state(pctldev->p,
1730 PINCTRL_STATE_SLEEP);
1731 if (IS_ERR(pctldev->hog_sleep))
1732 dev_dbg(dev, "failed to lookup the sleep state\n");
1735 pinctrl_init_device_debugfs(pctldev);
1740 mutex_destroy(&pctldev->mutex);
1744 EXPORT_SYMBOL_GPL(pinctrl_register);
1747 * pinctrl_unregister() - unregister pinmux
1748 * @pctldev: pin controller to unregister
1750 * Called by pinmux drivers to unregister a pinmux.
1752 void pinctrl_unregister(struct pinctrl_dev *pctldev)
1754 struct pinctrl_gpio_range *range, *n;
1755 if (pctldev == NULL)
1758 mutex_lock(&pinctrldev_list_mutex);
1759 mutex_lock(&pctldev->mutex);
1761 pinctrl_remove_device_debugfs(pctldev);
1763 if (!IS_ERR(pctldev->p))
1764 pinctrl_put(pctldev->p);
1766 /* TODO: check that no pinmuxes are still active? */
1767 list_del(&pctldev->node);
1768 /* Destroy descriptor tree */
1769 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
1770 pctldev->desc->npins);
1771 /* remove gpio ranges map */
1772 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
1773 list_del(&range->node);
1775 mutex_unlock(&pctldev->mutex);
1776 mutex_destroy(&pctldev->mutex);
1778 mutex_unlock(&pinctrldev_list_mutex);
1780 EXPORT_SYMBOL_GPL(pinctrl_unregister);
1782 static int __init pinctrl_init(void)
1784 pr_info("initialized pinctrl subsystem\n");
1785 pinctrl_init_debugfs();
1789 /* init early since many drivers really need to initialized pinmux early */
1790 core_initcall(pinctrl_init);