1 // SPDX-License-Identifier: GPL-2.0-only
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>
30 #include "../gpio/gpiolib.h"
31 #include <asm-generic/gpio.h>
35 #include "devicetree.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
);
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
);
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
;
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
);
117 mutex_unlock(&pinctrldev_list_mutex
);
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 (pctldev
->dev
->of_node
== np
) {
130 mutex_unlock(&pinctrldev_list_mutex
);
134 mutex_unlock(&pinctrldev_list_mutex
);
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
)
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
))
163 * pin_get_name_from_id() - look up a pin name from a pin id
164 * @pctldev: the pin control device to lookup the pin on
165 * @pin: pin number/id to look up
167 const char *pin_get_name(struct pinctrl_dev
*pctldev
, const unsigned pin
)
169 const struct pin_desc
*desc
;
171 desc
= pin_desc_get(pctldev
, pin
);
173 dev_err(pctldev
->dev
, "failed to get pin(%d) name\n",
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
,
189 for (i
= 0; i
< num_pins
; i
++) {
190 struct pin_desc
*pindesc
;
192 pindesc
= radix_tree_lookup(&pctldev
->pin_desc_tree
,
195 radix_tree_delete(&pctldev
->pin_desc_tree
,
197 if (pindesc
->dynamic_name
)
198 kfree(pindesc
->name
);
204 static int pinctrl_register_one_pin(struct pinctrl_dev
*pctldev
,
205 const struct pinctrl_pin_desc
*pin
)
207 struct pin_desc
*pindesc
;
209 pindesc
= pin_desc_get(pctldev
, pin
->number
);
211 dev_err(pctldev
->dev
, "pin %d already registered\n",
216 pindesc
= kzalloc(sizeof(*pindesc
), GFP_KERNEL
);
221 pindesc
->pctldev
= pctldev
;
223 /* Copy basic pin info */
225 pindesc
->name
= pin
->name
;
227 pindesc
->name
= kasprintf(GFP_KERNEL
, "PIN%u", pin
->number
);
228 if (!pindesc
->name
) {
232 pindesc
->dynamic_name
= true;
235 pindesc
->drv_data
= pin
->drv_data
;
237 radix_tree_insert(&pctldev
->pin_desc_tree
, pin
->number
, pindesc
);
238 pr_debug("registered pin %d (%s) on %s\n",
239 pin
->number
, pindesc
->name
, pctldev
->desc
->name
);
243 static int pinctrl_register_pins(struct pinctrl_dev
*pctldev
,
244 const struct pinctrl_pin_desc
*pins
,
250 for (i
= 0; i
< num_descs
; i
++) {
251 ret
= pinctrl_register_one_pin(pctldev
, &pins
[i
]);
260 * gpio_to_pin() - GPIO range GPIO number to pin number translation
261 * @range: GPIO range used for the translation
262 * @gpio: gpio pin to translate to a pin number
264 * Finds the pin number for a given GPIO using the specified GPIO range
265 * as a base for translation. The distinction between linear GPIO ranges
266 * and pin list based GPIO ranges is managed correctly by this function.
268 * This function assumes the gpio is part of the specified GPIO range, use
269 * only after making sure this is the case (e.g. by calling it on the
270 * result of successful pinctrl_get_device_gpio_range calls)!
272 static inline int gpio_to_pin(struct pinctrl_gpio_range
*range
,
275 unsigned int offset
= gpio
- range
->base
;
277 return range
->pins
[offset
];
279 return range
->pin_base
+ offset
;
283 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
284 * @pctldev: pin controller device to check
285 * @gpio: gpio pin to check taken from the global GPIO pin space
287 * Tries to match a GPIO pin number to the ranges handled by a certain pin
288 * controller, return the range or NULL
290 static struct pinctrl_gpio_range
*
291 pinctrl_match_gpio_range(struct pinctrl_dev
*pctldev
, unsigned gpio
)
293 struct pinctrl_gpio_range
*range
;
295 mutex_lock(&pctldev
->mutex
);
296 /* Loop over the ranges */
297 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
298 /* Check if we're in the valid range */
299 if (gpio
>= range
->base
&&
300 gpio
< range
->base
+ range
->npins
) {
301 mutex_unlock(&pctldev
->mutex
);
305 mutex_unlock(&pctldev
->mutex
);
310 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
311 * the same GPIO chip are in range
312 * @gpio: gpio pin to check taken from the global GPIO pin space
314 * This function is complement of pinctrl_match_gpio_range(). If the return
315 * value of pinctrl_match_gpio_range() is NULL, this function could be used
316 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
317 * of the same GPIO chip don't have back-end pinctrl interface.
318 * If the return value is true, it means that pinctrl device is ready & the
319 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
320 * is false, it means that pinctrl device may not be ready.
322 #ifdef CONFIG_GPIOLIB
323 static bool pinctrl_ready_for_gpio_range(unsigned gpio
)
325 struct pinctrl_dev
*pctldev
;
326 struct pinctrl_gpio_range
*range
= NULL
;
327 struct gpio_chip
*chip
= gpio_to_chip(gpio
);
329 if (WARN(!chip
, "no gpio_chip for gpio%i?", gpio
))
332 mutex_lock(&pinctrldev_list_mutex
);
334 /* Loop over the pin controllers */
335 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
336 /* Loop over the ranges */
337 mutex_lock(&pctldev
->mutex
);
338 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
339 /* Check if any gpio range overlapped with gpio chip */
340 if (range
->base
+ range
->npins
- 1 < chip
->base
||
341 range
->base
> chip
->base
+ chip
->ngpio
- 1)
343 mutex_unlock(&pctldev
->mutex
);
344 mutex_unlock(&pinctrldev_list_mutex
);
347 mutex_unlock(&pctldev
->mutex
);
350 mutex_unlock(&pinctrldev_list_mutex
);
355 static bool pinctrl_ready_for_gpio_range(unsigned gpio
) { return true; }
359 * pinctrl_get_device_gpio_range() - find device for GPIO range
360 * @gpio: the pin to locate the pin controller for
361 * @outdev: the pin control device if found
362 * @outrange: the GPIO range if found
364 * Find the pin controller handling a certain GPIO pin from the pinspace of
365 * the GPIO subsystem, return the device and the matching GPIO range. Returns
366 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
367 * may still have not been registered.
369 static int pinctrl_get_device_gpio_range(unsigned gpio
,
370 struct pinctrl_dev
**outdev
,
371 struct pinctrl_gpio_range
**outrange
)
373 struct pinctrl_dev
*pctldev
;
375 mutex_lock(&pinctrldev_list_mutex
);
377 /* Loop over the pin controllers */
378 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
379 struct pinctrl_gpio_range
*range
;
381 range
= pinctrl_match_gpio_range(pctldev
, gpio
);
385 mutex_unlock(&pinctrldev_list_mutex
);
390 mutex_unlock(&pinctrldev_list_mutex
);
392 return -EPROBE_DEFER
;
396 * pinctrl_add_gpio_range() - register a GPIO range for a controller
397 * @pctldev: pin controller device to add the range to
398 * @range: the GPIO range to add
400 * This adds a range of GPIOs to be handled by a certain pin controller. Call
401 * this to register handled ranges after registering your pin controller.
403 void pinctrl_add_gpio_range(struct pinctrl_dev
*pctldev
,
404 struct pinctrl_gpio_range
*range
)
406 mutex_lock(&pctldev
->mutex
);
407 list_add_tail(&range
->node
, &pctldev
->gpio_ranges
);
408 mutex_unlock(&pctldev
->mutex
);
410 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range
);
412 void pinctrl_add_gpio_ranges(struct pinctrl_dev
*pctldev
,
413 struct pinctrl_gpio_range
*ranges
,
418 for (i
= 0; i
< nranges
; i
++)
419 pinctrl_add_gpio_range(pctldev
, &ranges
[i
]);
421 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges
);
423 struct pinctrl_dev
*pinctrl_find_and_add_gpio_range(const char *devname
,
424 struct pinctrl_gpio_range
*range
)
426 struct pinctrl_dev
*pctldev
;
428 pctldev
= get_pinctrl_dev_from_devname(devname
);
431 * If we can't find this device, let's assume that is because
432 * it has not probed yet, so the driver trying to register this
433 * range need to defer probing.
436 return ERR_PTR(-EPROBE_DEFER
);
438 pinctrl_add_gpio_range(pctldev
, range
);
442 EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range
);
444 int pinctrl_get_group_pins(struct pinctrl_dev
*pctldev
, const char *pin_group
,
445 const unsigned **pins
, unsigned *num_pins
)
447 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
450 if (!pctlops
->get_group_pins
)
453 gs
= pinctrl_get_group_selector(pctldev
, pin_group
);
457 return pctlops
->get_group_pins(pctldev
, gs
, pins
, num_pins
);
459 EXPORT_SYMBOL_GPL(pinctrl_get_group_pins
);
461 struct pinctrl_gpio_range
*
462 pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev
*pctldev
,
465 struct pinctrl_gpio_range
*range
;
467 /* Loop over the ranges */
468 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
469 /* Check if we're in the valid range */
472 for (a
= 0; a
< range
->npins
; a
++) {
473 if (range
->pins
[a
] == pin
)
476 } else if (pin
>= range
->pin_base
&&
477 pin
< range
->pin_base
+ range
->npins
)
483 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock
);
486 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
487 * @pctldev: the pin controller device to look in
488 * @pin: a controller-local number to find the range for
490 struct pinctrl_gpio_range
*
491 pinctrl_find_gpio_range_from_pin(struct pinctrl_dev
*pctldev
,
494 struct pinctrl_gpio_range
*range
;
496 mutex_lock(&pctldev
->mutex
);
497 range
= pinctrl_find_gpio_range_from_pin_nolock(pctldev
, pin
);
498 mutex_unlock(&pctldev
->mutex
);
502 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin
);
505 * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller
506 * @pctldev: pin controller device to remove the range from
507 * @range: the GPIO range to remove
509 void pinctrl_remove_gpio_range(struct pinctrl_dev
*pctldev
,
510 struct pinctrl_gpio_range
*range
)
512 mutex_lock(&pctldev
->mutex
);
513 list_del(&range
->node
);
514 mutex_unlock(&pctldev
->mutex
);
516 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range
);
518 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
521 * pinctrl_generic_get_group_count() - returns the number of pin groups
522 * @pctldev: pin controller device
524 int pinctrl_generic_get_group_count(struct pinctrl_dev
*pctldev
)
526 return pctldev
->num_groups
;
528 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count
);
531 * pinctrl_generic_get_group_name() - returns the name of a pin group
532 * @pctldev: pin controller device
533 * @selector: group number
535 const char *pinctrl_generic_get_group_name(struct pinctrl_dev
*pctldev
,
536 unsigned int selector
)
538 struct group_desc
*group
;
540 group
= radix_tree_lookup(&pctldev
->pin_group_tree
,
547 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name
);
550 * pinctrl_generic_get_group_pins() - gets the pin group pins
551 * @pctldev: pin controller device
552 * @selector: group number
553 * @pins: pins in the group
554 * @num_pins: number of pins in the group
556 int pinctrl_generic_get_group_pins(struct pinctrl_dev
*pctldev
,
557 unsigned int selector
,
558 const unsigned int **pins
,
559 unsigned int *num_pins
)
561 struct group_desc
*group
;
563 group
= radix_tree_lookup(&pctldev
->pin_group_tree
,
566 dev_err(pctldev
->dev
, "%s could not find pingroup%i\n",
572 *num_pins
= group
->num_pins
;
576 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins
);
579 * pinctrl_generic_get_group() - returns a pin group based on the number
580 * @pctldev: pin controller device
581 * @selector: group number
583 struct group_desc
*pinctrl_generic_get_group(struct pinctrl_dev
*pctldev
,
584 unsigned int selector
)
586 struct group_desc
*group
;
588 group
= radix_tree_lookup(&pctldev
->pin_group_tree
,
595 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group
);
597 static int pinctrl_generic_group_name_to_selector(struct pinctrl_dev
*pctldev
,
598 const char *function
)
600 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
601 int ngroups
= ops
->get_groups_count(pctldev
);
604 /* See if this pctldev has this group */
605 while (selector
< ngroups
) {
606 const char *gname
= ops
->get_group_name(pctldev
, selector
);
608 if (gname
&& !strcmp(function
, gname
))
618 * pinctrl_generic_add_group() - adds a new pin group
619 * @pctldev: pin controller device
620 * @name: name of the pin group
621 * @pins: pins in the pin group
622 * @num_pins: number of pins in the pin group
623 * @data: pin controller driver specific data
625 * Note that the caller must take care of locking.
627 int pinctrl_generic_add_group(struct pinctrl_dev
*pctldev
, const char *name
,
628 int *pins
, int num_pins
, void *data
)
630 struct group_desc
*group
;
636 selector
= pinctrl_generic_group_name_to_selector(pctldev
, name
);
640 selector
= pctldev
->num_groups
;
642 group
= devm_kzalloc(pctldev
->dev
, sizeof(*group
), GFP_KERNEL
);
648 group
->num_pins
= num_pins
;
651 radix_tree_insert(&pctldev
->pin_group_tree
, selector
, group
);
653 pctldev
->num_groups
++;
657 EXPORT_SYMBOL_GPL(pinctrl_generic_add_group
);
660 * pinctrl_generic_remove_group() - removes a numbered pin group
661 * @pctldev: pin controller device
662 * @selector: group number
664 * Note that the caller must take care of locking.
666 int pinctrl_generic_remove_group(struct pinctrl_dev
*pctldev
,
667 unsigned int selector
)
669 struct group_desc
*group
;
671 group
= radix_tree_lookup(&pctldev
->pin_group_tree
,
676 radix_tree_delete(&pctldev
->pin_group_tree
, selector
);
677 devm_kfree(pctldev
->dev
, group
);
679 pctldev
->num_groups
--;
683 EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group
);
686 * pinctrl_generic_free_groups() - removes all pin groups
687 * @pctldev: pin controller device
689 * Note that the caller must take care of locking. The pinctrl groups
690 * are allocated with devm_kzalloc() so no need to free them here.
692 static void pinctrl_generic_free_groups(struct pinctrl_dev
*pctldev
)
694 struct radix_tree_iter iter
;
697 radix_tree_for_each_slot(slot
, &pctldev
->pin_group_tree
, &iter
, 0)
698 radix_tree_delete(&pctldev
->pin_group_tree
, iter
.index
);
700 pctldev
->num_groups
= 0;
704 static inline void pinctrl_generic_free_groups(struct pinctrl_dev
*pctldev
)
707 #endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
710 * pinctrl_get_group_selector() - returns the group selector for a group
711 * @pctldev: the pin controller handling the group
712 * @pin_group: the pin group to look up
714 int pinctrl_get_group_selector(struct pinctrl_dev
*pctldev
,
715 const char *pin_group
)
717 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
718 unsigned ngroups
= pctlops
->get_groups_count(pctldev
);
719 unsigned group_selector
= 0;
721 while (group_selector
< ngroups
) {
722 const char *gname
= pctlops
->get_group_name(pctldev
,
724 if (gname
&& !strcmp(gname
, pin_group
)) {
725 dev_dbg(pctldev
->dev
,
726 "found group selector %u for %s\n",
729 return group_selector
;
735 dev_err(pctldev
->dev
, "does not have pin group %s\n",
741 bool pinctrl_gpio_can_use_line(unsigned gpio
)
743 struct pinctrl_dev
*pctldev
;
744 struct pinctrl_gpio_range
*range
;
749 * Try to obtain GPIO range, if it fails
750 * we're probably dealing with GPIO driver
751 * without a backing pin controller - bail out.
753 if (pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
))
756 mutex_lock(&pctldev
->mutex
);
758 /* Convert to the pin controllers number space */
759 pin
= gpio_to_pin(range
, gpio
);
761 result
= pinmux_can_be_used_for_gpio(pctldev
, pin
);
763 mutex_unlock(&pctldev
->mutex
);
767 EXPORT_SYMBOL_GPL(pinctrl_gpio_can_use_line
);
770 * pinctrl_gpio_request() - request a single pin to be used as GPIO
771 * @gpio: the GPIO pin number from the GPIO subsystem number space
773 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
774 * as part of their gpio_request() semantics, platforms and individual drivers
775 * shall *NOT* request GPIO pins to be muxed in.
777 int pinctrl_gpio_request(unsigned gpio
)
779 struct pinctrl_dev
*pctldev
;
780 struct pinctrl_gpio_range
*range
;
784 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
786 if (pinctrl_ready_for_gpio_range(gpio
))
791 mutex_lock(&pctldev
->mutex
);
793 /* Convert to the pin controllers number space */
794 pin
= gpio_to_pin(range
, gpio
);
796 ret
= pinmux_request_gpio(pctldev
, range
, pin
, gpio
);
798 mutex_unlock(&pctldev
->mutex
);
802 EXPORT_SYMBOL_GPL(pinctrl_gpio_request
);
805 * pinctrl_gpio_free() - free control on a single pin, currently used as GPIO
806 * @gpio: the GPIO pin number from the GPIO subsystem number space
808 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
809 * as part of their gpio_free() semantics, platforms and individual drivers
810 * shall *NOT* request GPIO pins to be muxed out.
812 void pinctrl_gpio_free(unsigned gpio
)
814 struct pinctrl_dev
*pctldev
;
815 struct pinctrl_gpio_range
*range
;
819 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
823 mutex_lock(&pctldev
->mutex
);
825 /* Convert to the pin controllers number space */
826 pin
= gpio_to_pin(range
, gpio
);
828 pinmux_free_gpio(pctldev
, pin
, range
);
830 mutex_unlock(&pctldev
->mutex
);
832 EXPORT_SYMBOL_GPL(pinctrl_gpio_free
);
834 static int pinctrl_gpio_direction(unsigned gpio
, bool input
)
836 struct pinctrl_dev
*pctldev
;
837 struct pinctrl_gpio_range
*range
;
841 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
846 mutex_lock(&pctldev
->mutex
);
848 /* Convert to the pin controllers number space */
849 pin
= gpio_to_pin(range
, gpio
);
850 ret
= pinmux_gpio_direction(pctldev
, range
, pin
, input
);
852 mutex_unlock(&pctldev
->mutex
);
858 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
859 * @gpio: the GPIO pin number from the GPIO subsystem number space
861 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
862 * as part of their gpio_direction_input() semantics, platforms and individual
863 * drivers shall *NOT* touch pin control GPIO calls.
865 int pinctrl_gpio_direction_input(unsigned gpio
)
867 return pinctrl_gpio_direction(gpio
, true);
869 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input
);
872 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
873 * @gpio: the GPIO pin number from the GPIO subsystem number space
875 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
876 * as part of their gpio_direction_output() semantics, platforms and individual
877 * drivers shall *NOT* touch pin control GPIO calls.
879 int pinctrl_gpio_direction_output(unsigned gpio
)
881 return pinctrl_gpio_direction(gpio
, false);
883 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output
);
886 * pinctrl_gpio_set_config() - Apply config to given GPIO pin
887 * @gpio: the GPIO pin number from the GPIO subsystem number space
888 * @config: the configuration to apply to the GPIO
890 * This function should *ONLY* be used from gpiolib-based GPIO drivers, if
891 * they need to call the underlying pin controller to change GPIO config
892 * (for example set debounce time).
894 int pinctrl_gpio_set_config(unsigned gpio
, unsigned long config
)
896 unsigned long configs
[] = { config
};
897 struct pinctrl_gpio_range
*range
;
898 struct pinctrl_dev
*pctldev
;
901 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
905 mutex_lock(&pctldev
->mutex
);
906 pin
= gpio_to_pin(range
, gpio
);
907 ret
= pinconf_set_config(pctldev
, pin
, configs
, ARRAY_SIZE(configs
));
908 mutex_unlock(&pctldev
->mutex
);
912 EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config
);
914 static struct pinctrl_state
*find_state(struct pinctrl
*p
,
917 struct pinctrl_state
*state
;
919 list_for_each_entry(state
, &p
->states
, node
)
920 if (!strcmp(state
->name
, name
))
926 static struct pinctrl_state
*create_state(struct pinctrl
*p
,
929 struct pinctrl_state
*state
;
931 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
933 return ERR_PTR(-ENOMEM
);
936 INIT_LIST_HEAD(&state
->settings
);
938 list_add_tail(&state
->node
, &p
->states
);
943 static int add_setting(struct pinctrl
*p
, struct pinctrl_dev
*pctldev
,
944 const struct pinctrl_map
*map
)
946 struct pinctrl_state
*state
;
947 struct pinctrl_setting
*setting
;
950 state
= find_state(p
, map
->name
);
952 state
= create_state(p
, map
->name
);
954 return PTR_ERR(state
);
956 if (map
->type
== PIN_MAP_TYPE_DUMMY_STATE
)
959 setting
= kzalloc(sizeof(*setting
), GFP_KERNEL
);
963 setting
->type
= map
->type
;
966 setting
->pctldev
= pctldev
;
969 get_pinctrl_dev_from_devname(map
->ctrl_dev_name
);
970 if (!setting
->pctldev
) {
972 /* Do not defer probing of hogs (circular loop) */
973 if (!strcmp(map
->ctrl_dev_name
, map
->dev_name
))
976 * OK let us guess that the driver is not there yet, and
977 * let's defer obtaining this pinctrl handle to later...
979 dev_info(p
->dev
, "unknown pinctrl device %s in map entry, deferring probe",
981 return -EPROBE_DEFER
;
984 setting
->dev_name
= map
->dev_name
;
987 case PIN_MAP_TYPE_MUX_GROUP
:
988 ret
= pinmux_map_to_setting(map
, setting
);
990 case PIN_MAP_TYPE_CONFIGS_PIN
:
991 case PIN_MAP_TYPE_CONFIGS_GROUP
:
992 ret
= pinconf_map_to_setting(map
, setting
);
1003 list_add_tail(&setting
->node
, &state
->settings
);
1008 static struct pinctrl
*find_pinctrl(struct device
*dev
)
1012 mutex_lock(&pinctrl_list_mutex
);
1013 list_for_each_entry(p
, &pinctrl_list
, node
)
1014 if (p
->dev
== dev
) {
1015 mutex_unlock(&pinctrl_list_mutex
);
1019 mutex_unlock(&pinctrl_list_mutex
);
1023 static void pinctrl_free(struct pinctrl
*p
, bool inlist
);
1025 static struct pinctrl
*create_pinctrl(struct device
*dev
,
1026 struct pinctrl_dev
*pctldev
)
1029 const char *devname
;
1030 struct pinctrl_maps
*maps_node
;
1032 const struct pinctrl_map
*map
;
1036 * create the state cookie holder struct pinctrl for each
1037 * mapping, this is what consumers will get when requesting
1038 * a pin control handle with pinctrl_get()
1040 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
1042 return ERR_PTR(-ENOMEM
);
1044 INIT_LIST_HEAD(&p
->states
);
1045 INIT_LIST_HEAD(&p
->dt_maps
);
1047 ret
= pinctrl_dt_to_map(p
, pctldev
);
1050 return ERR_PTR(ret
);
1053 devname
= dev_name(dev
);
1055 mutex_lock(&pinctrl_maps_mutex
);
1056 /* Iterate over the pin control maps to locate the right ones */
1057 for_each_maps(maps_node
, i
, map
) {
1058 /* Map must be for this device */
1059 if (strcmp(map
->dev_name
, devname
))
1062 * If pctldev is not null, we are claiming hog for it,
1063 * that means, setting that is served by pctldev by itself.
1065 * Thus we must skip map that is for this device but is served
1069 strcmp(dev_name(pctldev
->dev
), map
->ctrl_dev_name
))
1072 ret
= add_setting(p
, pctldev
, map
);
1074 * At this point the adding of a setting may:
1076 * - Defer, if the pinctrl device is not yet available
1077 * - Fail, if the pinctrl device is not yet available,
1078 * AND the setting is a hog. We cannot defer that, since
1079 * the hog will kick in immediately after the device
1082 * If the error returned was not -EPROBE_DEFER then we
1083 * accumulate the errors to see if we end up with
1084 * an -EPROBE_DEFER later, as that is the worst case.
1086 if (ret
== -EPROBE_DEFER
) {
1087 pinctrl_free(p
, false);
1088 mutex_unlock(&pinctrl_maps_mutex
);
1089 return ERR_PTR(ret
);
1092 mutex_unlock(&pinctrl_maps_mutex
);
1095 /* If some other error than deferral occurred, return here */
1096 pinctrl_free(p
, false);
1097 return ERR_PTR(ret
);
1100 kref_init(&p
->users
);
1102 /* Add the pinctrl handle to the global list */
1103 mutex_lock(&pinctrl_list_mutex
);
1104 list_add_tail(&p
->node
, &pinctrl_list
);
1105 mutex_unlock(&pinctrl_list_mutex
);
1111 * pinctrl_get() - retrieves the pinctrl handle for a device
1112 * @dev: the device to obtain the handle for
1114 struct pinctrl
*pinctrl_get(struct device
*dev
)
1119 return ERR_PTR(-EINVAL
);
1122 * See if somebody else (such as the device core) has already
1123 * obtained a handle to the pinctrl for this device. In that case,
1124 * return another pointer to it.
1126 p
= find_pinctrl(dev
);
1128 dev_dbg(dev
, "obtain a copy of previously claimed pinctrl\n");
1129 kref_get(&p
->users
);
1133 return create_pinctrl(dev
, NULL
);
1135 EXPORT_SYMBOL_GPL(pinctrl_get
);
1137 static void pinctrl_free_setting(bool disable_setting
,
1138 struct pinctrl_setting
*setting
)
1140 switch (setting
->type
) {
1141 case PIN_MAP_TYPE_MUX_GROUP
:
1142 if (disable_setting
)
1143 pinmux_disable_setting(setting
);
1144 pinmux_free_setting(setting
);
1146 case PIN_MAP_TYPE_CONFIGS_PIN
:
1147 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1148 pinconf_free_setting(setting
);
1155 static void pinctrl_free(struct pinctrl
*p
, bool inlist
)
1157 struct pinctrl_state
*state
, *n1
;
1158 struct pinctrl_setting
*setting
, *n2
;
1160 mutex_lock(&pinctrl_list_mutex
);
1161 list_for_each_entry_safe(state
, n1
, &p
->states
, node
) {
1162 list_for_each_entry_safe(setting
, n2
, &state
->settings
, node
) {
1163 pinctrl_free_setting(state
== p
->state
, setting
);
1164 list_del(&setting
->node
);
1167 list_del(&state
->node
);
1171 pinctrl_dt_free_maps(p
);
1176 mutex_unlock(&pinctrl_list_mutex
);
1180 * pinctrl_release() - release the pinctrl handle
1181 * @kref: the kref in the pinctrl being released
1183 static void pinctrl_release(struct kref
*kref
)
1185 struct pinctrl
*p
= container_of(kref
, struct pinctrl
, users
);
1187 pinctrl_free(p
, true);
1191 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
1192 * @p: the pinctrl handle to release
1194 void pinctrl_put(struct pinctrl
*p
)
1196 kref_put(&p
->users
, pinctrl_release
);
1198 EXPORT_SYMBOL_GPL(pinctrl_put
);
1201 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
1202 * @p: the pinctrl handle to retrieve the state from
1203 * @name: the state name to retrieve
1205 struct pinctrl_state
*pinctrl_lookup_state(struct pinctrl
*p
,
1208 struct pinctrl_state
*state
;
1210 state
= find_state(p
, name
);
1212 if (pinctrl_dummy_state
) {
1213 /* create dummy state */
1214 dev_dbg(p
->dev
, "using pinctrl dummy state (%s)\n",
1216 state
= create_state(p
, name
);
1218 state
= ERR_PTR(-ENODEV
);
1223 EXPORT_SYMBOL_GPL(pinctrl_lookup_state
);
1225 static void pinctrl_link_add(struct pinctrl_dev
*pctldev
,
1226 struct device
*consumer
)
1228 if (pctldev
->desc
->link_consumers
)
1229 device_link_add(consumer
, pctldev
->dev
,
1230 DL_FLAG_PM_RUNTIME
|
1231 DL_FLAG_AUTOREMOVE_CONSUMER
);
1235 * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
1236 * @p: the pinctrl handle for the device that requests configuration
1237 * @state: the state handle to select/activate/program
1239 static int pinctrl_commit_state(struct pinctrl
*p
, struct pinctrl_state
*state
)
1241 struct pinctrl_setting
*setting
, *setting2
;
1242 struct pinctrl_state
*old_state
= p
->state
;
1247 * For each pinmux setting in the old state, forget SW's record
1248 * of mux owner for that pingroup. Any pingroups which are
1249 * still owned by the new state will be re-acquired by the call
1250 * to pinmux_enable_setting() in the loop below.
1252 list_for_each_entry(setting
, &p
->state
->settings
, node
) {
1253 if (setting
->type
!= PIN_MAP_TYPE_MUX_GROUP
)
1255 pinmux_disable_setting(setting
);
1261 /* Apply all the settings for the new state */
1262 list_for_each_entry(setting
, &state
->settings
, node
) {
1263 switch (setting
->type
) {
1264 case PIN_MAP_TYPE_MUX_GROUP
:
1265 ret
= pinmux_enable_setting(setting
);
1267 case PIN_MAP_TYPE_CONFIGS_PIN
:
1268 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1269 ret
= pinconf_apply_setting(setting
);
1277 goto unapply_new_state
;
1280 /* Do not link hogs (circular dependency) */
1281 if (p
!= setting
->pctldev
->p
)
1282 pinctrl_link_add(setting
->pctldev
, p
->dev
);
1290 dev_err(p
->dev
, "Error applying setting, reverse things back\n");
1292 list_for_each_entry(setting2
, &state
->settings
, node
) {
1293 if (&setting2
->node
== &setting
->node
)
1296 * All we can do here is pinmux_disable_setting.
1297 * That means that some pins are muxed differently now
1298 * than they were before applying the setting (We can't
1299 * "unmux a pin"!), but it's not a big deal since the pins
1300 * are free to be muxed by another apply_setting.
1302 if (setting2
->type
== PIN_MAP_TYPE_MUX_GROUP
)
1303 pinmux_disable_setting(setting2
);
1306 /* There's no infinite recursive loop here because p->state is NULL */
1308 pinctrl_select_state(p
, old_state
);
1314 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
1315 * @p: the pinctrl handle for the device that requests configuration
1316 * @state: the state handle to select/activate/program
1318 int pinctrl_select_state(struct pinctrl
*p
, struct pinctrl_state
*state
)
1320 if (p
->state
== state
)
1323 return pinctrl_commit_state(p
, state
);
1325 EXPORT_SYMBOL_GPL(pinctrl_select_state
);
1327 static void devm_pinctrl_release(struct device
*dev
, void *res
)
1329 pinctrl_put(*(struct pinctrl
**)res
);
1333 * devm_pinctrl_get() - Resource managed pinctrl_get()
1334 * @dev: the device to obtain the handle for
1336 * If there is a need to explicitly destroy the returned struct pinctrl,
1337 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1339 struct pinctrl
*devm_pinctrl_get(struct device
*dev
)
1341 struct pinctrl
**ptr
, *p
;
1343 ptr
= devres_alloc(devm_pinctrl_release
, sizeof(*ptr
), GFP_KERNEL
);
1345 return ERR_PTR(-ENOMEM
);
1347 p
= pinctrl_get(dev
);
1350 devres_add(dev
, ptr
);
1357 EXPORT_SYMBOL_GPL(devm_pinctrl_get
);
1359 static int devm_pinctrl_match(struct device
*dev
, void *res
, void *data
)
1361 struct pinctrl
**p
= res
;
1367 * devm_pinctrl_put() - Resource managed pinctrl_put()
1368 * @p: the pinctrl handle to release
1370 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1371 * this function will not need to be called and the resource management
1372 * code will ensure that the resource is freed.
1374 void devm_pinctrl_put(struct pinctrl
*p
)
1376 WARN_ON(devres_release(p
->dev
, devm_pinctrl_release
,
1377 devm_pinctrl_match
, p
));
1379 EXPORT_SYMBOL_GPL(devm_pinctrl_put
);
1382 * pinctrl_register_mappings() - register a set of pin controller mappings
1383 * @maps: the pincontrol mappings table to register. Note the pinctrl-core
1384 * keeps a reference to the passed in maps, so they should _not_ be
1385 * marked with __initdata.
1386 * @num_maps: the number of maps in the mapping table
1388 int pinctrl_register_mappings(const struct pinctrl_map
*maps
,
1392 struct pinctrl_maps
*maps_node
;
1394 pr_debug("add %u pinctrl maps\n", num_maps
);
1396 /* First sanity check the new mapping */
1397 for (i
= 0; i
< num_maps
; i
++) {
1398 if (!maps
[i
].dev_name
) {
1399 pr_err("failed to register map %s (%d): no device given\n",
1404 if (!maps
[i
].name
) {
1405 pr_err("failed to register map %d: no map name given\n",
1410 if (maps
[i
].type
!= PIN_MAP_TYPE_DUMMY_STATE
&&
1411 !maps
[i
].ctrl_dev_name
) {
1412 pr_err("failed to register map %s (%d): no pin control device given\n",
1417 switch (maps
[i
].type
) {
1418 case PIN_MAP_TYPE_DUMMY_STATE
:
1420 case PIN_MAP_TYPE_MUX_GROUP
:
1421 ret
= pinmux_validate_map(&maps
[i
], i
);
1425 case PIN_MAP_TYPE_CONFIGS_PIN
:
1426 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1427 ret
= pinconf_validate_map(&maps
[i
], i
);
1432 pr_err("failed to register map %s (%d): invalid type given\n",
1438 maps_node
= kzalloc(sizeof(*maps_node
), GFP_KERNEL
);
1442 maps_node
->maps
= maps
;
1443 maps_node
->num_maps
= num_maps
;
1445 mutex_lock(&pinctrl_maps_mutex
);
1446 list_add_tail(&maps_node
->node
, &pinctrl_maps
);
1447 mutex_unlock(&pinctrl_maps_mutex
);
1451 EXPORT_SYMBOL_GPL(pinctrl_register_mappings
);
1454 * pinctrl_unregister_mappings() - unregister a set of pin controller mappings
1455 * @map: the pincontrol mappings table passed to pinctrl_register_mappings()
1456 * when registering the mappings.
1458 void pinctrl_unregister_mappings(const struct pinctrl_map
*map
)
1460 struct pinctrl_maps
*maps_node
;
1462 mutex_lock(&pinctrl_maps_mutex
);
1463 list_for_each_entry(maps_node
, &pinctrl_maps
, node
) {
1464 if (maps_node
->maps
== map
) {
1465 list_del(&maps_node
->node
);
1467 mutex_unlock(&pinctrl_maps_mutex
);
1471 mutex_unlock(&pinctrl_maps_mutex
);
1473 EXPORT_SYMBOL_GPL(pinctrl_unregister_mappings
);
1476 * pinctrl_force_sleep() - turn a given controller device into sleep state
1477 * @pctldev: pin controller device
1479 int pinctrl_force_sleep(struct pinctrl_dev
*pctldev
)
1481 if (!IS_ERR(pctldev
->p
) && !IS_ERR(pctldev
->hog_sleep
))
1482 return pinctrl_commit_state(pctldev
->p
, pctldev
->hog_sleep
);
1485 EXPORT_SYMBOL_GPL(pinctrl_force_sleep
);
1488 * pinctrl_force_default() - turn a given controller device into default state
1489 * @pctldev: pin controller device
1491 int pinctrl_force_default(struct pinctrl_dev
*pctldev
)
1493 if (!IS_ERR(pctldev
->p
) && !IS_ERR(pctldev
->hog_default
))
1494 return pinctrl_commit_state(pctldev
->p
, pctldev
->hog_default
);
1497 EXPORT_SYMBOL_GPL(pinctrl_force_default
);
1500 * pinctrl_init_done() - tell pinctrl probe is done
1502 * We'll use this time to switch the pins from "init" to "default" unless the
1503 * driver selected some other state.
1505 * @dev: device to that's done probing
1507 int pinctrl_init_done(struct device
*dev
)
1509 struct dev_pin_info
*pins
= dev
->pins
;
1515 if (IS_ERR(pins
->init_state
))
1516 return 0; /* No such state */
1518 if (pins
->p
->state
!= pins
->init_state
)
1519 return 0; /* Not at init anyway */
1521 if (IS_ERR(pins
->default_state
))
1522 return 0; /* No default state */
1524 ret
= pinctrl_select_state(pins
->p
, pins
->default_state
);
1526 dev_err(dev
, "failed to activate default pinctrl state\n");
1531 static int pinctrl_select_bound_state(struct device
*dev
,
1532 struct pinctrl_state
*state
)
1534 struct dev_pin_info
*pins
= dev
->pins
;
1538 return 0; /* No such state */
1539 ret
= pinctrl_select_state(pins
->p
, state
);
1541 dev_err(dev
, "failed to activate pinctrl state %s\n",
1547 * pinctrl_select_default_state() - select default pinctrl state
1548 * @dev: device to select default state for
1550 int pinctrl_select_default_state(struct device
*dev
)
1555 return pinctrl_select_bound_state(dev
, dev
->pins
->default_state
);
1557 EXPORT_SYMBOL_GPL(pinctrl_select_default_state
);
1562 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1563 * @dev: device to select default state for
1565 int pinctrl_pm_select_default_state(struct device
*dev
)
1567 return pinctrl_select_default_state(dev
);
1569 EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state
);
1572 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1573 * @dev: device to select sleep state for
1575 int pinctrl_pm_select_sleep_state(struct device
*dev
)
1580 return pinctrl_select_bound_state(dev
, dev
->pins
->sleep_state
);
1582 EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state
);
1585 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1586 * @dev: device to select idle state for
1588 int pinctrl_pm_select_idle_state(struct device
*dev
)
1593 return pinctrl_select_bound_state(dev
, dev
->pins
->idle_state
);
1595 EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state
);
1598 #ifdef CONFIG_DEBUG_FS
1600 static int pinctrl_pins_show(struct seq_file
*s
, void *what
)
1602 struct pinctrl_dev
*pctldev
= s
->private;
1603 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1605 #ifdef CONFIG_GPIOLIB
1606 struct pinctrl_gpio_range
*range
;
1607 unsigned int gpio_num
;
1608 struct gpio_chip
*chip
;
1611 seq_printf(s
, "registered pins: %d\n", pctldev
->desc
->npins
);
1613 mutex_lock(&pctldev
->mutex
);
1615 /* The pin number can be retrived from the pin controller descriptor */
1616 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
1617 struct pin_desc
*desc
;
1619 pin
= pctldev
->desc
->pins
[i
].number
;
1620 desc
= pin_desc_get(pctldev
, pin
);
1621 /* Pin space may be sparse */
1625 seq_printf(s
, "pin %d (%s) ", pin
, desc
->name
);
1627 #ifdef CONFIG_GPIOLIB
1629 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
1630 if ((pin
>= range
->pin_base
) &&
1631 (pin
< (range
->pin_base
+ range
->npins
))) {
1632 gpio_num
= range
->base
+ (pin
- range
->pin_base
);
1636 chip
= gpio_to_chip(gpio_num
);
1637 if (chip
&& chip
->gpiodev
&& chip
->gpiodev
->base
)
1638 seq_printf(s
, "%u:%s ", gpio_num
-
1639 chip
->gpiodev
->base
, chip
->label
);
1641 seq_puts(s
, "0:? ");
1644 /* Driver-specific info per pin */
1645 if (ops
->pin_dbg_show
)
1646 ops
->pin_dbg_show(pctldev
, s
, pin
);
1651 mutex_unlock(&pctldev
->mutex
);
1655 DEFINE_SHOW_ATTRIBUTE(pinctrl_pins
);
1657 static int pinctrl_groups_show(struct seq_file
*s
, void *what
)
1659 struct pinctrl_dev
*pctldev
= s
->private;
1660 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1661 unsigned ngroups
, selector
= 0;
1663 mutex_lock(&pctldev
->mutex
);
1665 ngroups
= ops
->get_groups_count(pctldev
);
1667 seq_puts(s
, "registered pin groups:\n");
1668 while (selector
< ngroups
) {
1669 const unsigned *pins
= NULL
;
1670 unsigned num_pins
= 0;
1671 const char *gname
= ops
->get_group_name(pctldev
, selector
);
1676 if (ops
->get_group_pins
)
1677 ret
= ops
->get_group_pins(pctldev
, selector
,
1680 seq_printf(s
, "%s [ERROR GETTING PINS]\n",
1683 seq_printf(s
, "group: %s\n", gname
);
1684 for (i
= 0; i
< num_pins
; i
++) {
1685 pname
= pin_get_name(pctldev
, pins
[i
]);
1686 if (WARN_ON(!pname
)) {
1687 mutex_unlock(&pctldev
->mutex
);
1690 seq_printf(s
, "pin %d (%s)\n", pins
[i
], pname
);
1697 mutex_unlock(&pctldev
->mutex
);
1701 DEFINE_SHOW_ATTRIBUTE(pinctrl_groups
);
1703 static int pinctrl_gpioranges_show(struct seq_file
*s
, void *what
)
1705 struct pinctrl_dev
*pctldev
= s
->private;
1706 struct pinctrl_gpio_range
*range
;
1708 seq_puts(s
, "GPIO ranges handled:\n");
1710 mutex_lock(&pctldev
->mutex
);
1712 /* Loop over the ranges */
1713 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
1716 seq_printf(s
, "%u: %s GPIOS [%u - %u] PINS {",
1717 range
->id
, range
->name
,
1718 range
->base
, (range
->base
+ range
->npins
- 1));
1719 for (a
= 0; a
< range
->npins
- 1; a
++)
1720 seq_printf(s
, "%u, ", range
->pins
[a
]);
1721 seq_printf(s
, "%u}\n", range
->pins
[a
]);
1724 seq_printf(s
, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1725 range
->id
, range
->name
,
1726 range
->base
, (range
->base
+ range
->npins
- 1),
1728 (range
->pin_base
+ range
->npins
- 1));
1731 mutex_unlock(&pctldev
->mutex
);
1735 DEFINE_SHOW_ATTRIBUTE(pinctrl_gpioranges
);
1737 static int pinctrl_devices_show(struct seq_file
*s
, void *what
)
1739 struct pinctrl_dev
*pctldev
;
1741 seq_puts(s
, "name [pinmux] [pinconf]\n");
1743 mutex_lock(&pinctrldev_list_mutex
);
1745 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
1746 seq_printf(s
, "%s ", pctldev
->desc
->name
);
1747 if (pctldev
->desc
->pmxops
)
1748 seq_puts(s
, "yes ");
1751 if (pctldev
->desc
->confops
)
1758 mutex_unlock(&pinctrldev_list_mutex
);
1762 DEFINE_SHOW_ATTRIBUTE(pinctrl_devices
);
1764 static inline const char *map_type(enum pinctrl_map_type type
)
1766 static const char * const names
[] = {
1774 if (type
>= ARRAY_SIZE(names
))
1780 static int pinctrl_maps_show(struct seq_file
*s
, void *what
)
1782 struct pinctrl_maps
*maps_node
;
1784 const struct pinctrl_map
*map
;
1786 seq_puts(s
, "Pinctrl maps:\n");
1788 mutex_lock(&pinctrl_maps_mutex
);
1789 for_each_maps(maps_node
, i
, map
) {
1790 seq_printf(s
, "device %s\nstate %s\ntype %s (%d)\n",
1791 map
->dev_name
, map
->name
, map_type(map
->type
),
1794 if (map
->type
!= PIN_MAP_TYPE_DUMMY_STATE
)
1795 seq_printf(s
, "controlling device %s\n",
1796 map
->ctrl_dev_name
);
1798 switch (map
->type
) {
1799 case PIN_MAP_TYPE_MUX_GROUP
:
1800 pinmux_show_map(s
, map
);
1802 case PIN_MAP_TYPE_CONFIGS_PIN
:
1803 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1804 pinconf_show_map(s
, map
);
1812 mutex_unlock(&pinctrl_maps_mutex
);
1816 DEFINE_SHOW_ATTRIBUTE(pinctrl_maps
);
1818 static int pinctrl_show(struct seq_file
*s
, void *what
)
1821 struct pinctrl_state
*state
;
1822 struct pinctrl_setting
*setting
;
1824 seq_puts(s
, "Requested pin control handlers their pinmux maps:\n");
1826 mutex_lock(&pinctrl_list_mutex
);
1828 list_for_each_entry(p
, &pinctrl_list
, node
) {
1829 seq_printf(s
, "device: %s current state: %s\n",
1831 p
->state
? p
->state
->name
: "none");
1833 list_for_each_entry(state
, &p
->states
, node
) {
1834 seq_printf(s
, " state: %s\n", state
->name
);
1836 list_for_each_entry(setting
, &state
->settings
, node
) {
1837 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
1839 seq_printf(s
, " type: %s controller %s ",
1840 map_type(setting
->type
),
1841 pinctrl_dev_get_name(pctldev
));
1843 switch (setting
->type
) {
1844 case PIN_MAP_TYPE_MUX_GROUP
:
1845 pinmux_show_setting(s
, setting
);
1847 case PIN_MAP_TYPE_CONFIGS_PIN
:
1848 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1849 pinconf_show_setting(s
, setting
);
1858 mutex_unlock(&pinctrl_list_mutex
);
1862 DEFINE_SHOW_ATTRIBUTE(pinctrl
);
1864 static struct dentry
*debugfs_root
;
1866 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1868 struct dentry
*device_root
;
1869 const char *debugfs_name
;
1871 if (pctldev
->desc
->name
&&
1872 strcmp(dev_name(pctldev
->dev
), pctldev
->desc
->name
)) {
1873 debugfs_name
= devm_kasprintf(pctldev
->dev
, GFP_KERNEL
,
1874 "%s-%s", dev_name(pctldev
->dev
),
1875 pctldev
->desc
->name
);
1876 if (!debugfs_name
) {
1877 pr_warn("failed to determine debugfs dir name for %s\n",
1878 dev_name(pctldev
->dev
));
1882 debugfs_name
= dev_name(pctldev
->dev
);
1885 device_root
= debugfs_create_dir(debugfs_name
, debugfs_root
);
1886 pctldev
->device_root
= device_root
;
1888 if (IS_ERR(device_root
) || !device_root
) {
1889 pr_warn("failed to create debugfs directory for %s\n",
1890 dev_name(pctldev
->dev
));
1893 debugfs_create_file("pins", S_IFREG
| S_IRUGO
,
1894 device_root
, pctldev
, &pinctrl_pins_fops
);
1895 debugfs_create_file("pingroups", S_IFREG
| S_IRUGO
,
1896 device_root
, pctldev
, &pinctrl_groups_fops
);
1897 debugfs_create_file("gpio-ranges", S_IFREG
| S_IRUGO
,
1898 device_root
, pctldev
, &pinctrl_gpioranges_fops
);
1899 if (pctldev
->desc
->pmxops
)
1900 pinmux_init_device_debugfs(device_root
, pctldev
);
1901 if (pctldev
->desc
->confops
)
1902 pinconf_init_device_debugfs(device_root
, pctldev
);
1905 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1907 debugfs_remove_recursive(pctldev
->device_root
);
1910 static void pinctrl_init_debugfs(void)
1912 debugfs_root
= debugfs_create_dir("pinctrl", NULL
);
1913 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
1914 pr_warn("failed to create debugfs directory\n");
1915 debugfs_root
= NULL
;
1919 debugfs_create_file("pinctrl-devices", S_IFREG
| S_IRUGO
,
1920 debugfs_root
, NULL
, &pinctrl_devices_fops
);
1921 debugfs_create_file("pinctrl-maps", S_IFREG
| S_IRUGO
,
1922 debugfs_root
, NULL
, &pinctrl_maps_fops
);
1923 debugfs_create_file("pinctrl-handles", S_IFREG
| S_IRUGO
,
1924 debugfs_root
, NULL
, &pinctrl_fops
);
1927 #else /* CONFIG_DEBUG_FS */
1929 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1933 static void pinctrl_init_debugfs(void)
1937 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1943 static int pinctrl_check_ops(struct pinctrl_dev
*pctldev
)
1945 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1948 !ops
->get_groups_count
||
1949 !ops
->get_group_name
)
1956 * pinctrl_init_controller() - init a pin controller device
1957 * @pctldesc: descriptor for this pin controller
1958 * @dev: parent device for this pin controller
1959 * @driver_data: private pin controller data for this pin controller
1961 static struct pinctrl_dev
*
1962 pinctrl_init_controller(struct pinctrl_desc
*pctldesc
, struct device
*dev
,
1965 struct pinctrl_dev
*pctldev
;
1969 return ERR_PTR(-EINVAL
);
1970 if (!pctldesc
->name
)
1971 return ERR_PTR(-EINVAL
);
1973 pctldev
= kzalloc(sizeof(*pctldev
), GFP_KERNEL
);
1975 return ERR_PTR(-ENOMEM
);
1977 /* Initialize pin control device struct */
1978 pctldev
->owner
= pctldesc
->owner
;
1979 pctldev
->desc
= pctldesc
;
1980 pctldev
->driver_data
= driver_data
;
1981 INIT_RADIX_TREE(&pctldev
->pin_desc_tree
, GFP_KERNEL
);
1982 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
1983 INIT_RADIX_TREE(&pctldev
->pin_group_tree
, GFP_KERNEL
);
1985 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
1986 INIT_RADIX_TREE(&pctldev
->pin_function_tree
, GFP_KERNEL
);
1988 INIT_LIST_HEAD(&pctldev
->gpio_ranges
);
1989 INIT_LIST_HEAD(&pctldev
->node
);
1991 mutex_init(&pctldev
->mutex
);
1993 /* check core ops for sanity */
1994 ret
= pinctrl_check_ops(pctldev
);
1996 dev_err(dev
, "pinctrl ops lacks necessary functions\n");
2000 /* If we're implementing pinmuxing, check the ops for sanity */
2001 if (pctldesc
->pmxops
) {
2002 ret
= pinmux_check_ops(pctldev
);
2007 /* If we're implementing pinconfig, check the ops for sanity */
2008 if (pctldesc
->confops
) {
2009 ret
= pinconf_check_ops(pctldev
);
2014 /* Register all the pins */
2015 dev_dbg(dev
, "try to register %d pins ...\n", pctldesc
->npins
);
2016 ret
= pinctrl_register_pins(pctldev
, pctldesc
->pins
, pctldesc
->npins
);
2018 dev_err(dev
, "error during pin registration\n");
2019 pinctrl_free_pindescs(pctldev
, pctldesc
->pins
,
2027 mutex_destroy(&pctldev
->mutex
);
2029 return ERR_PTR(ret
);
2032 static int pinctrl_claim_hogs(struct pinctrl_dev
*pctldev
)
2034 pctldev
->p
= create_pinctrl(pctldev
->dev
, pctldev
);
2035 if (PTR_ERR(pctldev
->p
) == -ENODEV
) {
2036 dev_dbg(pctldev
->dev
, "no hogs found\n");
2041 if (IS_ERR(pctldev
->p
)) {
2042 dev_err(pctldev
->dev
, "error claiming hogs: %li\n",
2043 PTR_ERR(pctldev
->p
));
2045 return PTR_ERR(pctldev
->p
);
2048 pctldev
->hog_default
=
2049 pinctrl_lookup_state(pctldev
->p
, PINCTRL_STATE_DEFAULT
);
2050 if (IS_ERR(pctldev
->hog_default
)) {
2051 dev_dbg(pctldev
->dev
,
2052 "failed to lookup the default state\n");
2054 if (pinctrl_select_state(pctldev
->p
,
2055 pctldev
->hog_default
))
2056 dev_err(pctldev
->dev
,
2057 "failed to select default state\n");
2060 pctldev
->hog_sleep
=
2061 pinctrl_lookup_state(pctldev
->p
,
2062 PINCTRL_STATE_SLEEP
);
2063 if (IS_ERR(pctldev
->hog_sleep
))
2064 dev_dbg(pctldev
->dev
,
2065 "failed to lookup the sleep state\n");
2070 int pinctrl_enable(struct pinctrl_dev
*pctldev
)
2074 error
= pinctrl_claim_hogs(pctldev
);
2076 dev_err(pctldev
->dev
, "could not claim hogs: %i\n",
2078 mutex_destroy(&pctldev
->mutex
);
2084 mutex_lock(&pinctrldev_list_mutex
);
2085 list_add_tail(&pctldev
->node
, &pinctrldev_list
);
2086 mutex_unlock(&pinctrldev_list_mutex
);
2088 pinctrl_init_device_debugfs(pctldev
);
2092 EXPORT_SYMBOL_GPL(pinctrl_enable
);
2095 * pinctrl_register() - register a pin controller device
2096 * @pctldesc: descriptor for this pin controller
2097 * @dev: parent device for this pin controller
2098 * @driver_data: private pin controller data for this pin controller
2100 * Note that pinctrl_register() is known to have problems as the pin
2101 * controller driver functions are called before the driver has a
2102 * struct pinctrl_dev handle. To avoid issues later on, please use the
2103 * new pinctrl_register_and_init() below instead.
2105 struct pinctrl_dev
*pinctrl_register(struct pinctrl_desc
*pctldesc
,
2106 struct device
*dev
, void *driver_data
)
2108 struct pinctrl_dev
*pctldev
;
2111 pctldev
= pinctrl_init_controller(pctldesc
, dev
, driver_data
);
2112 if (IS_ERR(pctldev
))
2115 error
= pinctrl_enable(pctldev
);
2117 return ERR_PTR(error
);
2122 EXPORT_SYMBOL_GPL(pinctrl_register
);
2125 * pinctrl_register_and_init() - register and init pin controller device
2126 * @pctldesc: descriptor for this pin controller
2127 * @dev: parent device for this pin controller
2128 * @driver_data: private pin controller data for this pin controller
2129 * @pctldev: pin controller device
2131 * Note that pinctrl_enable() still needs to be manually called after
2132 * this once the driver is ready.
2134 int pinctrl_register_and_init(struct pinctrl_desc
*pctldesc
,
2135 struct device
*dev
, void *driver_data
,
2136 struct pinctrl_dev
**pctldev
)
2138 struct pinctrl_dev
*p
;
2140 p
= pinctrl_init_controller(pctldesc
, dev
, driver_data
);
2145 * We have pinctrl_start() call functions in the pin controller
2146 * driver with create_pinctrl() for at least dt_node_to_map(). So
2147 * let's make sure pctldev is properly initialized for the
2148 * pin controller driver before we do anything.
2154 EXPORT_SYMBOL_GPL(pinctrl_register_and_init
);
2157 * pinctrl_unregister() - unregister pinmux
2158 * @pctldev: pin controller to unregister
2160 * Called by pinmux drivers to unregister a pinmux.
2162 void pinctrl_unregister(struct pinctrl_dev
*pctldev
)
2164 struct pinctrl_gpio_range
*range
, *n
;
2169 mutex_lock(&pctldev
->mutex
);
2170 pinctrl_remove_device_debugfs(pctldev
);
2171 mutex_unlock(&pctldev
->mutex
);
2173 if (!IS_ERR_OR_NULL(pctldev
->p
))
2174 pinctrl_put(pctldev
->p
);
2176 mutex_lock(&pinctrldev_list_mutex
);
2177 mutex_lock(&pctldev
->mutex
);
2178 /* TODO: check that no pinmuxes are still active? */
2179 list_del(&pctldev
->node
);
2180 pinmux_generic_free_functions(pctldev
);
2181 pinctrl_generic_free_groups(pctldev
);
2182 /* Destroy descriptor tree */
2183 pinctrl_free_pindescs(pctldev
, pctldev
->desc
->pins
,
2184 pctldev
->desc
->npins
);
2185 /* remove gpio ranges map */
2186 list_for_each_entry_safe(range
, n
, &pctldev
->gpio_ranges
, node
)
2187 list_del(&range
->node
);
2189 mutex_unlock(&pctldev
->mutex
);
2190 mutex_destroy(&pctldev
->mutex
);
2192 mutex_unlock(&pinctrldev_list_mutex
);
2194 EXPORT_SYMBOL_GPL(pinctrl_unregister
);
2196 static void devm_pinctrl_dev_release(struct device
*dev
, void *res
)
2198 struct pinctrl_dev
*pctldev
= *(struct pinctrl_dev
**)res
;
2200 pinctrl_unregister(pctldev
);
2203 static int devm_pinctrl_dev_match(struct device
*dev
, void *res
, void *data
)
2205 struct pctldev
**r
= res
;
2207 if (WARN_ON(!r
|| !*r
))
2214 * devm_pinctrl_register() - Resource managed version of pinctrl_register().
2215 * @dev: parent device for this pin controller
2216 * @pctldesc: descriptor for this pin controller
2217 * @driver_data: private pin controller data for this pin controller
2219 * Returns an error pointer if pincontrol register failed. Otherwise
2220 * it returns valid pinctrl handle.
2222 * The pinctrl device will be automatically released when the device is unbound.
2224 struct pinctrl_dev
*devm_pinctrl_register(struct device
*dev
,
2225 struct pinctrl_desc
*pctldesc
,
2228 struct pinctrl_dev
**ptr
, *pctldev
;
2230 ptr
= devres_alloc(devm_pinctrl_dev_release
, sizeof(*ptr
), GFP_KERNEL
);
2232 return ERR_PTR(-ENOMEM
);
2234 pctldev
= pinctrl_register(pctldesc
, dev
, driver_data
);
2235 if (IS_ERR(pctldev
)) {
2241 devres_add(dev
, ptr
);
2245 EXPORT_SYMBOL_GPL(devm_pinctrl_register
);
2248 * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
2249 * @dev: parent device for this pin controller
2250 * @pctldesc: descriptor for this pin controller
2251 * @driver_data: private pin controller data for this pin controller
2252 * @pctldev: pin controller device
2254 * Returns zero on success or an error number on failure.
2256 * The pinctrl device will be automatically released when the device is unbound.
2258 int devm_pinctrl_register_and_init(struct device
*dev
,
2259 struct pinctrl_desc
*pctldesc
,
2261 struct pinctrl_dev
**pctldev
)
2263 struct pinctrl_dev
**ptr
;
2266 ptr
= devres_alloc(devm_pinctrl_dev_release
, sizeof(*ptr
), GFP_KERNEL
);
2270 error
= pinctrl_register_and_init(pctldesc
, dev
, driver_data
, pctldev
);
2277 devres_add(dev
, ptr
);
2281 EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init
);
2284 * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
2285 * @dev: device for which which resource was allocated
2286 * @pctldev: the pinctrl device to unregister.
2288 void devm_pinctrl_unregister(struct device
*dev
, struct pinctrl_dev
*pctldev
)
2290 WARN_ON(devres_release(dev
, devm_pinctrl_dev_release
,
2291 devm_pinctrl_dev_match
, pctldev
));
2293 EXPORT_SYMBOL_GPL(devm_pinctrl_unregister
);
2295 static int __init
pinctrl_init(void)
2297 pr_info("initialized pinctrl subsystem\n");
2298 pinctrl_init_debugfs();
2302 /* init early since many drivers really need to initialized pinmux early */
2303 core_initcall(pinctrl_init
);