Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / pinctrl / core.c
blobb3eec63c00ba044da37d8f75ac1eb94ede13fc14
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Core driver for the pin control subsystem
5 * Copyright (C) 2011-2012 ST-Ericsson SA
6 * Written on behalf of Linaro for ST-Ericsson
7 * Based on bits of regulator core, gpio core and clk core
9 * Author: Linus Walleij <linus.walleij@linaro.org>
11 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
13 #define pr_fmt(fmt) "pinctrl core: " fmt
15 #include <linux/array_size.h>
16 #include <linux/cleanup.h>
17 #include <linux/debugfs.h>
18 #include <linux/device.h>
19 #include <linux/err.h>
20 #include <linux/export.h>
21 #include <linux/init.h>
22 #include <linux/kref.h>
23 #include <linux/list.h>
24 #include <linux/seq_file.h>
25 #include <linux/slab.h>
27 #include <linux/gpio.h>
28 #include <linux/gpio/driver.h>
30 #include <linux/pinctrl/consumer.h>
31 #include <linux/pinctrl/devinfo.h>
32 #include <linux/pinctrl/machine.h>
33 #include <linux/pinctrl/pinctrl.h>
35 #include "core.h"
36 #include "devicetree.h"
37 #include "pinconf.h"
38 #include "pinmux.h"
40 static bool pinctrl_dummy_state;
42 /* Mutex taken to protect pinctrl_list */
43 static DEFINE_MUTEX(pinctrl_list_mutex);
45 /* Mutex taken to protect pinctrl_maps */
46 DEFINE_MUTEX(pinctrl_maps_mutex);
48 /* Mutex taken to protect pinctrldev_list */
49 static DEFINE_MUTEX(pinctrldev_list_mutex);
51 /* Global list of pin control devices (struct pinctrl_dev) */
52 static 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 LIST_HEAD(pinctrl_maps);
61 /**
62 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
64 * Usually this function is called by platforms without pinctrl driver support
65 * but run with some shared drivers using pinctrl APIs.
66 * After calling this function, the pinctrl core will return successfully
67 * with creating a dummy state for the driver to keep going smoothly.
69 void pinctrl_provide_dummies(void)
71 pinctrl_dummy_state = true;
74 const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
76 /* We're not allowed to register devices without name */
77 return pctldev->desc->name;
79 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
81 const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
83 return dev_name(pctldev->dev);
85 EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
87 void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
89 return pctldev->driver_data;
91 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
93 /**
94 * get_pinctrl_dev_from_devname() - look up pin controller device
95 * @devname: the name of a device instance, as returned by dev_name()
97 * Looks up a pin control device matching a certain device name or pure device
98 * pointer, the pure device pointer will take precedence.
100 struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
102 struct pinctrl_dev *pctldev;
104 if (!devname)
105 return NULL;
107 mutex_lock(&pinctrldev_list_mutex);
109 list_for_each_entry(pctldev, &pinctrldev_list, node) {
110 if (!strcmp(dev_name(pctldev->dev), devname)) {
111 /* Matched on device name */
112 mutex_unlock(&pinctrldev_list_mutex);
113 return pctldev;
117 mutex_unlock(&pinctrldev_list_mutex);
119 return NULL;
122 struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
124 struct pinctrl_dev *pctldev;
126 mutex_lock(&pinctrldev_list_mutex);
128 list_for_each_entry(pctldev, &pinctrldev_list, node)
129 if (device_match_of_node(pctldev->dev, np)) {
130 mutex_unlock(&pinctrldev_list_mutex);
131 return pctldev;
134 mutex_unlock(&pinctrldev_list_mutex);
136 return NULL;
140 * pin_get_from_name() - look up a pin number from a name
141 * @pctldev: the pin control device to lookup the pin on
142 * @name: the name of the pin to look up
144 int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
146 unsigned int i, pin;
148 /* The pin number can be retrived from the pin controller descriptor */
149 for (i = 0; i < pctldev->desc->npins; i++) {
150 struct pin_desc *desc;
152 pin = pctldev->desc->pins[i].number;
153 desc = pin_desc_get(pctldev, pin);
154 /* Pin space may be sparse */
155 if (desc && !strcmp(name, desc->name))
156 return pin;
159 return -EINVAL;
163 * pin_get_name() - look up a pin name from a pin id
164 * @pctldev: the pin control device to lookup the pin on
165 * @pin: pin number/id to look up
167 const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned int pin)
169 const struct pin_desc *desc;
171 desc = pin_desc_get(pctldev, pin);
172 if (!desc) {
173 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
174 pin);
175 return NULL;
178 return desc->name;
180 EXPORT_SYMBOL_GPL(pin_get_name);
182 /* Deletes a range of pin descriptors */
183 static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
184 const struct pinctrl_pin_desc *pins,
185 unsigned int num_pins)
187 int i;
189 for (i = 0; i < num_pins; i++) {
190 struct pin_desc *pindesc;
192 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
193 pins[i].number);
194 if (pindesc) {
195 radix_tree_delete(&pctldev->pin_desc_tree,
196 pins[i].number);
197 if (pindesc->dynamic_name)
198 kfree(pindesc->name);
200 kfree(pindesc);
204 static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
205 const struct pinctrl_pin_desc *pin)
207 struct pin_desc *pindesc;
208 int error;
210 pindesc = pin_desc_get(pctldev, pin->number);
211 if (pindesc) {
212 dev_err(pctldev->dev, "pin %d already registered\n",
213 pin->number);
214 return -EINVAL;
217 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
218 if (!pindesc)
219 return -ENOMEM;
221 /* Set owner */
222 pindesc->pctldev = pctldev;
223 #ifdef CONFIG_PINMUX
224 mutex_init(&pindesc->mux_lock);
225 #endif
227 /* Copy basic pin info */
228 if (pin->name) {
229 pindesc->name = pin->name;
230 } else {
231 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number);
232 if (!pindesc->name) {
233 error = -ENOMEM;
234 goto failed;
236 pindesc->dynamic_name = true;
239 pindesc->drv_data = pin->drv_data;
241 error = radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);
242 if (error)
243 goto failed;
245 pr_debug("registered pin %d (%s) on %s\n",
246 pin->number, pindesc->name, pctldev->desc->name);
247 return 0;
249 failed:
250 kfree(pindesc);
251 return error;
254 static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
255 const struct pinctrl_pin_desc *pins,
256 unsigned int num_descs)
258 unsigned int i;
259 int ret = 0;
261 for (i = 0; i < num_descs; i++) {
262 ret = pinctrl_register_one_pin(pctldev, &pins[i]);
263 if (ret)
264 return ret;
267 return 0;
271 * gpio_to_pin() - GPIO range GPIO number to pin number translation
272 * @range: GPIO range used for the translation
273 * @gc: GPIO chip structure from the GPIO subsystem
274 * @offset: hardware offset of the GPIO relative to the controller
276 * Finds the pin number for a given GPIO using the specified GPIO range
277 * as a base for translation. The distinction between linear GPIO ranges
278 * and pin list based GPIO ranges is managed correctly by this function.
280 * This function assumes the gpio is part of the specified GPIO range, use
281 * only after making sure this is the case (e.g. by calling it on the
282 * result of successful pinctrl_get_device_gpio_range calls)!
284 static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
285 struct gpio_chip *gc, unsigned int offset)
287 unsigned int pin = gc->base + offset - range->base;
288 if (range->pins)
289 return range->pins[pin];
290 else
291 return range->pin_base + pin;
295 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
296 * @pctldev: pin controller device to check
297 * @gc: GPIO chip structure from the GPIO subsystem
298 * @offset: hardware offset of the GPIO relative to the controller
300 * Tries to match a GPIO pin number to the ranges handled by a certain pin
301 * controller, return the range or NULL
303 static struct pinctrl_gpio_range *
304 pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, struct gpio_chip *gc,
305 unsigned int offset)
307 struct pinctrl_gpio_range *range;
309 mutex_lock(&pctldev->mutex);
310 /* Loop over the ranges */
311 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
312 /* Check if we're in the valid range */
313 if ((gc->base + offset) >= range->base &&
314 (gc->base + offset) < range->base + range->npins) {
315 mutex_unlock(&pctldev->mutex);
316 return range;
319 mutex_unlock(&pctldev->mutex);
320 return NULL;
324 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
325 * the same GPIO chip are in range
326 * @gc: GPIO chip structure from the GPIO subsystem
327 * @offset: hardware offset of the GPIO relative to the controller
329 * This function is complement of pinctrl_match_gpio_range(). If the return
330 * value of pinctrl_match_gpio_range() is NULL, this function could be used
331 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
332 * of the same GPIO chip don't have back-end pinctrl interface.
333 * If the return value is true, it means that pinctrl device is ready & the
334 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
335 * is false, it means that pinctrl device may not be ready.
337 #ifdef CONFIG_GPIOLIB
338 static bool pinctrl_ready_for_gpio_range(struct gpio_chip *gc,
339 unsigned int offset)
341 struct pinctrl_dev *pctldev;
342 struct pinctrl_gpio_range *range = NULL;
344 mutex_lock(&pinctrldev_list_mutex);
346 /* Loop over the pin controllers */
347 list_for_each_entry(pctldev, &pinctrldev_list, node) {
348 /* Loop over the ranges */
349 mutex_lock(&pctldev->mutex);
350 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
351 /* Check if any gpio range overlapped with gpio chip */
352 if (range->base + range->npins - 1 < gc->base ||
353 range->base > gc->base + gc->ngpio - 1)
354 continue;
355 mutex_unlock(&pctldev->mutex);
356 mutex_unlock(&pinctrldev_list_mutex);
357 return true;
359 mutex_unlock(&pctldev->mutex);
362 mutex_unlock(&pinctrldev_list_mutex);
364 return false;
366 #else
367 static inline bool
368 pinctrl_ready_for_gpio_range(struct gpio_chip *gc, unsigned int offset)
370 return true;
372 #endif
375 * pinctrl_get_device_gpio_range() - find device for GPIO range
376 * @gc: GPIO chip structure from the GPIO subsystem
377 * @offset: hardware offset of the GPIO relative to the controller
378 * @outdev: the pin control device if found
379 * @outrange: the GPIO range if found
381 * Find the pin controller handling a certain GPIO pin from the pinspace of
382 * the GPIO subsystem, return the device and the matching GPIO range. Returns
383 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
384 * may still have not been registered.
386 static int pinctrl_get_device_gpio_range(struct gpio_chip *gc,
387 unsigned int offset,
388 struct pinctrl_dev **outdev,
389 struct pinctrl_gpio_range **outrange)
391 struct pinctrl_dev *pctldev;
393 mutex_lock(&pinctrldev_list_mutex);
395 /* Loop over the pin controllers */
396 list_for_each_entry(pctldev, &pinctrldev_list, node) {
397 struct pinctrl_gpio_range *range;
399 range = pinctrl_match_gpio_range(pctldev, gc, offset);
400 if (range) {
401 *outdev = pctldev;
402 *outrange = range;
403 mutex_unlock(&pinctrldev_list_mutex);
404 return 0;
408 mutex_unlock(&pinctrldev_list_mutex);
410 return -EPROBE_DEFER;
414 * pinctrl_add_gpio_range() - register a GPIO range for a controller
415 * @pctldev: pin controller device to add the range to
416 * @range: the GPIO range to add
418 * DEPRECATED: Don't use this function in new code. See section 2 of
419 * Documentation/devicetree/bindings/gpio/gpio.txt on how to bind pinctrl and
420 * gpio drivers.
422 * This adds a range of GPIOs to be handled by a certain pin controller. Call
423 * this to register handled ranges after registering your pin controller.
425 void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
426 struct pinctrl_gpio_range *range)
428 mutex_lock(&pctldev->mutex);
429 list_add_tail(&range->node, &pctldev->gpio_ranges);
430 mutex_unlock(&pctldev->mutex);
432 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
434 void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
435 struct pinctrl_gpio_range *ranges,
436 unsigned int nranges)
438 int i;
440 for (i = 0; i < nranges; i++)
441 pinctrl_add_gpio_range(pctldev, &ranges[i]);
443 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
445 struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
446 struct pinctrl_gpio_range *range)
448 struct pinctrl_dev *pctldev;
450 pctldev = get_pinctrl_dev_from_devname(devname);
453 * If we can't find this device, let's assume that is because
454 * it has not probed yet, so the driver trying to register this
455 * range need to defer probing.
457 if (!pctldev)
458 return ERR_PTR(-EPROBE_DEFER);
460 pinctrl_add_gpio_range(pctldev, range);
462 return pctldev;
464 EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
466 int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
467 const unsigned int **pins, unsigned int *num_pins)
469 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
470 int gs;
472 if (!pctlops->get_group_pins)
473 return -EINVAL;
475 gs = pinctrl_get_group_selector(pctldev, pin_group);
476 if (gs < 0)
477 return gs;
479 return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
481 EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
483 struct pinctrl_gpio_range *
484 pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
485 unsigned int pin)
487 struct pinctrl_gpio_range *range;
489 /* Loop over the ranges */
490 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
491 /* Check if we're in the valid range */
492 if (range->pins) {
493 int a;
494 for (a = 0; a < range->npins; a++) {
495 if (range->pins[a] == pin)
496 return range;
498 } else if (pin >= range->pin_base &&
499 pin < range->pin_base + range->npins)
500 return range;
503 return NULL;
505 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock);
508 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
509 * @pctldev: the pin controller device to look in
510 * @pin: a controller-local number to find the range for
512 struct pinctrl_gpio_range *
513 pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
514 unsigned int pin)
516 struct pinctrl_gpio_range *range;
518 mutex_lock(&pctldev->mutex);
519 range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
520 mutex_unlock(&pctldev->mutex);
522 return range;
524 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
527 * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller
528 * @pctldev: pin controller device to remove the range from
529 * @range: the GPIO range to remove
531 void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
532 struct pinctrl_gpio_range *range)
534 mutex_lock(&pctldev->mutex);
535 list_del(&range->node);
536 mutex_unlock(&pctldev->mutex);
538 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
540 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
543 * pinctrl_generic_get_group_count() - returns the number of pin groups
544 * @pctldev: pin controller device
546 int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev)
548 return pctldev->num_groups;
550 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count);
553 * pinctrl_generic_get_group_name() - returns the name of a pin group
554 * @pctldev: pin controller device
555 * @selector: group number
557 const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
558 unsigned int selector)
560 struct group_desc *group;
562 group = radix_tree_lookup(&pctldev->pin_group_tree,
563 selector);
564 if (!group)
565 return NULL;
567 return group->grp.name;
569 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name);
572 * pinctrl_generic_get_group_pins() - gets the pin group pins
573 * @pctldev: pin controller device
574 * @selector: group number
575 * @pins: pins in the group
576 * @num_pins: number of pins in the group
578 int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
579 unsigned int selector,
580 const unsigned int **pins,
581 unsigned int *num_pins)
583 struct group_desc *group;
585 group = radix_tree_lookup(&pctldev->pin_group_tree,
586 selector);
587 if (!group) {
588 dev_err(pctldev->dev, "%s could not find pingroup%i\n",
589 __func__, selector);
590 return -EINVAL;
593 *pins = group->grp.pins;
594 *num_pins = group->grp.npins;
596 return 0;
598 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins);
601 * pinctrl_generic_get_group() - returns a pin group based on the number
602 * @pctldev: pin controller device
603 * @selector: group number
605 struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
606 unsigned int selector)
608 struct group_desc *group;
610 group = radix_tree_lookup(&pctldev->pin_group_tree,
611 selector);
612 if (!group)
613 return NULL;
615 return group;
617 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group);
619 static int pinctrl_generic_group_name_to_selector(struct pinctrl_dev *pctldev,
620 const char *function)
622 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
623 int ngroups = ops->get_groups_count(pctldev);
624 int selector = 0;
626 /* See if this pctldev has this group */
627 while (selector < ngroups) {
628 const char *gname = ops->get_group_name(pctldev, selector);
630 if (gname && !strcmp(function, gname))
631 return selector;
633 selector++;
636 return -EINVAL;
640 * pinctrl_generic_add_group() - adds a new pin group
641 * @pctldev: pin controller device
642 * @name: name of the pin group
643 * @pins: pins in the pin group
644 * @num_pins: number of pins in the pin group
645 * @data: pin controller driver specific data
647 * Note that the caller must take care of locking.
649 int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
650 const unsigned int *pins, int num_pins, void *data)
652 struct group_desc *group;
653 int selector, error;
655 if (!name)
656 return -EINVAL;
658 selector = pinctrl_generic_group_name_to_selector(pctldev, name);
659 if (selector >= 0)
660 return selector;
662 selector = pctldev->num_groups;
664 group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL);
665 if (!group)
666 return -ENOMEM;
668 *group = PINCTRL_GROUP_DESC(name, pins, num_pins, data);
670 error = radix_tree_insert(&pctldev->pin_group_tree, selector, group);
671 if (error)
672 return error;
674 pctldev->num_groups++;
676 return selector;
678 EXPORT_SYMBOL_GPL(pinctrl_generic_add_group);
681 * pinctrl_generic_remove_group() - removes a numbered pin group
682 * @pctldev: pin controller device
683 * @selector: group number
685 * Note that the caller must take care of locking.
687 int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
688 unsigned int selector)
690 struct group_desc *group;
692 group = radix_tree_lookup(&pctldev->pin_group_tree,
693 selector);
694 if (!group)
695 return -ENOENT;
697 radix_tree_delete(&pctldev->pin_group_tree, selector);
698 devm_kfree(pctldev->dev, group);
700 pctldev->num_groups--;
702 return 0;
704 EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group);
707 * pinctrl_generic_free_groups() - removes all pin groups
708 * @pctldev: pin controller device
710 * Note that the caller must take care of locking. The pinctrl groups
711 * are allocated with devm_kzalloc() so no need to free them here.
713 static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
715 struct radix_tree_iter iter;
716 void __rcu **slot;
718 radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0)
719 radix_tree_delete(&pctldev->pin_group_tree, iter.index);
721 pctldev->num_groups = 0;
724 #else
725 static inline void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
728 #endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
731 * pinctrl_get_group_selector() - returns the group selector for a group
732 * @pctldev: the pin controller handling the group
733 * @pin_group: the pin group to look up
735 int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
736 const char *pin_group)
738 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
739 unsigned int ngroups = pctlops->get_groups_count(pctldev);
740 unsigned int group_selector = 0;
742 while (group_selector < ngroups) {
743 const char *gname = pctlops->get_group_name(pctldev,
744 group_selector);
745 if (gname && !strcmp(gname, pin_group)) {
746 dev_dbg(pctldev->dev,
747 "found group selector %u for %s\n",
748 group_selector,
749 pin_group);
750 return group_selector;
753 group_selector++;
756 dev_err(pctldev->dev, "does not have pin group %s\n",
757 pin_group);
759 return -EINVAL;
762 bool pinctrl_gpio_can_use_line(struct gpio_chip *gc, unsigned int offset)
764 struct pinctrl_dev *pctldev;
765 struct pinctrl_gpio_range *range;
766 bool result;
767 int pin;
770 * Try to obtain GPIO range, if it fails
771 * we're probably dealing with GPIO driver
772 * without a backing pin controller - bail out.
774 if (pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range))
775 return true;
777 mutex_lock(&pctldev->mutex);
779 /* Convert to the pin controllers number space */
780 pin = gpio_to_pin(range, gc, offset);
782 result = pinmux_can_be_used_for_gpio(pctldev, pin);
784 mutex_unlock(&pctldev->mutex);
786 return result;
788 EXPORT_SYMBOL_GPL(pinctrl_gpio_can_use_line);
791 * pinctrl_gpio_request() - request a single pin to be used as GPIO
792 * @gc: GPIO chip structure from the GPIO subsystem
793 * @offset: hardware offset of the GPIO relative to the controller
795 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
796 * as part of their gpio_request() semantics, platforms and individual drivers
797 * shall *NOT* request GPIO pins to be muxed in.
799 int pinctrl_gpio_request(struct gpio_chip *gc, unsigned int offset)
801 struct pinctrl_gpio_range *range;
802 struct pinctrl_dev *pctldev;
803 int ret, pin;
805 ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range);
806 if (ret) {
807 if (pinctrl_ready_for_gpio_range(gc, offset))
808 ret = 0;
809 return ret;
812 mutex_lock(&pctldev->mutex);
814 /* Convert to the pin controllers number space */
815 pin = gpio_to_pin(range, gc, offset);
817 ret = pinmux_request_gpio(pctldev, range, pin, gc->base + offset);
819 mutex_unlock(&pctldev->mutex);
821 return ret;
823 EXPORT_SYMBOL_GPL(pinctrl_gpio_request);
826 * pinctrl_gpio_free() - free control on a single pin, currently used as GPIO
827 * @gc: GPIO chip structure from the GPIO subsystem
828 * @offset: hardware offset of the GPIO relative to the controller
830 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
831 * as part of their gpio_request() semantics, platforms and individual drivers
832 * shall *NOT* request GPIO pins to be muxed in.
834 void pinctrl_gpio_free(struct gpio_chip *gc, unsigned int offset)
836 struct pinctrl_gpio_range *range;
837 struct pinctrl_dev *pctldev;
838 int ret, pin;
840 ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range);
841 if (ret)
842 return;
844 mutex_lock(&pctldev->mutex);
846 /* Convert to the pin controllers number space */
847 pin = gpio_to_pin(range, gc, offset);
849 pinmux_free_gpio(pctldev, pin, range);
851 mutex_unlock(&pctldev->mutex);
853 EXPORT_SYMBOL_GPL(pinctrl_gpio_free);
855 static int pinctrl_gpio_direction(struct gpio_chip *gc, unsigned int offset,
856 bool input)
858 struct pinctrl_dev *pctldev;
859 struct pinctrl_gpio_range *range;
860 int ret;
861 int pin;
863 ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range);
864 if (ret) {
865 return ret;
868 mutex_lock(&pctldev->mutex);
870 /* Convert to the pin controllers number space */
871 pin = gpio_to_pin(range, gc, offset);
872 ret = pinmux_gpio_direction(pctldev, range, pin, input);
874 mutex_unlock(&pctldev->mutex);
876 return ret;
880 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
881 * @gc: GPIO chip structure from the GPIO subsystem
882 * @offset: hardware offset of the GPIO relative to the controller
884 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
885 * as part of their gpio_direction_input() semantics, platforms and individual
886 * drivers shall *NOT* touch pin control GPIO calls.
888 int pinctrl_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
890 return pinctrl_gpio_direction(gc, offset, true);
892 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
895 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
896 * @gc: GPIO chip structure from the GPIO subsystem
897 * @offset: hardware offset of the GPIO relative to the controller
899 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
900 * as part of their gpio_direction_output() semantics, platforms and individual
901 * drivers shall *NOT* touch pin control GPIO calls.
903 int pinctrl_gpio_direction_output(struct gpio_chip *gc, unsigned int offset)
905 return pinctrl_gpio_direction(gc, offset, false);
907 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
910 * pinctrl_gpio_set_config() - Apply config to given GPIO pin
911 * @gc: GPIO chip structure from the GPIO subsystem
912 * @offset: hardware offset of the GPIO relative to the controller
913 * @config: the configuration to apply to the GPIO
915 * This function should *ONLY* be used from gpiolib-based GPIO drivers, if
916 * they need to call the underlying pin controller to change GPIO config
917 * (for example set debounce time).
919 int pinctrl_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
920 unsigned long config)
922 unsigned long configs[] = { config };
923 struct pinctrl_gpio_range *range;
924 struct pinctrl_dev *pctldev;
925 int ret, pin;
927 ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range);
928 if (ret)
929 return ret;
931 mutex_lock(&pctldev->mutex);
932 pin = gpio_to_pin(range, gc, offset);
933 ret = pinconf_set_config(pctldev, pin, configs, ARRAY_SIZE(configs));
934 mutex_unlock(&pctldev->mutex);
936 return ret;
938 EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config);
940 static struct pinctrl_state *find_state(struct pinctrl *p,
941 const char *name)
943 struct pinctrl_state *state;
945 list_for_each_entry(state, &p->states, node)
946 if (!strcmp(state->name, name))
947 return state;
949 return NULL;
952 static struct pinctrl_state *create_state(struct pinctrl *p,
953 const char *name)
955 struct pinctrl_state *state;
957 state = kzalloc(sizeof(*state), GFP_KERNEL);
958 if (!state)
959 return ERR_PTR(-ENOMEM);
961 state->name = name;
962 INIT_LIST_HEAD(&state->settings);
964 list_add_tail(&state->node, &p->states);
966 return state;
969 static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev,
970 const struct pinctrl_map *map)
972 struct pinctrl_state *state;
973 struct pinctrl_setting *setting;
974 int ret;
976 state = find_state(p, map->name);
977 if (!state)
978 state = create_state(p, map->name);
979 if (IS_ERR(state))
980 return PTR_ERR(state);
982 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
983 return 0;
985 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
986 if (!setting)
987 return -ENOMEM;
989 setting->type = map->type;
991 if (pctldev)
992 setting->pctldev = pctldev;
993 else
994 setting->pctldev =
995 get_pinctrl_dev_from_devname(map->ctrl_dev_name);
996 if (!setting->pctldev) {
997 kfree(setting);
998 /* Do not defer probing of hogs (circular loop) */
999 if (!strcmp(map->ctrl_dev_name, map->dev_name))
1000 return -ENODEV;
1002 * OK let us guess that the driver is not there yet, and
1003 * let's defer obtaining this pinctrl handle to later...
1005 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
1006 map->ctrl_dev_name);
1007 return -EPROBE_DEFER;
1010 setting->dev_name = map->dev_name;
1012 switch (map->type) {
1013 case PIN_MAP_TYPE_MUX_GROUP:
1014 ret = pinmux_map_to_setting(map, setting);
1015 break;
1016 case PIN_MAP_TYPE_CONFIGS_PIN:
1017 case PIN_MAP_TYPE_CONFIGS_GROUP:
1018 ret = pinconf_map_to_setting(map, setting);
1019 break;
1020 default:
1021 ret = -EINVAL;
1022 break;
1024 if (ret < 0) {
1025 kfree(setting);
1026 return ret;
1029 list_add_tail(&setting->node, &state->settings);
1031 return 0;
1034 static struct pinctrl *find_pinctrl(struct device *dev)
1036 struct pinctrl *p;
1038 mutex_lock(&pinctrl_list_mutex);
1039 list_for_each_entry(p, &pinctrl_list, node)
1040 if (p->dev == dev) {
1041 mutex_unlock(&pinctrl_list_mutex);
1042 return p;
1045 mutex_unlock(&pinctrl_list_mutex);
1046 return NULL;
1049 static void pinctrl_free(struct pinctrl *p, bool inlist);
1051 static struct pinctrl *create_pinctrl(struct device *dev,
1052 struct pinctrl_dev *pctldev)
1054 struct pinctrl *p;
1055 const char *devname;
1056 struct pinctrl_maps *maps_node;
1057 const struct pinctrl_map *map;
1058 int ret;
1061 * create the state cookie holder struct pinctrl for each
1062 * mapping, this is what consumers will get when requesting
1063 * a pin control handle with pinctrl_get()
1065 p = kzalloc(sizeof(*p), GFP_KERNEL);
1066 if (!p)
1067 return ERR_PTR(-ENOMEM);
1068 p->dev = dev;
1069 INIT_LIST_HEAD(&p->states);
1070 INIT_LIST_HEAD(&p->dt_maps);
1072 ret = pinctrl_dt_to_map(p, pctldev);
1073 if (ret < 0) {
1074 kfree(p);
1075 return ERR_PTR(ret);
1078 devname = dev_name(dev);
1080 mutex_lock(&pinctrl_maps_mutex);
1081 /* Iterate over the pin control maps to locate the right ones */
1082 for_each_pin_map(maps_node, map) {
1083 /* Map must be for this device */
1084 if (strcmp(map->dev_name, devname))
1085 continue;
1087 * If pctldev is not null, we are claiming hog for it,
1088 * that means, setting that is served by pctldev by itself.
1090 * Thus we must skip map that is for this device but is served
1091 * by other device.
1093 if (pctldev &&
1094 strcmp(dev_name(pctldev->dev), map->ctrl_dev_name))
1095 continue;
1097 ret = add_setting(p, pctldev, map);
1099 * At this point the adding of a setting may:
1101 * - Defer, if the pinctrl device is not yet available
1102 * - Fail, if the pinctrl device is not yet available,
1103 * AND the setting is a hog. We cannot defer that, since
1104 * the hog will kick in immediately after the device
1105 * is registered.
1107 * If the error returned was not -EPROBE_DEFER then we
1108 * accumulate the errors to see if we end up with
1109 * an -EPROBE_DEFER later, as that is the worst case.
1111 if (ret == -EPROBE_DEFER) {
1112 mutex_unlock(&pinctrl_maps_mutex);
1113 pinctrl_free(p, false);
1114 return ERR_PTR(ret);
1117 mutex_unlock(&pinctrl_maps_mutex);
1119 if (ret < 0) {
1120 /* If some other error than deferral occurred, return here */
1121 pinctrl_free(p, false);
1122 return ERR_PTR(ret);
1125 kref_init(&p->users);
1127 /* Add the pinctrl handle to the global list */
1128 mutex_lock(&pinctrl_list_mutex);
1129 list_add_tail(&p->node, &pinctrl_list);
1130 mutex_unlock(&pinctrl_list_mutex);
1132 return p;
1136 * pinctrl_get() - retrieves the pinctrl handle for a device
1137 * @dev: the device to obtain the handle for
1139 struct pinctrl *pinctrl_get(struct device *dev)
1141 struct pinctrl *p;
1143 if (WARN_ON(!dev))
1144 return ERR_PTR(-EINVAL);
1147 * See if somebody else (such as the device core) has already
1148 * obtained a handle to the pinctrl for this device. In that case,
1149 * return another pointer to it.
1151 p = find_pinctrl(dev);
1152 if (p) {
1153 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
1154 kref_get(&p->users);
1155 return p;
1158 return create_pinctrl(dev, NULL);
1160 EXPORT_SYMBOL_GPL(pinctrl_get);
1162 static void pinctrl_free_setting(bool disable_setting,
1163 struct pinctrl_setting *setting)
1165 switch (setting->type) {
1166 case PIN_MAP_TYPE_MUX_GROUP:
1167 if (disable_setting)
1168 pinmux_disable_setting(setting);
1169 pinmux_free_setting(setting);
1170 break;
1171 case PIN_MAP_TYPE_CONFIGS_PIN:
1172 case PIN_MAP_TYPE_CONFIGS_GROUP:
1173 pinconf_free_setting(setting);
1174 break;
1175 default:
1176 break;
1180 static void pinctrl_free(struct pinctrl *p, bool inlist)
1182 struct pinctrl_state *state, *n1;
1183 struct pinctrl_setting *setting, *n2;
1185 mutex_lock(&pinctrl_list_mutex);
1186 list_for_each_entry_safe(state, n1, &p->states, node) {
1187 list_for_each_entry_safe(setting, n2, &state->settings, node) {
1188 pinctrl_free_setting(state == p->state, setting);
1189 list_del(&setting->node);
1190 kfree(setting);
1192 list_del(&state->node);
1193 kfree(state);
1196 pinctrl_dt_free_maps(p);
1198 if (inlist)
1199 list_del(&p->node);
1200 kfree(p);
1201 mutex_unlock(&pinctrl_list_mutex);
1205 * pinctrl_release() - release the pinctrl handle
1206 * @kref: the kref in the pinctrl being released
1208 static void pinctrl_release(struct kref *kref)
1210 struct pinctrl *p = container_of(kref, struct pinctrl, users);
1212 pinctrl_free(p, true);
1216 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
1217 * @p: the pinctrl handle to release
1219 void pinctrl_put(struct pinctrl *p)
1221 kref_put(&p->users, pinctrl_release);
1223 EXPORT_SYMBOL_GPL(pinctrl_put);
1226 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
1227 * @p: the pinctrl handle to retrieve the state from
1228 * @name: the state name to retrieve
1230 struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
1231 const char *name)
1233 struct pinctrl_state *state;
1235 state = find_state(p, name);
1236 if (!state) {
1237 if (pinctrl_dummy_state) {
1238 /* create dummy state */
1239 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
1240 name);
1241 state = create_state(p, name);
1242 } else
1243 state = ERR_PTR(-ENODEV);
1246 return state;
1248 EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
1250 static void pinctrl_link_add(struct pinctrl_dev *pctldev,
1251 struct device *consumer)
1253 if (pctldev->desc->link_consumers)
1254 device_link_add(consumer, pctldev->dev,
1255 DL_FLAG_PM_RUNTIME |
1256 DL_FLAG_AUTOREMOVE_CONSUMER);
1260 * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
1261 * @p: the pinctrl handle for the device that requests configuration
1262 * @state: the state handle to select/activate/program
1264 static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
1266 struct pinctrl_setting *setting, *setting2;
1267 struct pinctrl_state *old_state = READ_ONCE(p->state);
1268 int ret;
1270 if (old_state) {
1272 * For each pinmux setting in the old state, forget SW's record
1273 * of mux owner for that pingroup. Any pingroups which are
1274 * still owned by the new state will be re-acquired by the call
1275 * to pinmux_enable_setting() in the loop below.
1277 list_for_each_entry(setting, &old_state->settings, node) {
1278 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
1279 continue;
1280 pinmux_disable_setting(setting);
1284 p->state = NULL;
1286 /* Apply all the settings for the new state - pinmux first */
1287 list_for_each_entry(setting, &state->settings, node) {
1288 switch (setting->type) {
1289 case PIN_MAP_TYPE_MUX_GROUP:
1290 ret = pinmux_enable_setting(setting);
1291 break;
1292 case PIN_MAP_TYPE_CONFIGS_PIN:
1293 case PIN_MAP_TYPE_CONFIGS_GROUP:
1294 ret = 0;
1295 break;
1296 default:
1297 ret = -EINVAL;
1298 break;
1301 if (ret < 0)
1302 goto unapply_new_state;
1304 /* Do not link hogs (circular dependency) */
1305 if (p != setting->pctldev->p)
1306 pinctrl_link_add(setting->pctldev, p->dev);
1309 /* Apply all the settings for the new state - pinconf after */
1310 list_for_each_entry(setting, &state->settings, node) {
1311 switch (setting->type) {
1312 case PIN_MAP_TYPE_MUX_GROUP:
1313 ret = 0;
1314 break;
1315 case PIN_MAP_TYPE_CONFIGS_PIN:
1316 case PIN_MAP_TYPE_CONFIGS_GROUP:
1317 ret = pinconf_apply_setting(setting);
1318 break;
1319 default:
1320 ret = -EINVAL;
1321 break;
1324 if (ret < 0) {
1325 goto unapply_new_state;
1328 /* Do not link hogs (circular dependency) */
1329 if (p != setting->pctldev->p)
1330 pinctrl_link_add(setting->pctldev, p->dev);
1333 p->state = state;
1335 return 0;
1337 unapply_new_state:
1338 dev_err(p->dev, "Error applying setting, reverse things back\n");
1340 list_for_each_entry(setting2, &state->settings, node) {
1341 if (&setting2->node == &setting->node)
1342 break;
1344 * All we can do here is pinmux_disable_setting.
1345 * That means that some pins are muxed differently now
1346 * than they were before applying the setting (We can't
1347 * "unmux a pin"!), but it's not a big deal since the pins
1348 * are free to be muxed by another apply_setting.
1350 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1351 pinmux_disable_setting(setting2);
1354 /* There's no infinite recursive loop here because p->state is NULL */
1355 if (old_state)
1356 pinctrl_select_state(p, old_state);
1358 return ret;
1362 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
1363 * @p: the pinctrl handle for the device that requests configuration
1364 * @state: the state handle to select/activate/program
1366 int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
1368 if (p->state == state)
1369 return 0;
1371 return pinctrl_commit_state(p, state);
1373 EXPORT_SYMBOL_GPL(pinctrl_select_state);
1375 static void devm_pinctrl_release(struct device *dev, void *res)
1377 pinctrl_put(*(struct pinctrl **)res);
1381 * devm_pinctrl_get() - Resource managed pinctrl_get()
1382 * @dev: the device to obtain the handle for
1384 * If there is a need to explicitly destroy the returned struct pinctrl,
1385 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1387 struct pinctrl *devm_pinctrl_get(struct device *dev)
1389 struct pinctrl **ptr, *p;
1391 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1392 if (!ptr)
1393 return ERR_PTR(-ENOMEM);
1395 p = pinctrl_get(dev);
1396 if (!IS_ERR(p)) {
1397 *ptr = p;
1398 devres_add(dev, ptr);
1399 } else {
1400 devres_free(ptr);
1403 return p;
1405 EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1407 static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1409 struct pinctrl **p = res;
1411 return *p == data;
1415 * devm_pinctrl_put() - Resource managed pinctrl_put()
1416 * @p: the pinctrl handle to release
1418 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1419 * this function will not need to be called and the resource management
1420 * code will ensure that the resource is freed.
1422 void devm_pinctrl_put(struct pinctrl *p)
1424 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
1425 devm_pinctrl_match, p));
1427 EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1430 * pinctrl_register_mappings() - register a set of pin controller mappings
1431 * @maps: the pincontrol mappings table to register. Note the pinctrl-core
1432 * keeps a reference to the passed in maps, so they should _not_ be
1433 * marked with __initdata.
1434 * @num_maps: the number of maps in the mapping table
1436 int pinctrl_register_mappings(const struct pinctrl_map *maps,
1437 unsigned int num_maps)
1439 int i, ret;
1440 struct pinctrl_maps *maps_node;
1442 pr_debug("add %u pinctrl maps\n", num_maps);
1444 /* First sanity check the new mapping */
1445 for (i = 0; i < num_maps; i++) {
1446 if (!maps[i].dev_name) {
1447 pr_err("failed to register map %s (%d): no device given\n",
1448 maps[i].name, i);
1449 return -EINVAL;
1452 if (!maps[i].name) {
1453 pr_err("failed to register map %d: no map name given\n",
1455 return -EINVAL;
1458 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1459 !maps[i].ctrl_dev_name) {
1460 pr_err("failed to register map %s (%d): no pin control device given\n",
1461 maps[i].name, i);
1462 return -EINVAL;
1465 switch (maps[i].type) {
1466 case PIN_MAP_TYPE_DUMMY_STATE:
1467 break;
1468 case PIN_MAP_TYPE_MUX_GROUP:
1469 ret = pinmux_validate_map(&maps[i], i);
1470 if (ret < 0)
1471 return ret;
1472 break;
1473 case PIN_MAP_TYPE_CONFIGS_PIN:
1474 case PIN_MAP_TYPE_CONFIGS_GROUP:
1475 ret = pinconf_validate_map(&maps[i], i);
1476 if (ret < 0)
1477 return ret;
1478 break;
1479 default:
1480 pr_err("failed to register map %s (%d): invalid type given\n",
1481 maps[i].name, i);
1482 return -EINVAL;
1486 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1487 if (!maps_node)
1488 return -ENOMEM;
1490 maps_node->maps = maps;
1491 maps_node->num_maps = num_maps;
1493 mutex_lock(&pinctrl_maps_mutex);
1494 list_add_tail(&maps_node->node, &pinctrl_maps);
1495 mutex_unlock(&pinctrl_maps_mutex);
1497 return 0;
1499 EXPORT_SYMBOL_GPL(pinctrl_register_mappings);
1502 * pinctrl_unregister_mappings() - unregister a set of pin controller mappings
1503 * @map: the pincontrol mappings table passed to pinctrl_register_mappings()
1504 * when registering the mappings.
1506 void pinctrl_unregister_mappings(const struct pinctrl_map *map)
1508 struct pinctrl_maps *maps_node;
1510 mutex_lock(&pinctrl_maps_mutex);
1511 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1512 if (maps_node->maps == map) {
1513 list_del(&maps_node->node);
1514 kfree(maps_node);
1515 mutex_unlock(&pinctrl_maps_mutex);
1516 return;
1519 mutex_unlock(&pinctrl_maps_mutex);
1521 EXPORT_SYMBOL_GPL(pinctrl_unregister_mappings);
1524 * pinctrl_force_sleep() - turn a given controller device into sleep state
1525 * @pctldev: pin controller device
1527 int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1529 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1530 return pinctrl_commit_state(pctldev->p, pctldev->hog_sleep);
1531 return 0;
1533 EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1536 * pinctrl_force_default() - turn a given controller device into default state
1537 * @pctldev: pin controller device
1539 int pinctrl_force_default(struct pinctrl_dev *pctldev)
1541 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1542 return pinctrl_commit_state(pctldev->p, pctldev->hog_default);
1543 return 0;
1545 EXPORT_SYMBOL_GPL(pinctrl_force_default);
1548 * pinctrl_init_done() - tell pinctrl probe is done
1550 * We'll use this time to switch the pins from "init" to "default" unless the
1551 * driver selected some other state.
1553 * @dev: device to that's done probing
1555 int pinctrl_init_done(struct device *dev)
1557 struct dev_pin_info *pins = dev->pins;
1558 int ret;
1560 if (!pins)
1561 return 0;
1563 if (IS_ERR(pins->init_state))
1564 return 0; /* No such state */
1566 if (pins->p->state != pins->init_state)
1567 return 0; /* Not at init anyway */
1569 if (IS_ERR(pins->default_state))
1570 return 0; /* No default state */
1572 ret = pinctrl_select_state(pins->p, pins->default_state);
1573 if (ret)
1574 dev_err(dev, "failed to activate default pinctrl state\n");
1576 return ret;
1579 static int pinctrl_select_bound_state(struct device *dev,
1580 struct pinctrl_state *state)
1582 struct dev_pin_info *pins = dev->pins;
1583 int ret;
1585 if (IS_ERR(state))
1586 return 0; /* No such state */
1587 ret = pinctrl_select_state(pins->p, state);
1588 if (ret)
1589 dev_err(dev, "failed to activate pinctrl state %s\n",
1590 state->name);
1591 return ret;
1595 * pinctrl_select_default_state() - select default pinctrl state
1596 * @dev: device to select default state for
1598 int pinctrl_select_default_state(struct device *dev)
1600 if (!dev->pins)
1601 return 0;
1603 return pinctrl_select_bound_state(dev, dev->pins->default_state);
1605 EXPORT_SYMBOL_GPL(pinctrl_select_default_state);
1607 #ifdef CONFIG_PM
1610 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1611 * @dev: device to select default state for
1613 int pinctrl_pm_select_default_state(struct device *dev)
1615 return pinctrl_select_default_state(dev);
1617 EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
1620 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1621 * @dev: device to select sleep state for
1623 int pinctrl_pm_select_sleep_state(struct device *dev)
1625 if (!dev->pins)
1626 return 0;
1628 return pinctrl_select_bound_state(dev, dev->pins->sleep_state);
1630 EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
1633 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1634 * @dev: device to select idle state for
1636 int pinctrl_pm_select_idle_state(struct device *dev)
1638 if (!dev->pins)
1639 return 0;
1641 return pinctrl_select_bound_state(dev, dev->pins->idle_state);
1643 EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
1644 #endif
1646 #ifdef CONFIG_DEBUG_FS
1648 static int pinctrl_pins_show(struct seq_file *s, void *what)
1650 struct pinctrl_dev *pctldev = s->private;
1651 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1652 unsigned int i, pin;
1653 #ifdef CONFIG_GPIOLIB
1654 struct gpio_device *gdev = NULL;
1655 struct pinctrl_gpio_range *range;
1656 int gpio_num;
1657 #endif
1659 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
1661 mutex_lock(&pctldev->mutex);
1663 /* The pin number can be retrived from the pin controller descriptor */
1664 for (i = 0; i < pctldev->desc->npins; i++) {
1665 struct pin_desc *desc;
1667 pin = pctldev->desc->pins[i].number;
1668 desc = pin_desc_get(pctldev, pin);
1669 /* Pin space may be sparse */
1670 if (!desc)
1671 continue;
1673 seq_printf(s, "pin %d (%s) ", pin, desc->name);
1675 #ifdef CONFIG_GPIOLIB
1676 gdev = NULL;
1677 gpio_num = -1;
1678 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1679 if (range->pins != NULL) {
1680 for (int i = 0; i < range->npins; ++i) {
1681 if (range->pins[i] == pin) {
1682 gpio_num = range->base + i;
1683 break;
1686 } else if ((pin >= range->pin_base) &&
1687 (pin < (range->pin_base + range->npins))) {
1688 gpio_num =
1689 range->base + (pin - range->pin_base);
1691 if (gpio_num != -1)
1692 break;
1694 if (gpio_num >= 0)
1696 * FIXME: gpio_num comes from the global GPIO numberspace.
1697 * we need to get rid of the range->base eventually and
1698 * get the descriptor directly from the gpio_chip.
1700 gdev = gpiod_to_gpio_device(gpio_to_desc(gpio_num));
1701 if (gdev)
1702 seq_printf(s, "%u:%s ",
1703 gpio_num - gpio_device_get_base(gdev),
1704 gpio_device_get_label(gdev));
1705 else
1706 seq_puts(s, "0:? ");
1707 #endif
1709 /* Driver-specific info per pin */
1710 if (ops->pin_dbg_show)
1711 ops->pin_dbg_show(pctldev, s, pin);
1713 seq_puts(s, "\n");
1716 mutex_unlock(&pctldev->mutex);
1718 return 0;
1720 DEFINE_SHOW_ATTRIBUTE(pinctrl_pins);
1722 static int pinctrl_groups_show(struct seq_file *s, void *what)
1724 struct pinctrl_dev *pctldev = s->private;
1725 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1726 unsigned int ngroups, selector = 0;
1728 mutex_lock(&pctldev->mutex);
1730 ngroups = ops->get_groups_count(pctldev);
1732 seq_puts(s, "registered pin groups:\n");
1733 while (selector < ngroups) {
1734 const unsigned int *pins = NULL;
1735 unsigned int num_pins = 0;
1736 const char *gname = ops->get_group_name(pctldev, selector);
1737 const char *pname;
1738 int ret = 0;
1739 int i;
1741 if (ops->get_group_pins)
1742 ret = ops->get_group_pins(pctldev, selector,
1743 &pins, &num_pins);
1744 if (ret)
1745 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1746 gname);
1747 else {
1748 seq_printf(s, "group: %s\n", gname);
1749 for (i = 0; i < num_pins; i++) {
1750 pname = pin_get_name(pctldev, pins[i]);
1751 if (WARN_ON(!pname)) {
1752 mutex_unlock(&pctldev->mutex);
1753 return -EINVAL;
1755 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1757 seq_puts(s, "\n");
1759 selector++;
1762 mutex_unlock(&pctldev->mutex);
1764 return 0;
1766 DEFINE_SHOW_ATTRIBUTE(pinctrl_groups);
1768 static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1770 struct pinctrl_dev *pctldev = s->private;
1771 struct pinctrl_gpio_range *range;
1773 seq_puts(s, "GPIO ranges handled:\n");
1775 mutex_lock(&pctldev->mutex);
1777 /* Loop over the ranges */
1778 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1779 if (range->pins) {
1780 int a;
1781 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1782 range->id, range->name,
1783 range->base, (range->base + range->npins - 1));
1784 for (a = 0; a < range->npins - 1; a++)
1785 seq_printf(s, "%u, ", range->pins[a]);
1786 seq_printf(s, "%u}\n", range->pins[a]);
1788 else
1789 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1790 range->id, range->name,
1791 range->base, (range->base + range->npins - 1),
1792 range->pin_base,
1793 (range->pin_base + range->npins - 1));
1796 mutex_unlock(&pctldev->mutex);
1798 return 0;
1800 DEFINE_SHOW_ATTRIBUTE(pinctrl_gpioranges);
1802 static int pinctrl_devices_show(struct seq_file *s, void *what)
1804 struct pinctrl_dev *pctldev;
1806 seq_puts(s, "name [pinmux] [pinconf]\n");
1808 mutex_lock(&pinctrldev_list_mutex);
1810 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1811 seq_printf(s, "%s ", pctldev->desc->name);
1812 if (pctldev->desc->pmxops)
1813 seq_puts(s, "yes ");
1814 else
1815 seq_puts(s, "no ");
1816 if (pctldev->desc->confops)
1817 seq_puts(s, "yes");
1818 else
1819 seq_puts(s, "no");
1820 seq_puts(s, "\n");
1823 mutex_unlock(&pinctrldev_list_mutex);
1825 return 0;
1827 DEFINE_SHOW_ATTRIBUTE(pinctrl_devices);
1829 static inline const char *map_type(enum pinctrl_map_type type)
1831 static const char * const names[] = {
1832 "INVALID",
1833 "DUMMY_STATE",
1834 "MUX_GROUP",
1835 "CONFIGS_PIN",
1836 "CONFIGS_GROUP",
1839 if (type >= ARRAY_SIZE(names))
1840 return "UNKNOWN";
1842 return names[type];
1845 static int pinctrl_maps_show(struct seq_file *s, void *what)
1847 struct pinctrl_maps *maps_node;
1848 const struct pinctrl_map *map;
1850 seq_puts(s, "Pinctrl maps:\n");
1852 mutex_lock(&pinctrl_maps_mutex);
1853 for_each_pin_map(maps_node, map) {
1854 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1855 map->dev_name, map->name, map_type(map->type),
1856 map->type);
1858 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1859 seq_printf(s, "controlling device %s\n",
1860 map->ctrl_dev_name);
1862 switch (map->type) {
1863 case PIN_MAP_TYPE_MUX_GROUP:
1864 pinmux_show_map(s, map);
1865 break;
1866 case PIN_MAP_TYPE_CONFIGS_PIN:
1867 case PIN_MAP_TYPE_CONFIGS_GROUP:
1868 pinconf_show_map(s, map);
1869 break;
1870 default:
1871 break;
1874 seq_putc(s, '\n');
1876 mutex_unlock(&pinctrl_maps_mutex);
1878 return 0;
1880 DEFINE_SHOW_ATTRIBUTE(pinctrl_maps);
1882 static int pinctrl_show(struct seq_file *s, void *what)
1884 struct pinctrl *p;
1885 struct pinctrl_state *state;
1886 struct pinctrl_setting *setting;
1888 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
1890 mutex_lock(&pinctrl_list_mutex);
1892 list_for_each_entry(p, &pinctrl_list, node) {
1893 seq_printf(s, "device: %s current state: %s\n",
1894 dev_name(p->dev),
1895 p->state ? p->state->name : "none");
1897 list_for_each_entry(state, &p->states, node) {
1898 seq_printf(s, " state: %s\n", state->name);
1900 list_for_each_entry(setting, &state->settings, node) {
1901 struct pinctrl_dev *pctldev = setting->pctldev;
1903 seq_printf(s, " type: %s controller %s ",
1904 map_type(setting->type),
1905 pinctrl_dev_get_name(pctldev));
1907 switch (setting->type) {
1908 case PIN_MAP_TYPE_MUX_GROUP:
1909 pinmux_show_setting(s, setting);
1910 break;
1911 case PIN_MAP_TYPE_CONFIGS_PIN:
1912 case PIN_MAP_TYPE_CONFIGS_GROUP:
1913 pinconf_show_setting(s, setting);
1914 break;
1915 default:
1916 break;
1922 mutex_unlock(&pinctrl_list_mutex);
1924 return 0;
1926 DEFINE_SHOW_ATTRIBUTE(pinctrl);
1928 static struct dentry *debugfs_root;
1930 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1932 struct dentry *device_root;
1933 const char *debugfs_name;
1935 if (pctldev->desc->name &&
1936 strcmp(dev_name(pctldev->dev), pctldev->desc->name)) {
1937 debugfs_name = devm_kasprintf(pctldev->dev, GFP_KERNEL,
1938 "%s-%s", dev_name(pctldev->dev),
1939 pctldev->desc->name);
1940 if (!debugfs_name) {
1941 pr_warn("failed to determine debugfs dir name for %s\n",
1942 dev_name(pctldev->dev));
1943 return;
1945 } else {
1946 debugfs_name = dev_name(pctldev->dev);
1949 device_root = debugfs_create_dir(debugfs_name, debugfs_root);
1950 pctldev->device_root = device_root;
1952 if (IS_ERR(device_root) || !device_root) {
1953 pr_warn("failed to create debugfs directory for %s\n",
1954 dev_name(pctldev->dev));
1955 return;
1957 debugfs_create_file("pins", 0444,
1958 device_root, pctldev, &pinctrl_pins_fops);
1959 debugfs_create_file("pingroups", 0444,
1960 device_root, pctldev, &pinctrl_groups_fops);
1961 debugfs_create_file("gpio-ranges", 0444,
1962 device_root, pctldev, &pinctrl_gpioranges_fops);
1963 if (pctldev->desc->pmxops)
1964 pinmux_init_device_debugfs(device_root, pctldev);
1965 if (pctldev->desc->confops)
1966 pinconf_init_device_debugfs(device_root, pctldev);
1969 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1971 debugfs_remove_recursive(pctldev->device_root);
1974 static void pinctrl_init_debugfs(void)
1976 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1977 if (IS_ERR(debugfs_root)) {
1978 pr_warn("failed to create debugfs directory\n");
1979 debugfs_root = NULL;
1980 return;
1983 debugfs_create_file("pinctrl-devices", 0444,
1984 debugfs_root, NULL, &pinctrl_devices_fops);
1985 debugfs_create_file("pinctrl-maps", 0444,
1986 debugfs_root, NULL, &pinctrl_maps_fops);
1987 debugfs_create_file("pinctrl-handles", 0444,
1988 debugfs_root, NULL, &pinctrl_fops);
1991 #else /* CONFIG_DEBUG_FS */
1993 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1997 static void pinctrl_init_debugfs(void)
2001 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
2005 #endif
2007 static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
2009 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
2011 if (!ops ||
2012 !ops->get_groups_count ||
2013 !ops->get_group_name)
2014 return -EINVAL;
2016 return 0;
2020 * pinctrl_init_controller() - init a pin controller device
2021 * @pctldesc: descriptor for this pin controller
2022 * @dev: parent device for this pin controller
2023 * @driver_data: private pin controller data for this pin controller
2025 static struct pinctrl_dev *
2026 pinctrl_init_controller(struct pinctrl_desc *pctldesc, struct device *dev,
2027 void *driver_data)
2029 struct pinctrl_dev *pctldev;
2030 int ret;
2032 if (!pctldesc)
2033 return ERR_PTR(-EINVAL);
2034 if (!pctldesc->name)
2035 return ERR_PTR(-EINVAL);
2037 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
2038 if (!pctldev)
2039 return ERR_PTR(-ENOMEM);
2041 /* Initialize pin control device struct */
2042 pctldev->owner = pctldesc->owner;
2043 pctldev->desc = pctldesc;
2044 pctldev->driver_data = driver_data;
2045 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
2046 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
2047 INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL);
2048 #endif
2049 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
2050 INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL);
2051 #endif
2052 INIT_LIST_HEAD(&pctldev->gpio_ranges);
2053 INIT_LIST_HEAD(&pctldev->node);
2054 pctldev->dev = dev;
2055 mutex_init(&pctldev->mutex);
2057 /* check core ops for sanity */
2058 ret = pinctrl_check_ops(pctldev);
2059 if (ret) {
2060 dev_err(dev, "pinctrl ops lacks necessary functions\n");
2061 goto out_err;
2064 /* If we're implementing pinmuxing, check the ops for sanity */
2065 if (pctldesc->pmxops) {
2066 ret = pinmux_check_ops(pctldev);
2067 if (ret)
2068 goto out_err;
2071 /* If we're implementing pinconfig, check the ops for sanity */
2072 if (pctldesc->confops) {
2073 ret = pinconf_check_ops(pctldev);
2074 if (ret)
2075 goto out_err;
2078 /* Register all the pins */
2079 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
2080 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
2081 if (ret) {
2082 dev_err(dev, "error during pin registration\n");
2083 pinctrl_free_pindescs(pctldev, pctldesc->pins,
2084 pctldesc->npins);
2085 goto out_err;
2088 return pctldev;
2090 out_err:
2091 mutex_destroy(&pctldev->mutex);
2092 kfree(pctldev);
2093 return ERR_PTR(ret);
2096 static void pinctrl_uninit_controller(struct pinctrl_dev *pctldev, struct pinctrl_desc *pctldesc)
2098 pinctrl_free_pindescs(pctldev, pctldesc->pins,
2099 pctldesc->npins);
2100 mutex_destroy(&pctldev->mutex);
2101 kfree(pctldev);
2104 static int pinctrl_claim_hogs(struct pinctrl_dev *pctldev)
2106 pctldev->p = create_pinctrl(pctldev->dev, pctldev);
2107 if (PTR_ERR(pctldev->p) == -ENODEV) {
2108 dev_dbg(pctldev->dev, "no hogs found\n");
2110 return 0;
2113 if (IS_ERR(pctldev->p)) {
2114 dev_err(pctldev->dev, "error claiming hogs: %li\n",
2115 PTR_ERR(pctldev->p));
2117 return PTR_ERR(pctldev->p);
2120 pctldev->hog_default =
2121 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
2122 if (IS_ERR(pctldev->hog_default)) {
2123 dev_dbg(pctldev->dev,
2124 "failed to lookup the default state\n");
2125 } else {
2126 if (pinctrl_select_state(pctldev->p,
2127 pctldev->hog_default))
2128 dev_err(pctldev->dev,
2129 "failed to select default state\n");
2132 pctldev->hog_sleep =
2133 pinctrl_lookup_state(pctldev->p,
2134 PINCTRL_STATE_SLEEP);
2135 if (IS_ERR(pctldev->hog_sleep))
2136 dev_dbg(pctldev->dev,
2137 "failed to lookup the sleep state\n");
2139 return 0;
2142 int pinctrl_enable(struct pinctrl_dev *pctldev)
2144 int error;
2146 error = pinctrl_claim_hogs(pctldev);
2147 if (error) {
2148 dev_err(pctldev->dev, "could not claim hogs: %i\n", error);
2149 return error;
2152 mutex_lock(&pinctrldev_list_mutex);
2153 list_add_tail(&pctldev->node, &pinctrldev_list);
2154 mutex_unlock(&pinctrldev_list_mutex);
2156 pinctrl_init_device_debugfs(pctldev);
2158 return 0;
2160 EXPORT_SYMBOL_GPL(pinctrl_enable);
2163 * pinctrl_register() - register a pin controller device
2164 * @pctldesc: descriptor for this pin controller
2165 * @dev: parent device for this pin controller
2166 * @driver_data: private pin controller data for this pin controller
2168 * Note that pinctrl_register() is known to have problems as the pin
2169 * controller driver functions are called before the driver has a
2170 * struct pinctrl_dev handle. To avoid issues later on, please use the
2171 * new pinctrl_register_and_init() below instead.
2173 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
2174 struct device *dev, void *driver_data)
2176 struct pinctrl_dev *pctldev;
2177 int error;
2179 pctldev = pinctrl_init_controller(pctldesc, dev, driver_data);
2180 if (IS_ERR(pctldev))
2181 return pctldev;
2183 error = pinctrl_enable(pctldev);
2184 if (error) {
2185 pinctrl_uninit_controller(pctldev, pctldesc);
2186 return ERR_PTR(error);
2189 return pctldev;
2191 EXPORT_SYMBOL_GPL(pinctrl_register);
2194 * pinctrl_register_and_init() - register and init pin controller device
2195 * @pctldesc: descriptor for this pin controller
2196 * @dev: parent device for this pin controller
2197 * @driver_data: private pin controller data for this pin controller
2198 * @pctldev: pin controller device
2200 * Note that pinctrl_enable() still needs to be manually called after
2201 * this once the driver is ready.
2203 int pinctrl_register_and_init(struct pinctrl_desc *pctldesc,
2204 struct device *dev, void *driver_data,
2205 struct pinctrl_dev **pctldev)
2207 struct pinctrl_dev *p;
2209 p = pinctrl_init_controller(pctldesc, dev, driver_data);
2210 if (IS_ERR(p))
2211 return PTR_ERR(p);
2214 * We have pinctrl_start() call functions in the pin controller
2215 * driver with create_pinctrl() for at least dt_node_to_map(). So
2216 * let's make sure pctldev is properly initialized for the
2217 * pin controller driver before we do anything.
2219 *pctldev = p;
2221 return 0;
2223 EXPORT_SYMBOL_GPL(pinctrl_register_and_init);
2226 * pinctrl_unregister() - unregister pinmux
2227 * @pctldev: pin controller to unregister
2229 * Called by pinmux drivers to unregister a pinmux.
2231 void pinctrl_unregister(struct pinctrl_dev *pctldev)
2233 struct pinctrl_gpio_range *range, *n;
2235 if (!pctldev)
2236 return;
2238 mutex_lock(&pctldev->mutex);
2239 pinctrl_remove_device_debugfs(pctldev);
2240 mutex_unlock(&pctldev->mutex);
2242 if (!IS_ERR_OR_NULL(pctldev->p))
2243 pinctrl_put(pctldev->p);
2245 mutex_lock(&pinctrldev_list_mutex);
2246 mutex_lock(&pctldev->mutex);
2247 /* TODO: check that no pinmuxes are still active? */
2248 list_del(&pctldev->node);
2249 pinmux_generic_free_functions(pctldev);
2250 pinctrl_generic_free_groups(pctldev);
2251 /* Destroy descriptor tree */
2252 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
2253 pctldev->desc->npins);
2254 /* remove gpio ranges map */
2255 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
2256 list_del(&range->node);
2258 mutex_unlock(&pctldev->mutex);
2259 mutex_destroy(&pctldev->mutex);
2260 kfree(pctldev);
2261 mutex_unlock(&pinctrldev_list_mutex);
2263 EXPORT_SYMBOL_GPL(pinctrl_unregister);
2265 static void devm_pinctrl_dev_release(struct device *dev, void *res)
2267 struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
2269 pinctrl_unregister(pctldev);
2272 static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
2274 struct pctldev **r = res;
2276 if (WARN_ON(!r || !*r))
2277 return 0;
2279 return *r == data;
2283 * devm_pinctrl_register() - Resource managed version of pinctrl_register().
2284 * @dev: parent device for this pin controller
2285 * @pctldesc: descriptor for this pin controller
2286 * @driver_data: private pin controller data for this pin controller
2288 * Returns an error pointer if pincontrol register failed. Otherwise
2289 * it returns valid pinctrl handle.
2291 * The pinctrl device will be automatically released when the device is unbound.
2293 struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
2294 struct pinctrl_desc *pctldesc,
2295 void *driver_data)
2297 struct pinctrl_dev **ptr, *pctldev;
2299 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2300 if (!ptr)
2301 return ERR_PTR(-ENOMEM);
2303 pctldev = pinctrl_register(pctldesc, dev, driver_data);
2304 if (IS_ERR(pctldev)) {
2305 devres_free(ptr);
2306 return pctldev;
2309 *ptr = pctldev;
2310 devres_add(dev, ptr);
2312 return pctldev;
2314 EXPORT_SYMBOL_GPL(devm_pinctrl_register);
2317 * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
2318 * @dev: parent device for this pin controller
2319 * @pctldesc: descriptor for this pin controller
2320 * @driver_data: private pin controller data for this pin controller
2321 * @pctldev: pin controller device
2323 * Returns zero on success or an error number on failure.
2325 * The pinctrl device will be automatically released when the device is unbound.
2327 int devm_pinctrl_register_and_init(struct device *dev,
2328 struct pinctrl_desc *pctldesc,
2329 void *driver_data,
2330 struct pinctrl_dev **pctldev)
2332 struct pinctrl_dev **ptr;
2333 int error;
2335 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2336 if (!ptr)
2337 return -ENOMEM;
2339 error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev);
2340 if (error) {
2341 devres_free(ptr);
2342 return error;
2345 *ptr = *pctldev;
2346 devres_add(dev, ptr);
2348 return 0;
2350 EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init);
2353 * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
2354 * @dev: device for which resource was allocated
2355 * @pctldev: the pinctrl device to unregister.
2357 void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
2359 WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
2360 devm_pinctrl_dev_match, pctldev));
2362 EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
2364 static int __init pinctrl_init(void)
2366 pr_info("initialized pinctrl subsystem\n");
2367 pinctrl_init_debugfs();
2368 return 0;
2371 /* init early since many drivers really need to initialized pinmux early */
2372 core_initcall(pinctrl_init);