treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / pinctrl / core.c
blob446d84fe0e31c7ec471bfc7fa66a3e782996ac04
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/kernel.h>
16 #include <linux/kref.h>
17 #include <linux/export.h>
18 #include <linux/init.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/err.h>
22 #include <linux/list.h>
23 #include <linux/debugfs.h>
24 #include <linux/seq_file.h>
25 #include <linux/pinctrl/consumer.h>
26 #include <linux/pinctrl/pinctrl.h>
27 #include <linux/pinctrl/machine.h>
29 #ifdef CONFIG_GPIOLIB
30 #include <asm-generic/gpio.h>
31 #endif
33 #include "core.h"
34 #include "devicetree.h"
35 #include "pinmux.h"
36 #include "pinconf.h"
39 static bool pinctrl_dummy_state;
41 /* Mutex taken to protect pinctrl_list */
42 static DEFINE_MUTEX(pinctrl_list_mutex);
44 /* Mutex taken to protect pinctrl_maps */
45 DEFINE_MUTEX(pinctrl_maps_mutex);
47 /* Mutex taken to protect pinctrldev_list */
48 static DEFINE_MUTEX(pinctrldev_list_mutex);
50 /* Global list of pin control devices (struct pinctrl_dev) */
51 static LIST_HEAD(pinctrldev_list);
53 /* List of pin controller handles (struct pinctrl) */
54 static LIST_HEAD(pinctrl_list);
56 /* List of pinctrl maps (struct pinctrl_maps) */
57 LIST_HEAD(pinctrl_maps);
60 /**
61 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
63 * Usually this function is called by platforms without pinctrl driver support
64 * but run with some shared drivers using pinctrl APIs.
65 * After calling this function, the pinctrl core will return successfully
66 * with creating a dummy state for the driver to keep going smoothly.
68 void pinctrl_provide_dummies(void)
70 pinctrl_dummy_state = true;
73 const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
75 /* We're not allowed to register devices without name */
76 return pctldev->desc->name;
78 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
80 const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
82 return dev_name(pctldev->dev);
84 EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
86 void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
88 return pctldev->driver_data;
90 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
92 /**
93 * get_pinctrl_dev_from_devname() - look up pin controller device
94 * @devname: the name of a device instance, as returned by dev_name()
96 * Looks up a pin control device matching a certain device name or pure device
97 * pointer, the pure device pointer will take precedence.
99 struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
101 struct pinctrl_dev *pctldev;
103 if (!devname)
104 return NULL;
106 mutex_lock(&pinctrldev_list_mutex);
108 list_for_each_entry(pctldev, &pinctrldev_list, node) {
109 if (!strcmp(dev_name(pctldev->dev), devname)) {
110 /* Matched on device name */
111 mutex_unlock(&pinctrldev_list_mutex);
112 return pctldev;
116 mutex_unlock(&pinctrldev_list_mutex);
118 return NULL;
121 struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
123 struct pinctrl_dev *pctldev;
125 mutex_lock(&pinctrldev_list_mutex);
127 list_for_each_entry(pctldev, &pinctrldev_list, node)
128 if (pctldev->dev->of_node == np) {
129 mutex_unlock(&pinctrldev_list_mutex);
130 return pctldev;
133 mutex_unlock(&pinctrldev_list_mutex);
135 return NULL;
139 * pin_get_from_name() - look up a pin number from a name
140 * @pctldev: the pin control device to lookup the pin on
141 * @name: the name of the pin to look up
143 int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
145 unsigned i, pin;
147 /* The pin number can be retrived from the pin controller descriptor */
148 for (i = 0; i < pctldev->desc->npins; i++) {
149 struct pin_desc *desc;
151 pin = pctldev->desc->pins[i].number;
152 desc = pin_desc_get(pctldev, pin);
153 /* Pin space may be sparse */
154 if (desc && !strcmp(name, desc->name))
155 return pin;
158 return -EINVAL;
162 * pin_get_name_from_id() - look up a pin name from a pin id
163 * @pctldev: the pin control device to lookup the pin on
164 * @name: the name of the pin to look up
166 const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
168 const struct pin_desc *desc;
170 desc = pin_desc_get(pctldev, pin);
171 if (!desc) {
172 dev_err(pctldev->dev, "failed to get pin(%d) name\n",
173 pin);
174 return NULL;
177 return desc->name;
180 /* Deletes a range of pin descriptors */
181 static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
182 const struct pinctrl_pin_desc *pins,
183 unsigned num_pins)
185 int i;
187 for (i = 0; i < num_pins; i++) {
188 struct pin_desc *pindesc;
190 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
191 pins[i].number);
192 if (pindesc) {
193 radix_tree_delete(&pctldev->pin_desc_tree,
194 pins[i].number);
195 if (pindesc->dynamic_name)
196 kfree(pindesc->name);
198 kfree(pindesc);
202 static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
203 const struct pinctrl_pin_desc *pin)
205 struct pin_desc *pindesc;
207 pindesc = pin_desc_get(pctldev, pin->number);
208 if (pindesc) {
209 dev_err(pctldev->dev, "pin %d already registered\n",
210 pin->number);
211 return -EINVAL;
214 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
215 if (!pindesc)
216 return -ENOMEM;
218 /* Set owner */
219 pindesc->pctldev = pctldev;
221 /* Copy basic pin info */
222 if (pin->name) {
223 pindesc->name = pin->name;
224 } else {
225 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number);
226 if (!pindesc->name) {
227 kfree(pindesc);
228 return -ENOMEM;
230 pindesc->dynamic_name = true;
233 pindesc->drv_data = pin->drv_data;
235 radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);
236 pr_debug("registered pin %d (%s) on %s\n",
237 pin->number, pindesc->name, pctldev->desc->name);
238 return 0;
241 static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
242 const struct pinctrl_pin_desc *pins,
243 unsigned num_descs)
245 unsigned i;
246 int ret = 0;
248 for (i = 0; i < num_descs; i++) {
249 ret = pinctrl_register_one_pin(pctldev, &pins[i]);
250 if (ret)
251 return ret;
254 return 0;
258 * gpio_to_pin() - GPIO range GPIO number to pin number translation
259 * @range: GPIO range used for the translation
260 * @gpio: gpio pin to translate to a pin number
262 * Finds the pin number for a given GPIO using the specified GPIO range
263 * as a base for translation. The distinction between linear GPIO ranges
264 * and pin list based GPIO ranges is managed correctly by this function.
266 * This function assumes the gpio is part of the specified GPIO range, use
267 * only after making sure this is the case (e.g. by calling it on the
268 * result of successful pinctrl_get_device_gpio_range calls)!
270 static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
271 unsigned int gpio)
273 unsigned int offset = gpio - range->base;
274 if (range->pins)
275 return range->pins[offset];
276 else
277 return range->pin_base + offset;
281 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
282 * @pctldev: pin controller device to check
283 * @gpio: gpio pin to check taken from the global GPIO pin space
285 * Tries to match a GPIO pin number to the ranges handled by a certain pin
286 * controller, return the range or NULL
288 static struct pinctrl_gpio_range *
289 pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
291 struct pinctrl_gpio_range *range;
293 mutex_lock(&pctldev->mutex);
294 /* Loop over the ranges */
295 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
296 /* Check if we're in the valid range */
297 if (gpio >= range->base &&
298 gpio < range->base + range->npins) {
299 mutex_unlock(&pctldev->mutex);
300 return range;
303 mutex_unlock(&pctldev->mutex);
304 return NULL;
308 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
309 * the same GPIO chip are in range
310 * @gpio: gpio pin to check taken from the global GPIO pin space
312 * This function is complement of pinctrl_match_gpio_range(). If the return
313 * value of pinctrl_match_gpio_range() is NULL, this function could be used
314 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
315 * of the same GPIO chip don't have back-end pinctrl interface.
316 * If the return value is true, it means that pinctrl device is ready & the
317 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
318 * is false, it means that pinctrl device may not be ready.
320 #ifdef CONFIG_GPIOLIB
321 static bool pinctrl_ready_for_gpio_range(unsigned gpio)
323 struct pinctrl_dev *pctldev;
324 struct pinctrl_gpio_range *range = NULL;
325 struct gpio_chip *chip = gpio_to_chip(gpio);
327 if (WARN(!chip, "no gpio_chip for gpio%i?", gpio))
328 return false;
330 mutex_lock(&pinctrldev_list_mutex);
332 /* Loop over the pin controllers */
333 list_for_each_entry(pctldev, &pinctrldev_list, node) {
334 /* Loop over the ranges */
335 mutex_lock(&pctldev->mutex);
336 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
337 /* Check if any gpio range overlapped with gpio chip */
338 if (range->base + range->npins - 1 < chip->base ||
339 range->base > chip->base + chip->ngpio - 1)
340 continue;
341 mutex_unlock(&pctldev->mutex);
342 mutex_unlock(&pinctrldev_list_mutex);
343 return true;
345 mutex_unlock(&pctldev->mutex);
348 mutex_unlock(&pinctrldev_list_mutex);
350 return false;
352 #else
353 static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
354 #endif
357 * pinctrl_get_device_gpio_range() - find device for GPIO range
358 * @gpio: the pin to locate the pin controller for
359 * @outdev: the pin control device if found
360 * @outrange: the GPIO range if found
362 * Find the pin controller handling a certain GPIO pin from the pinspace of
363 * the GPIO subsystem, return the device and the matching GPIO range. Returns
364 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
365 * may still have not been registered.
367 static int pinctrl_get_device_gpio_range(unsigned gpio,
368 struct pinctrl_dev **outdev,
369 struct pinctrl_gpio_range **outrange)
371 struct pinctrl_dev *pctldev;
373 mutex_lock(&pinctrldev_list_mutex);
375 /* Loop over the pin controllers */
376 list_for_each_entry(pctldev, &pinctrldev_list, node) {
377 struct pinctrl_gpio_range *range;
379 range = pinctrl_match_gpio_range(pctldev, gpio);
380 if (range) {
381 *outdev = pctldev;
382 *outrange = range;
383 mutex_unlock(&pinctrldev_list_mutex);
384 return 0;
388 mutex_unlock(&pinctrldev_list_mutex);
390 return -EPROBE_DEFER;
394 * pinctrl_add_gpio_range() - register a GPIO range for a controller
395 * @pctldev: pin controller device to add the range to
396 * @range: the GPIO range to add
398 * This adds a range of GPIOs to be handled by a certain pin controller. Call
399 * this to register handled ranges after registering your pin controller.
401 void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
402 struct pinctrl_gpio_range *range)
404 mutex_lock(&pctldev->mutex);
405 list_add_tail(&range->node, &pctldev->gpio_ranges);
406 mutex_unlock(&pctldev->mutex);
408 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
410 void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
411 struct pinctrl_gpio_range *ranges,
412 unsigned nranges)
414 int i;
416 for (i = 0; i < nranges; i++)
417 pinctrl_add_gpio_range(pctldev, &ranges[i]);
419 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
421 struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
422 struct pinctrl_gpio_range *range)
424 struct pinctrl_dev *pctldev;
426 pctldev = get_pinctrl_dev_from_devname(devname);
429 * If we can't find this device, let's assume that is because
430 * it has not probed yet, so the driver trying to register this
431 * range need to defer probing.
433 if (!pctldev) {
434 return ERR_PTR(-EPROBE_DEFER);
436 pinctrl_add_gpio_range(pctldev, range);
438 return pctldev;
440 EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
442 int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
443 const unsigned **pins, unsigned *num_pins)
445 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
446 int gs;
448 if (!pctlops->get_group_pins)
449 return -EINVAL;
451 gs = pinctrl_get_group_selector(pctldev, pin_group);
452 if (gs < 0)
453 return gs;
455 return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
457 EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
459 struct pinctrl_gpio_range *
460 pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
461 unsigned int pin)
463 struct pinctrl_gpio_range *range;
465 /* Loop over the ranges */
466 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
467 /* Check if we're in the valid range */
468 if (range->pins) {
469 int a;
470 for (a = 0; a < range->npins; a++) {
471 if (range->pins[a] == pin)
472 return range;
474 } else if (pin >= range->pin_base &&
475 pin < range->pin_base + range->npins)
476 return range;
479 return NULL;
481 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock);
484 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
485 * @pctldev: the pin controller device to look in
486 * @pin: a controller-local number to find the range for
488 struct pinctrl_gpio_range *
489 pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
490 unsigned int pin)
492 struct pinctrl_gpio_range *range;
494 mutex_lock(&pctldev->mutex);
495 range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
496 mutex_unlock(&pctldev->mutex);
498 return range;
500 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
503 * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller
504 * @pctldev: pin controller device to remove the range from
505 * @range: the GPIO range to remove
507 void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
508 struct pinctrl_gpio_range *range)
510 mutex_lock(&pctldev->mutex);
511 list_del(&range->node);
512 mutex_unlock(&pctldev->mutex);
514 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
516 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
519 * pinctrl_generic_get_group_count() - returns the number of pin groups
520 * @pctldev: pin controller device
522 int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev)
524 return pctldev->num_groups;
526 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count);
529 * pinctrl_generic_get_group_name() - returns the name of a pin group
530 * @pctldev: pin controller device
531 * @selector: group number
533 const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
534 unsigned int selector)
536 struct group_desc *group;
538 group = radix_tree_lookup(&pctldev->pin_group_tree,
539 selector);
540 if (!group)
541 return NULL;
543 return group->name;
545 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name);
548 * pinctrl_generic_get_group_pins() - gets the pin group pins
549 * @pctldev: pin controller device
550 * @selector: group number
551 * @pins: pins in the group
552 * @num_pins: number of pins in the group
554 int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
555 unsigned int selector,
556 const unsigned int **pins,
557 unsigned int *num_pins)
559 struct group_desc *group;
561 group = radix_tree_lookup(&pctldev->pin_group_tree,
562 selector);
563 if (!group) {
564 dev_err(pctldev->dev, "%s could not find pingroup%i\n",
565 __func__, selector);
566 return -EINVAL;
569 *pins = group->pins;
570 *num_pins = group->num_pins;
572 return 0;
574 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins);
577 * pinctrl_generic_get_group() - returns a pin group based on the number
578 * @pctldev: pin controller device
579 * @gselector: group number
581 struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
582 unsigned int selector)
584 struct group_desc *group;
586 group = radix_tree_lookup(&pctldev->pin_group_tree,
587 selector);
588 if (!group)
589 return NULL;
591 return group;
593 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group);
595 static int pinctrl_generic_group_name_to_selector(struct pinctrl_dev *pctldev,
596 const char *function)
598 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
599 int ngroups = ops->get_groups_count(pctldev);
600 int selector = 0;
602 /* See if this pctldev has this group */
603 while (selector < ngroups) {
604 const char *gname = ops->get_group_name(pctldev, selector);
606 if (gname && !strcmp(function, gname))
607 return selector;
609 selector++;
612 return -EINVAL;
616 * pinctrl_generic_add_group() - adds a new pin group
617 * @pctldev: pin controller device
618 * @name: name of the pin group
619 * @pins: pins in the pin group
620 * @num_pins: number of pins in the pin group
621 * @data: pin controller driver specific data
623 * Note that the caller must take care of locking.
625 int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
626 int *pins, int num_pins, void *data)
628 struct group_desc *group;
629 int selector;
631 if (!name)
632 return -EINVAL;
634 selector = pinctrl_generic_group_name_to_selector(pctldev, name);
635 if (selector >= 0)
636 return selector;
638 selector = pctldev->num_groups;
640 group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL);
641 if (!group)
642 return -ENOMEM;
644 group->name = name;
645 group->pins = pins;
646 group->num_pins = num_pins;
647 group->data = data;
649 radix_tree_insert(&pctldev->pin_group_tree, selector, group);
651 pctldev->num_groups++;
653 return selector;
655 EXPORT_SYMBOL_GPL(pinctrl_generic_add_group);
658 * pinctrl_generic_remove_group() - removes a numbered pin group
659 * @pctldev: pin controller device
660 * @selector: group number
662 * Note that the caller must take care of locking.
664 int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
665 unsigned int selector)
667 struct group_desc *group;
669 group = radix_tree_lookup(&pctldev->pin_group_tree,
670 selector);
671 if (!group)
672 return -ENOENT;
674 radix_tree_delete(&pctldev->pin_group_tree, selector);
675 devm_kfree(pctldev->dev, group);
677 pctldev->num_groups--;
679 return 0;
681 EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group);
684 * pinctrl_generic_free_groups() - removes all pin groups
685 * @pctldev: pin controller device
687 * Note that the caller must take care of locking. The pinctrl groups
688 * are allocated with devm_kzalloc() so no need to free them here.
690 static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
692 struct radix_tree_iter iter;
693 void __rcu **slot;
695 radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0)
696 radix_tree_delete(&pctldev->pin_group_tree, iter.index);
698 pctldev->num_groups = 0;
701 #else
702 static inline void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
705 #endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
708 * pinctrl_get_group_selector() - returns the group selector for a group
709 * @pctldev: the pin controller handling the group
710 * @pin_group: the pin group to look up
712 int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
713 const char *pin_group)
715 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
716 unsigned ngroups = pctlops->get_groups_count(pctldev);
717 unsigned group_selector = 0;
719 while (group_selector < ngroups) {
720 const char *gname = pctlops->get_group_name(pctldev,
721 group_selector);
722 if (gname && !strcmp(gname, pin_group)) {
723 dev_dbg(pctldev->dev,
724 "found group selector %u for %s\n",
725 group_selector,
726 pin_group);
727 return group_selector;
730 group_selector++;
733 dev_err(pctldev->dev, "does not have pin group %s\n",
734 pin_group);
736 return -EINVAL;
739 bool pinctrl_gpio_can_use_line(unsigned gpio)
741 struct pinctrl_dev *pctldev;
742 struct pinctrl_gpio_range *range;
743 bool result;
744 int pin;
747 * Try to obtain GPIO range, if it fails
748 * we're probably dealing with GPIO driver
749 * without a backing pin controller - bail out.
751 if (pinctrl_get_device_gpio_range(gpio, &pctldev, &range))
752 return true;
754 mutex_lock(&pctldev->mutex);
756 /* Convert to the pin controllers number space */
757 pin = gpio_to_pin(range, gpio);
759 result = pinmux_can_be_used_for_gpio(pctldev, pin);
761 mutex_unlock(&pctldev->mutex);
763 return result;
765 EXPORT_SYMBOL_GPL(pinctrl_gpio_can_use_line);
768 * pinctrl_gpio_request() - request a single pin to be used as GPIO
769 * @gpio: the GPIO pin number from the GPIO subsystem number space
771 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
772 * as part of their gpio_request() semantics, platforms and individual drivers
773 * shall *NOT* request GPIO pins to be muxed in.
775 int pinctrl_gpio_request(unsigned gpio)
777 struct pinctrl_dev *pctldev;
778 struct pinctrl_gpio_range *range;
779 int ret;
780 int pin;
782 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
783 if (ret) {
784 if (pinctrl_ready_for_gpio_range(gpio))
785 ret = 0;
786 return ret;
789 mutex_lock(&pctldev->mutex);
791 /* Convert to the pin controllers number space */
792 pin = gpio_to_pin(range, gpio);
794 ret = pinmux_request_gpio(pctldev, range, pin, gpio);
796 mutex_unlock(&pctldev->mutex);
798 return ret;
800 EXPORT_SYMBOL_GPL(pinctrl_gpio_request);
803 * pinctrl_gpio_free() - free control on a single pin, currently used as GPIO
804 * @gpio: the GPIO pin number from the GPIO subsystem number space
806 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
807 * as part of their gpio_free() semantics, platforms and individual drivers
808 * shall *NOT* request GPIO pins to be muxed out.
810 void pinctrl_gpio_free(unsigned gpio)
812 struct pinctrl_dev *pctldev;
813 struct pinctrl_gpio_range *range;
814 int ret;
815 int pin;
817 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
818 if (ret) {
819 return;
821 mutex_lock(&pctldev->mutex);
823 /* Convert to the pin controllers number space */
824 pin = gpio_to_pin(range, gpio);
826 pinmux_free_gpio(pctldev, pin, range);
828 mutex_unlock(&pctldev->mutex);
830 EXPORT_SYMBOL_GPL(pinctrl_gpio_free);
832 static int pinctrl_gpio_direction(unsigned gpio, bool input)
834 struct pinctrl_dev *pctldev;
835 struct pinctrl_gpio_range *range;
836 int ret;
837 int pin;
839 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
840 if (ret) {
841 return ret;
844 mutex_lock(&pctldev->mutex);
846 /* Convert to the pin controllers number space */
847 pin = gpio_to_pin(range, gpio);
848 ret = pinmux_gpio_direction(pctldev, range, pin, input);
850 mutex_unlock(&pctldev->mutex);
852 return ret;
856 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
857 * @gpio: the GPIO pin number from the GPIO subsystem number space
859 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
860 * as part of their gpio_direction_input() semantics, platforms and individual
861 * drivers shall *NOT* touch pin control GPIO calls.
863 int pinctrl_gpio_direction_input(unsigned gpio)
865 return pinctrl_gpio_direction(gpio, true);
867 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
870 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
871 * @gpio: the GPIO pin number from the GPIO subsystem number space
873 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
874 * as part of their gpio_direction_output() semantics, platforms and individual
875 * drivers shall *NOT* touch pin control GPIO calls.
877 int pinctrl_gpio_direction_output(unsigned gpio)
879 return pinctrl_gpio_direction(gpio, false);
881 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
884 * pinctrl_gpio_set_config() - Apply config to given GPIO pin
885 * @gpio: the GPIO pin number from the GPIO subsystem number space
886 * @config: the configuration to apply to the GPIO
888 * This function should *ONLY* be used from gpiolib-based GPIO drivers, if
889 * they need to call the underlying pin controller to change GPIO config
890 * (for example set debounce time).
892 int pinctrl_gpio_set_config(unsigned gpio, unsigned long config)
894 unsigned long configs[] = { config };
895 struct pinctrl_gpio_range *range;
896 struct pinctrl_dev *pctldev;
897 int ret, pin;
899 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
900 if (ret)
901 return ret;
903 mutex_lock(&pctldev->mutex);
904 pin = gpio_to_pin(range, gpio);
905 ret = pinconf_set_config(pctldev, pin, configs, ARRAY_SIZE(configs));
906 mutex_unlock(&pctldev->mutex);
908 return ret;
910 EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config);
912 static struct pinctrl_state *find_state(struct pinctrl *p,
913 const char *name)
915 struct pinctrl_state *state;
917 list_for_each_entry(state, &p->states, node)
918 if (!strcmp(state->name, name))
919 return state;
921 return NULL;
924 static struct pinctrl_state *create_state(struct pinctrl *p,
925 const char *name)
927 struct pinctrl_state *state;
929 state = kzalloc(sizeof(*state), GFP_KERNEL);
930 if (!state)
931 return ERR_PTR(-ENOMEM);
933 state->name = name;
934 INIT_LIST_HEAD(&state->settings);
936 list_add_tail(&state->node, &p->states);
938 return state;
941 static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev,
942 const struct pinctrl_map *map)
944 struct pinctrl_state *state;
945 struct pinctrl_setting *setting;
946 int ret;
948 state = find_state(p, map->name);
949 if (!state)
950 state = create_state(p, map->name);
951 if (IS_ERR(state))
952 return PTR_ERR(state);
954 if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
955 return 0;
957 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
958 if (!setting)
959 return -ENOMEM;
961 setting->type = map->type;
963 if (pctldev)
964 setting->pctldev = pctldev;
965 else
966 setting->pctldev =
967 get_pinctrl_dev_from_devname(map->ctrl_dev_name);
968 if (!setting->pctldev) {
969 kfree(setting);
970 /* Do not defer probing of hogs (circular loop) */
971 if (!strcmp(map->ctrl_dev_name, map->dev_name))
972 return -ENODEV;
974 * OK let us guess that the driver is not there yet, and
975 * let's defer obtaining this pinctrl handle to later...
977 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
978 map->ctrl_dev_name);
979 return -EPROBE_DEFER;
982 setting->dev_name = map->dev_name;
984 switch (map->type) {
985 case PIN_MAP_TYPE_MUX_GROUP:
986 ret = pinmux_map_to_setting(map, setting);
987 break;
988 case PIN_MAP_TYPE_CONFIGS_PIN:
989 case PIN_MAP_TYPE_CONFIGS_GROUP:
990 ret = pinconf_map_to_setting(map, setting);
991 break;
992 default:
993 ret = -EINVAL;
994 break;
996 if (ret < 0) {
997 kfree(setting);
998 return ret;
1001 list_add_tail(&setting->node, &state->settings);
1003 return 0;
1006 static struct pinctrl *find_pinctrl(struct device *dev)
1008 struct pinctrl *p;
1010 mutex_lock(&pinctrl_list_mutex);
1011 list_for_each_entry(p, &pinctrl_list, node)
1012 if (p->dev == dev) {
1013 mutex_unlock(&pinctrl_list_mutex);
1014 return p;
1017 mutex_unlock(&pinctrl_list_mutex);
1018 return NULL;
1021 static void pinctrl_free(struct pinctrl *p, bool inlist);
1023 static struct pinctrl *create_pinctrl(struct device *dev,
1024 struct pinctrl_dev *pctldev)
1026 struct pinctrl *p;
1027 const char *devname;
1028 struct pinctrl_maps *maps_node;
1029 int i;
1030 const struct pinctrl_map *map;
1031 int ret;
1034 * create the state cookie holder struct pinctrl for each
1035 * mapping, this is what consumers will get when requesting
1036 * a pin control handle with pinctrl_get()
1038 p = kzalloc(sizeof(*p), GFP_KERNEL);
1039 if (!p)
1040 return ERR_PTR(-ENOMEM);
1041 p->dev = dev;
1042 INIT_LIST_HEAD(&p->states);
1043 INIT_LIST_HEAD(&p->dt_maps);
1045 ret = pinctrl_dt_to_map(p, pctldev);
1046 if (ret < 0) {
1047 kfree(p);
1048 return ERR_PTR(ret);
1051 devname = dev_name(dev);
1053 mutex_lock(&pinctrl_maps_mutex);
1054 /* Iterate over the pin control maps to locate the right ones */
1055 for_each_maps(maps_node, i, map) {
1056 /* Map must be for this device */
1057 if (strcmp(map->dev_name, devname))
1058 continue;
1060 * If pctldev is not null, we are claiming hog for it,
1061 * that means, setting that is served by pctldev by itself.
1063 * Thus we must skip map that is for this device but is served
1064 * by other device.
1066 if (pctldev &&
1067 strcmp(dev_name(pctldev->dev), map->ctrl_dev_name))
1068 continue;
1070 ret = add_setting(p, pctldev, map);
1072 * At this point the adding of a setting may:
1074 * - Defer, if the pinctrl device is not yet available
1075 * - Fail, if the pinctrl device is not yet available,
1076 * AND the setting is a hog. We cannot defer that, since
1077 * the hog will kick in immediately after the device
1078 * is registered.
1080 * If the error returned was not -EPROBE_DEFER then we
1081 * accumulate the errors to see if we end up with
1082 * an -EPROBE_DEFER later, as that is the worst case.
1084 if (ret == -EPROBE_DEFER) {
1085 pinctrl_free(p, false);
1086 mutex_unlock(&pinctrl_maps_mutex);
1087 return ERR_PTR(ret);
1090 mutex_unlock(&pinctrl_maps_mutex);
1092 if (ret < 0) {
1093 /* If some other error than deferral occurred, return here */
1094 pinctrl_free(p, false);
1095 return ERR_PTR(ret);
1098 kref_init(&p->users);
1100 /* Add the pinctrl handle to the global list */
1101 mutex_lock(&pinctrl_list_mutex);
1102 list_add_tail(&p->node, &pinctrl_list);
1103 mutex_unlock(&pinctrl_list_mutex);
1105 return p;
1109 * pinctrl_get() - retrieves the pinctrl handle for a device
1110 * @dev: the device to obtain the handle for
1112 struct pinctrl *pinctrl_get(struct device *dev)
1114 struct pinctrl *p;
1116 if (WARN_ON(!dev))
1117 return ERR_PTR(-EINVAL);
1120 * See if somebody else (such as the device core) has already
1121 * obtained a handle to the pinctrl for this device. In that case,
1122 * return another pointer to it.
1124 p = find_pinctrl(dev);
1125 if (p) {
1126 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
1127 kref_get(&p->users);
1128 return p;
1131 return create_pinctrl(dev, NULL);
1133 EXPORT_SYMBOL_GPL(pinctrl_get);
1135 static void pinctrl_free_setting(bool disable_setting,
1136 struct pinctrl_setting *setting)
1138 switch (setting->type) {
1139 case PIN_MAP_TYPE_MUX_GROUP:
1140 if (disable_setting)
1141 pinmux_disable_setting(setting);
1142 pinmux_free_setting(setting);
1143 break;
1144 case PIN_MAP_TYPE_CONFIGS_PIN:
1145 case PIN_MAP_TYPE_CONFIGS_GROUP:
1146 pinconf_free_setting(setting);
1147 break;
1148 default:
1149 break;
1153 static void pinctrl_free(struct pinctrl *p, bool inlist)
1155 struct pinctrl_state *state, *n1;
1156 struct pinctrl_setting *setting, *n2;
1158 mutex_lock(&pinctrl_list_mutex);
1159 list_for_each_entry_safe(state, n1, &p->states, node) {
1160 list_for_each_entry_safe(setting, n2, &state->settings, node) {
1161 pinctrl_free_setting(state == p->state, setting);
1162 list_del(&setting->node);
1163 kfree(setting);
1165 list_del(&state->node);
1166 kfree(state);
1169 pinctrl_dt_free_maps(p);
1171 if (inlist)
1172 list_del(&p->node);
1173 kfree(p);
1174 mutex_unlock(&pinctrl_list_mutex);
1178 * pinctrl_release() - release the pinctrl handle
1179 * @kref: the kref in the pinctrl being released
1181 static void pinctrl_release(struct kref *kref)
1183 struct pinctrl *p = container_of(kref, struct pinctrl, users);
1185 pinctrl_free(p, true);
1189 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
1190 * @p: the pinctrl handle to release
1192 void pinctrl_put(struct pinctrl *p)
1194 kref_put(&p->users, pinctrl_release);
1196 EXPORT_SYMBOL_GPL(pinctrl_put);
1199 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
1200 * @p: the pinctrl handle to retrieve the state from
1201 * @name: the state name to retrieve
1203 struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
1204 const char *name)
1206 struct pinctrl_state *state;
1208 state = find_state(p, name);
1209 if (!state) {
1210 if (pinctrl_dummy_state) {
1211 /* create dummy state */
1212 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
1213 name);
1214 state = create_state(p, name);
1215 } else
1216 state = ERR_PTR(-ENODEV);
1219 return state;
1221 EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
1223 static void pinctrl_link_add(struct pinctrl_dev *pctldev,
1224 struct device *consumer)
1226 if (pctldev->desc->link_consumers)
1227 device_link_add(consumer, pctldev->dev,
1228 DL_FLAG_PM_RUNTIME |
1229 DL_FLAG_AUTOREMOVE_CONSUMER);
1233 * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
1234 * @p: the pinctrl handle for the device that requests configuration
1235 * @state: the state handle to select/activate/program
1237 static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
1239 struct pinctrl_setting *setting, *setting2;
1240 struct pinctrl_state *old_state = p->state;
1241 int ret;
1243 if (p->state) {
1245 * For each pinmux setting in the old state, forget SW's record
1246 * of mux owner for that pingroup. Any pingroups which are
1247 * still owned by the new state will be re-acquired by the call
1248 * to pinmux_enable_setting() in the loop below.
1250 list_for_each_entry(setting, &p->state->settings, node) {
1251 if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
1252 continue;
1253 pinmux_disable_setting(setting);
1257 p->state = NULL;
1259 /* Apply all the settings for the new state */
1260 list_for_each_entry(setting, &state->settings, node) {
1261 switch (setting->type) {
1262 case PIN_MAP_TYPE_MUX_GROUP:
1263 ret = pinmux_enable_setting(setting);
1264 break;
1265 case PIN_MAP_TYPE_CONFIGS_PIN:
1266 case PIN_MAP_TYPE_CONFIGS_GROUP:
1267 ret = pinconf_apply_setting(setting);
1268 break;
1269 default:
1270 ret = -EINVAL;
1271 break;
1274 if (ret < 0) {
1275 goto unapply_new_state;
1278 /* Do not link hogs (circular dependency) */
1279 if (p != setting->pctldev->p)
1280 pinctrl_link_add(setting->pctldev, p->dev);
1283 p->state = state;
1285 return 0;
1287 unapply_new_state:
1288 dev_err(p->dev, "Error applying setting, reverse things back\n");
1290 list_for_each_entry(setting2, &state->settings, node) {
1291 if (&setting2->node == &setting->node)
1292 break;
1294 * All we can do here is pinmux_disable_setting.
1295 * That means that some pins are muxed differently now
1296 * than they were before applying the setting (We can't
1297 * "unmux a pin"!), but it's not a big deal since the pins
1298 * are free to be muxed by another apply_setting.
1300 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
1301 pinmux_disable_setting(setting2);
1304 /* There's no infinite recursive loop here because p->state is NULL */
1305 if (old_state)
1306 pinctrl_select_state(p, old_state);
1308 return ret;
1312 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
1313 * @p: the pinctrl handle for the device that requests configuration
1314 * @state: the state handle to select/activate/program
1316 int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
1318 if (p->state == state)
1319 return 0;
1321 return pinctrl_commit_state(p, state);
1323 EXPORT_SYMBOL_GPL(pinctrl_select_state);
1325 static void devm_pinctrl_release(struct device *dev, void *res)
1327 pinctrl_put(*(struct pinctrl **)res);
1331 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1332 * @dev: the device to obtain the handle for
1334 * If there is a need to explicitly destroy the returned struct pinctrl,
1335 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1337 struct pinctrl *devm_pinctrl_get(struct device *dev)
1339 struct pinctrl **ptr, *p;
1341 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
1342 if (!ptr)
1343 return ERR_PTR(-ENOMEM);
1345 p = pinctrl_get(dev);
1346 if (!IS_ERR(p)) {
1347 *ptr = p;
1348 devres_add(dev, ptr);
1349 } else {
1350 devres_free(ptr);
1353 return p;
1355 EXPORT_SYMBOL_GPL(devm_pinctrl_get);
1357 static int devm_pinctrl_match(struct device *dev, void *res, void *data)
1359 struct pinctrl **p = res;
1361 return *p == data;
1365 * devm_pinctrl_put() - Resource managed pinctrl_put()
1366 * @p: the pinctrl handle to release
1368 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1369 * this function will not need to be called and the resource management
1370 * code will ensure that the resource is freed.
1372 void devm_pinctrl_put(struct pinctrl *p)
1374 WARN_ON(devres_release(p->dev, devm_pinctrl_release,
1375 devm_pinctrl_match, p));
1377 EXPORT_SYMBOL_GPL(devm_pinctrl_put);
1380 * pinctrl_register_mappings() - register a set of pin controller mappings
1381 * @maps: the pincontrol mappings table to register. Note the pinctrl-core
1382 * keeps a reference to the passed in maps, so they should _not_ be
1383 * marked with __initdata.
1384 * @num_maps: the number of maps in the mapping table
1386 int pinctrl_register_mappings(const struct pinctrl_map *maps,
1387 unsigned num_maps)
1389 int i, ret;
1390 struct pinctrl_maps *maps_node;
1392 pr_debug("add %u pinctrl maps\n", num_maps);
1394 /* First sanity check the new mapping */
1395 for (i = 0; i < num_maps; i++) {
1396 if (!maps[i].dev_name) {
1397 pr_err("failed to register map %s (%d): no device given\n",
1398 maps[i].name, i);
1399 return -EINVAL;
1402 if (!maps[i].name) {
1403 pr_err("failed to register map %d: no map name given\n",
1405 return -EINVAL;
1408 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
1409 !maps[i].ctrl_dev_name) {
1410 pr_err("failed to register map %s (%d): no pin control device given\n",
1411 maps[i].name, i);
1412 return -EINVAL;
1415 switch (maps[i].type) {
1416 case PIN_MAP_TYPE_DUMMY_STATE:
1417 break;
1418 case PIN_MAP_TYPE_MUX_GROUP:
1419 ret = pinmux_validate_map(&maps[i], i);
1420 if (ret < 0)
1421 return ret;
1422 break;
1423 case PIN_MAP_TYPE_CONFIGS_PIN:
1424 case PIN_MAP_TYPE_CONFIGS_GROUP:
1425 ret = pinconf_validate_map(&maps[i], i);
1426 if (ret < 0)
1427 return ret;
1428 break;
1429 default:
1430 pr_err("failed to register map %s (%d): invalid type given\n",
1431 maps[i].name, i);
1432 return -EINVAL;
1436 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
1437 if (!maps_node)
1438 return -ENOMEM;
1440 maps_node->maps = maps;
1441 maps_node->num_maps = num_maps;
1443 mutex_lock(&pinctrl_maps_mutex);
1444 list_add_tail(&maps_node->node, &pinctrl_maps);
1445 mutex_unlock(&pinctrl_maps_mutex);
1447 return 0;
1449 EXPORT_SYMBOL_GPL(pinctrl_register_mappings);
1452 * pinctrl_unregister_mappings() - unregister a set of pin controller mappings
1453 * @maps: the pincontrol mappings table passed to pinctrl_register_mappings()
1454 * when registering the mappings.
1456 void pinctrl_unregister_mappings(const struct pinctrl_map *map)
1458 struct pinctrl_maps *maps_node;
1460 mutex_lock(&pinctrl_maps_mutex);
1461 list_for_each_entry(maps_node, &pinctrl_maps, node) {
1462 if (maps_node->maps == map) {
1463 list_del(&maps_node->node);
1464 kfree(maps_node);
1465 mutex_unlock(&pinctrl_maps_mutex);
1466 return;
1469 mutex_unlock(&pinctrl_maps_mutex);
1471 EXPORT_SYMBOL_GPL(pinctrl_unregister_mappings);
1474 * pinctrl_force_sleep() - turn a given controller device into sleep state
1475 * @pctldev: pin controller device
1477 int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
1479 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
1480 return pinctrl_commit_state(pctldev->p, pctldev->hog_sleep);
1481 return 0;
1483 EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
1486 * pinctrl_force_default() - turn a given controller device into default state
1487 * @pctldev: pin controller device
1489 int pinctrl_force_default(struct pinctrl_dev *pctldev)
1491 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
1492 return pinctrl_commit_state(pctldev->p, pctldev->hog_default);
1493 return 0;
1495 EXPORT_SYMBOL_GPL(pinctrl_force_default);
1498 * pinctrl_init_done() - tell pinctrl probe is done
1500 * We'll use this time to switch the pins from "init" to "default" unless the
1501 * driver selected some other state.
1503 * @dev: device to that's done probing
1505 int pinctrl_init_done(struct device *dev)
1507 struct dev_pin_info *pins = dev->pins;
1508 int ret;
1510 if (!pins)
1511 return 0;
1513 if (IS_ERR(pins->init_state))
1514 return 0; /* No such state */
1516 if (pins->p->state != pins->init_state)
1517 return 0; /* Not at init anyway */
1519 if (IS_ERR(pins->default_state))
1520 return 0; /* No default state */
1522 ret = pinctrl_select_state(pins->p, pins->default_state);
1523 if (ret)
1524 dev_err(dev, "failed to activate default pinctrl state\n");
1526 return ret;
1529 static int pinctrl_select_bound_state(struct device *dev,
1530 struct pinctrl_state *state)
1532 struct dev_pin_info *pins = dev->pins;
1533 int ret;
1535 if (IS_ERR(state))
1536 return 0; /* No such state */
1537 ret = pinctrl_select_state(pins->p, state);
1538 if (ret)
1539 dev_err(dev, "failed to activate pinctrl state %s\n",
1540 state->name);
1541 return ret;
1545 * pinctrl_select_default_state() - select default pinctrl state
1546 * @dev: device to select default state for
1548 int pinctrl_select_default_state(struct device *dev)
1550 if (!dev->pins)
1551 return 0;
1553 return pinctrl_select_bound_state(dev, dev->pins->default_state);
1555 EXPORT_SYMBOL_GPL(pinctrl_select_default_state);
1557 #ifdef CONFIG_PM
1560 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1561 * @dev: device to select default state for
1563 int pinctrl_pm_select_default_state(struct device *dev)
1565 return pinctrl_select_default_state(dev);
1567 EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
1570 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1571 * @dev: device to select sleep state for
1573 int pinctrl_pm_select_sleep_state(struct device *dev)
1575 if (!dev->pins)
1576 return 0;
1578 return pinctrl_select_bound_state(dev, dev->pins->sleep_state);
1580 EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
1583 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1584 * @dev: device to select idle state for
1586 int pinctrl_pm_select_idle_state(struct device *dev)
1588 if (!dev->pins)
1589 return 0;
1591 return pinctrl_select_bound_state(dev, dev->pins->idle_state);
1593 EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
1594 #endif
1596 #ifdef CONFIG_DEBUG_FS
1598 static int pinctrl_pins_show(struct seq_file *s, void *what)
1600 struct pinctrl_dev *pctldev = s->private;
1601 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1602 unsigned i, pin;
1604 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
1606 mutex_lock(&pctldev->mutex);
1608 /* The pin number can be retrived from the pin controller descriptor */
1609 for (i = 0; i < pctldev->desc->npins; i++) {
1610 struct pin_desc *desc;
1612 pin = pctldev->desc->pins[i].number;
1613 desc = pin_desc_get(pctldev, pin);
1614 /* Pin space may be sparse */
1615 if (!desc)
1616 continue;
1618 seq_printf(s, "pin %d (%s) ", pin, desc->name);
1620 /* Driver-specific info per pin */
1621 if (ops->pin_dbg_show)
1622 ops->pin_dbg_show(pctldev, s, pin);
1624 seq_puts(s, "\n");
1627 mutex_unlock(&pctldev->mutex);
1629 return 0;
1631 DEFINE_SHOW_ATTRIBUTE(pinctrl_pins);
1633 static int pinctrl_groups_show(struct seq_file *s, void *what)
1635 struct pinctrl_dev *pctldev = s->private;
1636 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1637 unsigned ngroups, selector = 0;
1639 mutex_lock(&pctldev->mutex);
1641 ngroups = ops->get_groups_count(pctldev);
1643 seq_puts(s, "registered pin groups:\n");
1644 while (selector < ngroups) {
1645 const unsigned *pins = NULL;
1646 unsigned num_pins = 0;
1647 const char *gname = ops->get_group_name(pctldev, selector);
1648 const char *pname;
1649 int ret = 0;
1650 int i;
1652 if (ops->get_group_pins)
1653 ret = ops->get_group_pins(pctldev, selector,
1654 &pins, &num_pins);
1655 if (ret)
1656 seq_printf(s, "%s [ERROR GETTING PINS]\n",
1657 gname);
1658 else {
1659 seq_printf(s, "group: %s\n", gname);
1660 for (i = 0; i < num_pins; i++) {
1661 pname = pin_get_name(pctldev, pins[i]);
1662 if (WARN_ON(!pname)) {
1663 mutex_unlock(&pctldev->mutex);
1664 return -EINVAL;
1666 seq_printf(s, "pin %d (%s)\n", pins[i], pname);
1668 seq_puts(s, "\n");
1670 selector++;
1673 mutex_unlock(&pctldev->mutex);
1675 return 0;
1677 DEFINE_SHOW_ATTRIBUTE(pinctrl_groups);
1679 static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
1681 struct pinctrl_dev *pctldev = s->private;
1682 struct pinctrl_gpio_range *range;
1684 seq_puts(s, "GPIO ranges handled:\n");
1686 mutex_lock(&pctldev->mutex);
1688 /* Loop over the ranges */
1689 list_for_each_entry(range, &pctldev->gpio_ranges, node) {
1690 if (range->pins) {
1691 int a;
1692 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
1693 range->id, range->name,
1694 range->base, (range->base + range->npins - 1));
1695 for (a = 0; a < range->npins - 1; a++)
1696 seq_printf(s, "%u, ", range->pins[a]);
1697 seq_printf(s, "%u}\n", range->pins[a]);
1699 else
1700 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1701 range->id, range->name,
1702 range->base, (range->base + range->npins - 1),
1703 range->pin_base,
1704 (range->pin_base + range->npins - 1));
1707 mutex_unlock(&pctldev->mutex);
1709 return 0;
1711 DEFINE_SHOW_ATTRIBUTE(pinctrl_gpioranges);
1713 static int pinctrl_devices_show(struct seq_file *s, void *what)
1715 struct pinctrl_dev *pctldev;
1717 seq_puts(s, "name [pinmux] [pinconf]\n");
1719 mutex_lock(&pinctrldev_list_mutex);
1721 list_for_each_entry(pctldev, &pinctrldev_list, node) {
1722 seq_printf(s, "%s ", pctldev->desc->name);
1723 if (pctldev->desc->pmxops)
1724 seq_puts(s, "yes ");
1725 else
1726 seq_puts(s, "no ");
1727 if (pctldev->desc->confops)
1728 seq_puts(s, "yes");
1729 else
1730 seq_puts(s, "no");
1731 seq_puts(s, "\n");
1734 mutex_unlock(&pinctrldev_list_mutex);
1736 return 0;
1738 DEFINE_SHOW_ATTRIBUTE(pinctrl_devices);
1740 static inline const char *map_type(enum pinctrl_map_type type)
1742 static const char * const names[] = {
1743 "INVALID",
1744 "DUMMY_STATE",
1745 "MUX_GROUP",
1746 "CONFIGS_PIN",
1747 "CONFIGS_GROUP",
1750 if (type >= ARRAY_SIZE(names))
1751 return "UNKNOWN";
1753 return names[type];
1756 static int pinctrl_maps_show(struct seq_file *s, void *what)
1758 struct pinctrl_maps *maps_node;
1759 int i;
1760 const struct pinctrl_map *map;
1762 seq_puts(s, "Pinctrl maps:\n");
1764 mutex_lock(&pinctrl_maps_mutex);
1765 for_each_maps(maps_node, i, map) {
1766 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
1767 map->dev_name, map->name, map_type(map->type),
1768 map->type);
1770 if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
1771 seq_printf(s, "controlling device %s\n",
1772 map->ctrl_dev_name);
1774 switch (map->type) {
1775 case PIN_MAP_TYPE_MUX_GROUP:
1776 pinmux_show_map(s, map);
1777 break;
1778 case PIN_MAP_TYPE_CONFIGS_PIN:
1779 case PIN_MAP_TYPE_CONFIGS_GROUP:
1780 pinconf_show_map(s, map);
1781 break;
1782 default:
1783 break;
1786 seq_putc(s, '\n');
1788 mutex_unlock(&pinctrl_maps_mutex);
1790 return 0;
1792 DEFINE_SHOW_ATTRIBUTE(pinctrl_maps);
1794 static int pinctrl_show(struct seq_file *s, void *what)
1796 struct pinctrl *p;
1797 struct pinctrl_state *state;
1798 struct pinctrl_setting *setting;
1800 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
1802 mutex_lock(&pinctrl_list_mutex);
1804 list_for_each_entry(p, &pinctrl_list, node) {
1805 seq_printf(s, "device: %s current state: %s\n",
1806 dev_name(p->dev),
1807 p->state ? p->state->name : "none");
1809 list_for_each_entry(state, &p->states, node) {
1810 seq_printf(s, " state: %s\n", state->name);
1812 list_for_each_entry(setting, &state->settings, node) {
1813 struct pinctrl_dev *pctldev = setting->pctldev;
1815 seq_printf(s, " type: %s controller %s ",
1816 map_type(setting->type),
1817 pinctrl_dev_get_name(pctldev));
1819 switch (setting->type) {
1820 case PIN_MAP_TYPE_MUX_GROUP:
1821 pinmux_show_setting(s, setting);
1822 break;
1823 case PIN_MAP_TYPE_CONFIGS_PIN:
1824 case PIN_MAP_TYPE_CONFIGS_GROUP:
1825 pinconf_show_setting(s, setting);
1826 break;
1827 default:
1828 break;
1834 mutex_unlock(&pinctrl_list_mutex);
1836 return 0;
1838 DEFINE_SHOW_ATTRIBUTE(pinctrl);
1840 static struct dentry *debugfs_root;
1842 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1844 struct dentry *device_root;
1845 const char *debugfs_name;
1847 if (pctldev->desc->name &&
1848 strcmp(dev_name(pctldev->dev), pctldev->desc->name)) {
1849 debugfs_name = devm_kasprintf(pctldev->dev, GFP_KERNEL,
1850 "%s-%s", dev_name(pctldev->dev),
1851 pctldev->desc->name);
1852 if (!debugfs_name) {
1853 pr_warn("failed to determine debugfs dir name for %s\n",
1854 dev_name(pctldev->dev));
1855 return;
1857 } else {
1858 debugfs_name = dev_name(pctldev->dev);
1861 device_root = debugfs_create_dir(debugfs_name, debugfs_root);
1862 pctldev->device_root = device_root;
1864 if (IS_ERR(device_root) || !device_root) {
1865 pr_warn("failed to create debugfs directory for %s\n",
1866 dev_name(pctldev->dev));
1867 return;
1869 debugfs_create_file("pins", S_IFREG | S_IRUGO,
1870 device_root, pctldev, &pinctrl_pins_fops);
1871 debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
1872 device_root, pctldev, &pinctrl_groups_fops);
1873 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
1874 device_root, pctldev, &pinctrl_gpioranges_fops);
1875 if (pctldev->desc->pmxops)
1876 pinmux_init_device_debugfs(device_root, pctldev);
1877 if (pctldev->desc->confops)
1878 pinconf_init_device_debugfs(device_root, pctldev);
1881 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1883 debugfs_remove_recursive(pctldev->device_root);
1886 static void pinctrl_init_debugfs(void)
1888 debugfs_root = debugfs_create_dir("pinctrl", NULL);
1889 if (IS_ERR(debugfs_root) || !debugfs_root) {
1890 pr_warn("failed to create debugfs directory\n");
1891 debugfs_root = NULL;
1892 return;
1895 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
1896 debugfs_root, NULL, &pinctrl_devices_fops);
1897 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1898 debugfs_root, NULL, &pinctrl_maps_fops);
1899 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1900 debugfs_root, NULL, &pinctrl_fops);
1903 #else /* CONFIG_DEBUG_FS */
1905 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
1909 static void pinctrl_init_debugfs(void)
1913 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
1917 #endif
1919 static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
1921 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1923 if (!ops ||
1924 !ops->get_groups_count ||
1925 !ops->get_group_name)
1926 return -EINVAL;
1928 return 0;
1932 * pinctrl_init_controller() - init a pin controller device
1933 * @pctldesc: descriptor for this pin controller
1934 * @dev: parent device for this pin controller
1935 * @driver_data: private pin controller data for this pin controller
1937 static struct pinctrl_dev *
1938 pinctrl_init_controller(struct pinctrl_desc *pctldesc, struct device *dev,
1939 void *driver_data)
1941 struct pinctrl_dev *pctldev;
1942 int ret;
1944 if (!pctldesc)
1945 return ERR_PTR(-EINVAL);
1946 if (!pctldesc->name)
1947 return ERR_PTR(-EINVAL);
1949 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
1950 if (!pctldev)
1951 return ERR_PTR(-ENOMEM);
1953 /* Initialize pin control device struct */
1954 pctldev->owner = pctldesc->owner;
1955 pctldev->desc = pctldesc;
1956 pctldev->driver_data = driver_data;
1957 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
1958 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
1959 INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL);
1960 #endif
1961 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
1962 INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL);
1963 #endif
1964 INIT_LIST_HEAD(&pctldev->gpio_ranges);
1965 INIT_LIST_HEAD(&pctldev->node);
1966 pctldev->dev = dev;
1967 mutex_init(&pctldev->mutex);
1969 /* check core ops for sanity */
1970 ret = pinctrl_check_ops(pctldev);
1971 if (ret) {
1972 dev_err(dev, "pinctrl ops lacks necessary functions\n");
1973 goto out_err;
1976 /* If we're implementing pinmuxing, check the ops for sanity */
1977 if (pctldesc->pmxops) {
1978 ret = pinmux_check_ops(pctldev);
1979 if (ret)
1980 goto out_err;
1983 /* If we're implementing pinconfig, check the ops for sanity */
1984 if (pctldesc->confops) {
1985 ret = pinconf_check_ops(pctldev);
1986 if (ret)
1987 goto out_err;
1990 /* Register all the pins */
1991 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
1992 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
1993 if (ret) {
1994 dev_err(dev, "error during pin registration\n");
1995 pinctrl_free_pindescs(pctldev, pctldesc->pins,
1996 pctldesc->npins);
1997 goto out_err;
2000 return pctldev;
2002 out_err:
2003 mutex_destroy(&pctldev->mutex);
2004 kfree(pctldev);
2005 return ERR_PTR(ret);
2008 static int pinctrl_claim_hogs(struct pinctrl_dev *pctldev)
2010 pctldev->p = create_pinctrl(pctldev->dev, pctldev);
2011 if (PTR_ERR(pctldev->p) == -ENODEV) {
2012 dev_dbg(pctldev->dev, "no hogs found\n");
2014 return 0;
2017 if (IS_ERR(pctldev->p)) {
2018 dev_err(pctldev->dev, "error claiming hogs: %li\n",
2019 PTR_ERR(pctldev->p));
2021 return PTR_ERR(pctldev->p);
2024 kref_get(&pctldev->p->users);
2025 pctldev->hog_default =
2026 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
2027 if (IS_ERR(pctldev->hog_default)) {
2028 dev_dbg(pctldev->dev,
2029 "failed to lookup the default state\n");
2030 } else {
2031 if (pinctrl_select_state(pctldev->p,
2032 pctldev->hog_default))
2033 dev_err(pctldev->dev,
2034 "failed to select default state\n");
2037 pctldev->hog_sleep =
2038 pinctrl_lookup_state(pctldev->p,
2039 PINCTRL_STATE_SLEEP);
2040 if (IS_ERR(pctldev->hog_sleep))
2041 dev_dbg(pctldev->dev,
2042 "failed to lookup the sleep state\n");
2044 return 0;
2047 int pinctrl_enable(struct pinctrl_dev *pctldev)
2049 int error;
2051 error = pinctrl_claim_hogs(pctldev);
2052 if (error) {
2053 dev_err(pctldev->dev, "could not claim hogs: %i\n",
2054 error);
2055 mutex_destroy(&pctldev->mutex);
2056 kfree(pctldev);
2058 return error;
2061 mutex_lock(&pinctrldev_list_mutex);
2062 list_add_tail(&pctldev->node, &pinctrldev_list);
2063 mutex_unlock(&pinctrldev_list_mutex);
2065 pinctrl_init_device_debugfs(pctldev);
2067 return 0;
2069 EXPORT_SYMBOL_GPL(pinctrl_enable);
2072 * pinctrl_register() - register a pin controller device
2073 * @pctldesc: descriptor for this pin controller
2074 * @dev: parent device for this pin controller
2075 * @driver_data: private pin controller data for this pin controller
2077 * Note that pinctrl_register() is known to have problems as the pin
2078 * controller driver functions are called before the driver has a
2079 * struct pinctrl_dev handle. To avoid issues later on, please use the
2080 * new pinctrl_register_and_init() below instead.
2082 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
2083 struct device *dev, void *driver_data)
2085 struct pinctrl_dev *pctldev;
2086 int error;
2088 pctldev = pinctrl_init_controller(pctldesc, dev, driver_data);
2089 if (IS_ERR(pctldev))
2090 return pctldev;
2092 error = pinctrl_enable(pctldev);
2093 if (error)
2094 return ERR_PTR(error);
2096 return pctldev;
2099 EXPORT_SYMBOL_GPL(pinctrl_register);
2102 * pinctrl_register_and_init() - register and init pin controller device
2103 * @pctldesc: descriptor for this pin controller
2104 * @dev: parent device for this pin controller
2105 * @driver_data: private pin controller data for this pin controller
2106 * @pctldev: pin controller device
2108 * Note that pinctrl_enable() still needs to be manually called after
2109 * this once the driver is ready.
2111 int pinctrl_register_and_init(struct pinctrl_desc *pctldesc,
2112 struct device *dev, void *driver_data,
2113 struct pinctrl_dev **pctldev)
2115 struct pinctrl_dev *p;
2117 p = pinctrl_init_controller(pctldesc, dev, driver_data);
2118 if (IS_ERR(p))
2119 return PTR_ERR(p);
2122 * We have pinctrl_start() call functions in the pin controller
2123 * driver with create_pinctrl() for at least dt_node_to_map(). So
2124 * let's make sure pctldev is properly initialized for the
2125 * pin controller driver before we do anything.
2127 *pctldev = p;
2129 return 0;
2131 EXPORT_SYMBOL_GPL(pinctrl_register_and_init);
2134 * pinctrl_unregister() - unregister pinmux
2135 * @pctldev: pin controller to unregister
2137 * Called by pinmux drivers to unregister a pinmux.
2139 void pinctrl_unregister(struct pinctrl_dev *pctldev)
2141 struct pinctrl_gpio_range *range, *n;
2143 if (!pctldev)
2144 return;
2146 mutex_lock(&pctldev->mutex);
2147 pinctrl_remove_device_debugfs(pctldev);
2148 mutex_unlock(&pctldev->mutex);
2150 if (!IS_ERR_OR_NULL(pctldev->p))
2151 pinctrl_put(pctldev->p);
2153 mutex_lock(&pinctrldev_list_mutex);
2154 mutex_lock(&pctldev->mutex);
2155 /* TODO: check that no pinmuxes are still active? */
2156 list_del(&pctldev->node);
2157 pinmux_generic_free_functions(pctldev);
2158 pinctrl_generic_free_groups(pctldev);
2159 /* Destroy descriptor tree */
2160 pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
2161 pctldev->desc->npins);
2162 /* remove gpio ranges map */
2163 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
2164 list_del(&range->node);
2166 mutex_unlock(&pctldev->mutex);
2167 mutex_destroy(&pctldev->mutex);
2168 kfree(pctldev);
2169 mutex_unlock(&pinctrldev_list_mutex);
2171 EXPORT_SYMBOL_GPL(pinctrl_unregister);
2173 static void devm_pinctrl_dev_release(struct device *dev, void *res)
2175 struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
2177 pinctrl_unregister(pctldev);
2180 static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
2182 struct pctldev **r = res;
2184 if (WARN_ON(!r || !*r))
2185 return 0;
2187 return *r == data;
2191 * devm_pinctrl_register() - Resource managed version of pinctrl_register().
2192 * @dev: parent device for this pin controller
2193 * @pctldesc: descriptor for this pin controller
2194 * @driver_data: private pin controller data for this pin controller
2196 * Returns an error pointer if pincontrol register failed. Otherwise
2197 * it returns valid pinctrl handle.
2199 * The pinctrl device will be automatically released when the device is unbound.
2201 struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
2202 struct pinctrl_desc *pctldesc,
2203 void *driver_data)
2205 struct pinctrl_dev **ptr, *pctldev;
2207 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2208 if (!ptr)
2209 return ERR_PTR(-ENOMEM);
2211 pctldev = pinctrl_register(pctldesc, dev, driver_data);
2212 if (IS_ERR(pctldev)) {
2213 devres_free(ptr);
2214 return pctldev;
2217 *ptr = pctldev;
2218 devres_add(dev, ptr);
2220 return pctldev;
2222 EXPORT_SYMBOL_GPL(devm_pinctrl_register);
2225 * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
2226 * @dev: parent device for this pin controller
2227 * @pctldesc: descriptor for this pin controller
2228 * @driver_data: private pin controller data for this pin controller
2230 * Returns an error pointer if pincontrol register failed. Otherwise
2231 * it returns valid pinctrl handle.
2233 * The pinctrl device will be automatically released when the device is unbound.
2235 int devm_pinctrl_register_and_init(struct device *dev,
2236 struct pinctrl_desc *pctldesc,
2237 void *driver_data,
2238 struct pinctrl_dev **pctldev)
2240 struct pinctrl_dev **ptr;
2241 int error;
2243 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
2244 if (!ptr)
2245 return -ENOMEM;
2247 error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev);
2248 if (error) {
2249 devres_free(ptr);
2250 return error;
2253 *ptr = *pctldev;
2254 devres_add(dev, ptr);
2256 return 0;
2258 EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init);
2261 * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
2262 * @dev: device for which which resource was allocated
2263 * @pctldev: the pinctrl device to unregister.
2265 void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
2267 WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
2268 devm_pinctrl_dev_match, pctldev));
2270 EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
2272 static int __init pinctrl_init(void)
2274 pr_info("initialized pinctrl subsystem\n");
2275 pinctrl_init_debugfs();
2276 return 0;
2279 /* init early since many drivers really need to initialized pinmux early */
2280 core_initcall(pinctrl_init);