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/array_size.h>
16 #include <linux/debugfs.h>
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/export.h>
20 #include <linux/init.h>
21 #include <linux/kref.h>
22 #include <linux/list.h>
23 #include <linux/seq_file.h>
24 #include <linux/slab.h>
26 #include <linux/gpio/driver.h>
28 #include <linux/pinctrl/consumer.h>
29 #include <linux/pinctrl/devinfo.h>
30 #include <linux/pinctrl/machine.h>
31 #include <linux/pinctrl/pinctrl.h>
34 #include "../gpio/gpiolib.h"
38 #include "devicetree.h"
42 static bool pinctrl_dummy_state
;
44 /* Mutex taken to protect pinctrl_list */
45 static DEFINE_MUTEX(pinctrl_list_mutex
);
47 /* Mutex taken to protect pinctrl_maps */
48 DEFINE_MUTEX(pinctrl_maps_mutex
);
50 /* Mutex taken to protect pinctrldev_list */
51 static DEFINE_MUTEX(pinctrldev_list_mutex
);
53 /* Global list of pin control devices (struct pinctrl_dev) */
54 static LIST_HEAD(pinctrldev_list
);
56 /* List of pin controller handles (struct pinctrl) */
57 static LIST_HEAD(pinctrl_list
);
59 /* List of pinctrl maps (struct pinctrl_maps) */
60 LIST_HEAD(pinctrl_maps
);
64 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
66 * Usually this function is called by platforms without pinctrl driver support
67 * but run with some shared drivers using pinctrl APIs.
68 * After calling this function, the pinctrl core will return successfully
69 * with creating a dummy state for the driver to keep going smoothly.
71 void pinctrl_provide_dummies(void)
73 pinctrl_dummy_state
= true;
76 const char *pinctrl_dev_get_name(struct pinctrl_dev
*pctldev
)
78 /* We're not allowed to register devices without name */
79 return pctldev
->desc
->name
;
81 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name
);
83 const char *pinctrl_dev_get_devname(struct pinctrl_dev
*pctldev
)
85 return dev_name(pctldev
->dev
);
87 EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname
);
89 void *pinctrl_dev_get_drvdata(struct pinctrl_dev
*pctldev
)
91 return pctldev
->driver_data
;
93 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata
);
96 * get_pinctrl_dev_from_devname() - look up pin controller device
97 * @devname: the name of a device instance, as returned by dev_name()
99 * Looks up a pin control device matching a certain device name or pure device
100 * pointer, the pure device pointer will take precedence.
102 struct pinctrl_dev
*get_pinctrl_dev_from_devname(const char *devname
)
104 struct pinctrl_dev
*pctldev
;
109 mutex_lock(&pinctrldev_list_mutex
);
111 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
112 if (!strcmp(dev_name(pctldev
->dev
), devname
)) {
113 /* Matched on device name */
114 mutex_unlock(&pinctrldev_list_mutex
);
119 mutex_unlock(&pinctrldev_list_mutex
);
124 struct pinctrl_dev
*get_pinctrl_dev_from_of_node(struct device_node
*np
)
126 struct pinctrl_dev
*pctldev
;
128 mutex_lock(&pinctrldev_list_mutex
);
130 list_for_each_entry(pctldev
, &pinctrldev_list
, node
)
131 if (device_match_of_node(pctldev
->dev
, np
)) {
132 mutex_unlock(&pinctrldev_list_mutex
);
136 mutex_unlock(&pinctrldev_list_mutex
);
142 * pin_get_from_name() - look up a pin number from a name
143 * @pctldev: the pin control device to lookup the pin on
144 * @name: the name of the pin to look up
146 int pin_get_from_name(struct pinctrl_dev
*pctldev
, const char *name
)
150 /* The pin number can be retrived from the pin controller descriptor */
151 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
152 struct pin_desc
*desc
;
154 pin
= pctldev
->desc
->pins
[i
].number
;
155 desc
= pin_desc_get(pctldev
, pin
);
156 /* Pin space may be sparse */
157 if (desc
&& !strcmp(name
, desc
->name
))
165 * pin_get_name() - look up a pin name from a pin id
166 * @pctldev: the pin control device to lookup the pin on
167 * @pin: pin number/id to look up
169 const char *pin_get_name(struct pinctrl_dev
*pctldev
, const unsigned pin
)
171 const struct pin_desc
*desc
;
173 desc
= pin_desc_get(pctldev
, pin
);
175 dev_err(pctldev
->dev
, "failed to get pin(%d) name\n",
182 EXPORT_SYMBOL_GPL(pin_get_name
);
184 /* Deletes a range of pin descriptors */
185 static void pinctrl_free_pindescs(struct pinctrl_dev
*pctldev
,
186 const struct pinctrl_pin_desc
*pins
,
191 for (i
= 0; i
< num_pins
; i
++) {
192 struct pin_desc
*pindesc
;
194 pindesc
= radix_tree_lookup(&pctldev
->pin_desc_tree
,
197 radix_tree_delete(&pctldev
->pin_desc_tree
,
199 if (pindesc
->dynamic_name
)
200 kfree(pindesc
->name
);
206 static int pinctrl_register_one_pin(struct pinctrl_dev
*pctldev
,
207 const struct pinctrl_pin_desc
*pin
)
209 struct pin_desc
*pindesc
;
212 pindesc
= pin_desc_get(pctldev
, pin
->number
);
214 dev_err(pctldev
->dev
, "pin %d already registered\n",
219 pindesc
= kzalloc(sizeof(*pindesc
), GFP_KERNEL
);
224 pindesc
->pctldev
= pctldev
;
226 /* Copy basic pin info */
228 pindesc
->name
= pin
->name
;
230 pindesc
->name
= kasprintf(GFP_KERNEL
, "PIN%u", pin
->number
);
231 if (!pindesc
->name
) {
235 pindesc
->dynamic_name
= true;
238 pindesc
->drv_data
= pin
->drv_data
;
240 error
= radix_tree_insert(&pctldev
->pin_desc_tree
, pin
->number
, pindesc
);
244 pr_debug("registered pin %d (%s) on %s\n",
245 pin
->number
, pindesc
->name
, pctldev
->desc
->name
);
253 static int pinctrl_register_pins(struct pinctrl_dev
*pctldev
,
254 const struct pinctrl_pin_desc
*pins
,
260 for (i
= 0; i
< num_descs
; i
++) {
261 ret
= pinctrl_register_one_pin(pctldev
, &pins
[i
]);
270 * gpio_to_pin() - GPIO range GPIO number to pin number translation
271 * @range: GPIO range used for the translation
272 * @gc: GPIO chip structure from the GPIO subsystem
273 * @offset: hardware offset of the GPIO relative to the controller
275 * Finds the pin number for a given GPIO using the specified GPIO range
276 * as a base for translation. The distinction between linear GPIO ranges
277 * and pin list based GPIO ranges is managed correctly by this function.
279 * This function assumes the gpio is part of the specified GPIO range, use
280 * only after making sure this is the case (e.g. by calling it on the
281 * result of successful pinctrl_get_device_gpio_range calls)!
283 static inline int gpio_to_pin(struct pinctrl_gpio_range
*range
,
284 struct gpio_chip
*gc
, unsigned int offset
)
286 unsigned int pin
= gc
->base
+ offset
- range
->base
;
288 return range
->pins
[pin
];
290 return range
->pin_base
+ pin
;
294 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
295 * @pctldev: pin controller device to check
296 * @gc: GPIO chip structure from the GPIO subsystem
297 * @offset: hardware offset of the GPIO relative to the controller
299 * Tries to match a GPIO pin number to the ranges handled by a certain pin
300 * controller, return the range or NULL
302 static struct pinctrl_gpio_range
*
303 pinctrl_match_gpio_range(struct pinctrl_dev
*pctldev
, struct gpio_chip
*gc
,
306 struct pinctrl_gpio_range
*range
;
308 mutex_lock(&pctldev
->mutex
);
309 /* Loop over the ranges */
310 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
311 /* Check if we're in the valid range */
312 if ((gc
->base
+ offset
) >= range
->base
&&
313 (gc
->base
+ offset
) < range
->base
+ range
->npins
) {
314 mutex_unlock(&pctldev
->mutex
);
318 mutex_unlock(&pctldev
->mutex
);
323 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
324 * the same GPIO chip are in range
325 * @gc: GPIO chip structure from the GPIO subsystem
326 * @offset: hardware offset of the GPIO relative to the controller
328 * This function is complement of pinctrl_match_gpio_range(). If the return
329 * value of pinctrl_match_gpio_range() is NULL, this function could be used
330 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
331 * of the same GPIO chip don't have back-end pinctrl interface.
332 * If the return value is true, it means that pinctrl device is ready & the
333 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
334 * is false, it means that pinctrl device may not be ready.
336 #ifdef CONFIG_GPIOLIB
337 static bool pinctrl_ready_for_gpio_range(struct gpio_chip
*gc
,
340 struct pinctrl_dev
*pctldev
;
341 struct pinctrl_gpio_range
*range
= NULL
;
343 mutex_lock(&pinctrldev_list_mutex
);
345 /* Loop over the pin controllers */
346 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
347 /* Loop over the ranges */
348 mutex_lock(&pctldev
->mutex
);
349 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
350 /* Check if any gpio range overlapped with gpio chip */
351 if (range
->base
+ range
->npins
- 1 < gc
->base
||
352 range
->base
> gc
->base
+ gc
->ngpio
- 1)
354 mutex_unlock(&pctldev
->mutex
);
355 mutex_unlock(&pinctrldev_list_mutex
);
358 mutex_unlock(&pctldev
->mutex
);
361 mutex_unlock(&pinctrldev_list_mutex
);
367 pinctrl_ready_for_gpio_range(struct gpio_chip
*gc
, unsigned int offset
)
374 * pinctrl_get_device_gpio_range() - find device for GPIO range
375 * @gc: GPIO chip structure from the GPIO subsystem
376 * @offset: hardware offset of the GPIO relative to the controller
377 * @outdev: the pin control device if found
378 * @outrange: the GPIO range if found
380 * Find the pin controller handling a certain GPIO pin from the pinspace of
381 * the GPIO subsystem, return the device and the matching GPIO range. Returns
382 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
383 * may still have not been registered.
385 static int pinctrl_get_device_gpio_range(struct gpio_chip
*gc
,
387 struct pinctrl_dev
**outdev
,
388 struct pinctrl_gpio_range
**outrange
)
390 struct pinctrl_dev
*pctldev
;
392 mutex_lock(&pinctrldev_list_mutex
);
394 /* Loop over the pin controllers */
395 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
396 struct pinctrl_gpio_range
*range
;
398 range
= pinctrl_match_gpio_range(pctldev
, gc
, offset
);
402 mutex_unlock(&pinctrldev_list_mutex
);
407 mutex_unlock(&pinctrldev_list_mutex
);
409 return -EPROBE_DEFER
;
413 * pinctrl_add_gpio_range() - register a GPIO range for a controller
414 * @pctldev: pin controller device to add the range to
415 * @range: the GPIO range to add
417 * This adds a range of GPIOs to be handled by a certain pin controller. Call
418 * this to register handled ranges after registering your pin controller.
420 void pinctrl_add_gpio_range(struct pinctrl_dev
*pctldev
,
421 struct pinctrl_gpio_range
*range
)
423 mutex_lock(&pctldev
->mutex
);
424 list_add_tail(&range
->node
, &pctldev
->gpio_ranges
);
425 mutex_unlock(&pctldev
->mutex
);
427 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range
);
429 void pinctrl_add_gpio_ranges(struct pinctrl_dev
*pctldev
,
430 struct pinctrl_gpio_range
*ranges
,
435 for (i
= 0; i
< nranges
; i
++)
436 pinctrl_add_gpio_range(pctldev
, &ranges
[i
]);
438 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges
);
440 struct pinctrl_dev
*pinctrl_find_and_add_gpio_range(const char *devname
,
441 struct pinctrl_gpio_range
*range
)
443 struct pinctrl_dev
*pctldev
;
445 pctldev
= get_pinctrl_dev_from_devname(devname
);
448 * If we can't find this device, let's assume that is because
449 * it has not probed yet, so the driver trying to register this
450 * range need to defer probing.
453 return ERR_PTR(-EPROBE_DEFER
);
455 pinctrl_add_gpio_range(pctldev
, range
);
459 EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range
);
461 int pinctrl_get_group_pins(struct pinctrl_dev
*pctldev
, const char *pin_group
,
462 const unsigned **pins
, unsigned *num_pins
)
464 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
467 if (!pctlops
->get_group_pins
)
470 gs
= pinctrl_get_group_selector(pctldev
, pin_group
);
474 return pctlops
->get_group_pins(pctldev
, gs
, pins
, num_pins
);
476 EXPORT_SYMBOL_GPL(pinctrl_get_group_pins
);
478 struct pinctrl_gpio_range
*
479 pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev
*pctldev
,
482 struct pinctrl_gpio_range
*range
;
484 /* Loop over the ranges */
485 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
486 /* Check if we're in the valid range */
489 for (a
= 0; a
< range
->npins
; a
++) {
490 if (range
->pins
[a
] == pin
)
493 } else if (pin
>= range
->pin_base
&&
494 pin
< range
->pin_base
+ range
->npins
)
500 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock
);
503 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
504 * @pctldev: the pin controller device to look in
505 * @pin: a controller-local number to find the range for
507 struct pinctrl_gpio_range
*
508 pinctrl_find_gpio_range_from_pin(struct pinctrl_dev
*pctldev
,
511 struct pinctrl_gpio_range
*range
;
513 mutex_lock(&pctldev
->mutex
);
514 range
= pinctrl_find_gpio_range_from_pin_nolock(pctldev
, pin
);
515 mutex_unlock(&pctldev
->mutex
);
519 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin
);
522 * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller
523 * @pctldev: pin controller device to remove the range from
524 * @range: the GPIO range to remove
526 void pinctrl_remove_gpio_range(struct pinctrl_dev
*pctldev
,
527 struct pinctrl_gpio_range
*range
)
529 mutex_lock(&pctldev
->mutex
);
530 list_del(&range
->node
);
531 mutex_unlock(&pctldev
->mutex
);
533 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range
);
535 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
538 * pinctrl_generic_get_group_count() - returns the number of pin groups
539 * @pctldev: pin controller device
541 int pinctrl_generic_get_group_count(struct pinctrl_dev
*pctldev
)
543 return pctldev
->num_groups
;
545 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count
);
548 * pinctrl_generic_get_group_name() - returns the name of a pin group
549 * @pctldev: pin controller device
550 * @selector: group number
552 const char *pinctrl_generic_get_group_name(struct pinctrl_dev
*pctldev
,
553 unsigned int selector
)
555 struct group_desc
*group
;
557 group
= radix_tree_lookup(&pctldev
->pin_group_tree
,
564 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name
);
567 * pinctrl_generic_get_group_pins() - gets the pin group pins
568 * @pctldev: pin controller device
569 * @selector: group number
570 * @pins: pins in the group
571 * @num_pins: number of pins in the group
573 int pinctrl_generic_get_group_pins(struct pinctrl_dev
*pctldev
,
574 unsigned int selector
,
575 const unsigned int **pins
,
576 unsigned int *num_pins
)
578 struct group_desc
*group
;
580 group
= radix_tree_lookup(&pctldev
->pin_group_tree
,
583 dev_err(pctldev
->dev
, "%s could not find pingroup%i\n",
589 *num_pins
= group
->num_pins
;
593 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins
);
596 * pinctrl_generic_get_group() - returns a pin group based on the number
597 * @pctldev: pin controller device
598 * @selector: group number
600 struct group_desc
*pinctrl_generic_get_group(struct pinctrl_dev
*pctldev
,
601 unsigned int selector
)
603 struct group_desc
*group
;
605 group
= radix_tree_lookup(&pctldev
->pin_group_tree
,
612 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group
);
614 static int pinctrl_generic_group_name_to_selector(struct pinctrl_dev
*pctldev
,
615 const char *function
)
617 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
618 int ngroups
= ops
->get_groups_count(pctldev
);
621 /* See if this pctldev has this group */
622 while (selector
< ngroups
) {
623 const char *gname
= ops
->get_group_name(pctldev
, selector
);
625 if (gname
&& !strcmp(function
, gname
))
635 * pinctrl_generic_add_group() - adds a new pin group
636 * @pctldev: pin controller device
637 * @name: name of the pin group
638 * @pins: pins in the pin group
639 * @num_pins: number of pins in the pin group
640 * @data: pin controller driver specific data
642 * Note that the caller must take care of locking.
644 int pinctrl_generic_add_group(struct pinctrl_dev
*pctldev
, const char *name
,
645 int *pins
, int num_pins
, void *data
)
647 struct group_desc
*group
;
653 selector
= pinctrl_generic_group_name_to_selector(pctldev
, name
);
657 selector
= pctldev
->num_groups
;
659 group
= devm_kzalloc(pctldev
->dev
, sizeof(*group
), GFP_KERNEL
);
665 group
->num_pins
= num_pins
;
668 error
= radix_tree_insert(&pctldev
->pin_group_tree
, selector
, group
);
672 pctldev
->num_groups
++;
676 EXPORT_SYMBOL_GPL(pinctrl_generic_add_group
);
679 * pinctrl_generic_remove_group() - removes a numbered pin group
680 * @pctldev: pin controller device
681 * @selector: group number
683 * Note that the caller must take care of locking.
685 int pinctrl_generic_remove_group(struct pinctrl_dev
*pctldev
,
686 unsigned int selector
)
688 struct group_desc
*group
;
690 group
= radix_tree_lookup(&pctldev
->pin_group_tree
,
695 radix_tree_delete(&pctldev
->pin_group_tree
, selector
);
696 devm_kfree(pctldev
->dev
, group
);
698 pctldev
->num_groups
--;
702 EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group
);
705 * pinctrl_generic_free_groups() - removes all pin groups
706 * @pctldev: pin controller device
708 * Note that the caller must take care of locking. The pinctrl groups
709 * are allocated with devm_kzalloc() so no need to free them here.
711 static void pinctrl_generic_free_groups(struct pinctrl_dev
*pctldev
)
713 struct radix_tree_iter iter
;
716 radix_tree_for_each_slot(slot
, &pctldev
->pin_group_tree
, &iter
, 0)
717 radix_tree_delete(&pctldev
->pin_group_tree
, iter
.index
);
719 pctldev
->num_groups
= 0;
723 static inline void pinctrl_generic_free_groups(struct pinctrl_dev
*pctldev
)
726 #endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
729 * pinctrl_get_group_selector() - returns the group selector for a group
730 * @pctldev: the pin controller handling the group
731 * @pin_group: the pin group to look up
733 int pinctrl_get_group_selector(struct pinctrl_dev
*pctldev
,
734 const char *pin_group
)
736 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
737 unsigned ngroups
= pctlops
->get_groups_count(pctldev
);
738 unsigned group_selector
= 0;
740 while (group_selector
< ngroups
) {
741 const char *gname
= pctlops
->get_group_name(pctldev
,
743 if (gname
&& !strcmp(gname
, pin_group
)) {
744 dev_dbg(pctldev
->dev
,
745 "found group selector %u for %s\n",
748 return group_selector
;
754 dev_err(pctldev
->dev
, "does not have pin group %s\n",
760 bool pinctrl_gpio_can_use_line(struct gpio_chip
*gc
, unsigned int offset
)
762 struct pinctrl_dev
*pctldev
;
763 struct pinctrl_gpio_range
*range
;
768 * Try to obtain GPIO range, if it fails
769 * we're probably dealing with GPIO driver
770 * without a backing pin controller - bail out.
772 if (pinctrl_get_device_gpio_range(gc
, offset
, &pctldev
, &range
))
775 mutex_lock(&pctldev
->mutex
);
777 /* Convert to the pin controllers number space */
778 pin
= gpio_to_pin(range
, gc
, offset
);
780 result
= pinmux_can_be_used_for_gpio(pctldev
, pin
);
782 mutex_unlock(&pctldev
->mutex
);
786 EXPORT_SYMBOL_GPL(pinctrl_gpio_can_use_line
);
789 * pinctrl_gpio_request() - request a single pin to be used as GPIO
790 * @gc: GPIO chip structure from the GPIO subsystem
791 * @offset: hardware offset of the GPIO relative to the controller
793 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
794 * as part of their gpio_request() semantics, platforms and individual drivers
795 * shall *NOT* request GPIO pins to be muxed in.
797 int pinctrl_gpio_request(struct gpio_chip
*gc
, unsigned int offset
)
799 struct pinctrl_gpio_range
*range
;
800 struct pinctrl_dev
*pctldev
;
803 ret
= pinctrl_get_device_gpio_range(gc
, offset
, &pctldev
, &range
);
805 if (pinctrl_ready_for_gpio_range(gc
, offset
))
810 mutex_lock(&pctldev
->mutex
);
812 /* Convert to the pin controllers number space */
813 pin
= gpio_to_pin(range
, gc
, offset
);
815 ret
= pinmux_request_gpio(pctldev
, range
, pin
, gc
->base
+ offset
);
817 mutex_unlock(&pctldev
->mutex
);
821 EXPORT_SYMBOL_GPL(pinctrl_gpio_request
);
824 * pinctrl_gpio_free() - free control on a single pin, currently used as GPIO
825 * @gc: GPIO chip structure from the GPIO subsystem
826 * @offset: hardware offset of the GPIO relative to the controller
828 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
829 * as part of their gpio_request() semantics, platforms and individual drivers
830 * shall *NOT* request GPIO pins to be muxed in.
832 void pinctrl_gpio_free(struct gpio_chip
*gc
, unsigned int offset
)
834 struct pinctrl_gpio_range
*range
;
835 struct pinctrl_dev
*pctldev
;
838 ret
= pinctrl_get_device_gpio_range(gc
, offset
, &pctldev
, &range
);
842 mutex_lock(&pctldev
->mutex
);
844 /* Convert to the pin controllers number space */
845 pin
= gpio_to_pin(range
, gc
, offset
);
847 pinmux_free_gpio(pctldev
, pin
, range
);
849 mutex_unlock(&pctldev
->mutex
);
851 EXPORT_SYMBOL_GPL(pinctrl_gpio_free
);
853 static int pinctrl_gpio_direction(struct gpio_chip
*gc
, unsigned int offset
,
856 struct pinctrl_dev
*pctldev
;
857 struct pinctrl_gpio_range
*range
;
861 ret
= pinctrl_get_device_gpio_range(gc
, offset
, &pctldev
, &range
);
866 mutex_lock(&pctldev
->mutex
);
868 /* Convert to the pin controllers number space */
869 pin
= gpio_to_pin(range
, gc
, offset
);
870 ret
= pinmux_gpio_direction(pctldev
, range
, pin
, input
);
872 mutex_unlock(&pctldev
->mutex
);
878 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
879 * @gc: GPIO chip structure from the GPIO subsystem
880 * @offset: hardware offset of the GPIO relative to the controller
882 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
883 * as part of their gpio_direction_input() semantics, platforms and individual
884 * drivers shall *NOT* touch pin control GPIO calls.
886 int pinctrl_gpio_direction_input(struct gpio_chip
*gc
, unsigned int offset
)
888 return pinctrl_gpio_direction(gc
, offset
, true);
890 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input
);
893 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
894 * @gc: GPIO chip structure from the GPIO subsystem
895 * @offset: hardware offset of the GPIO relative to the controller
897 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
898 * as part of their gpio_direction_output() semantics, platforms and individual
899 * drivers shall *NOT* touch pin control GPIO calls.
901 int pinctrl_gpio_direction_output(struct gpio_chip
*gc
, unsigned int offset
)
903 return pinctrl_gpio_direction(gc
, offset
, false);
905 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output
);
908 * pinctrl_gpio_set_config() - Apply config to given GPIO pin
909 * @gc: GPIO chip structure from the GPIO subsystem
910 * @offset: hardware offset of the GPIO relative to the controller
911 * @config: the configuration to apply to the GPIO
913 * This function should *ONLY* be used from gpiolib-based GPIO drivers, if
914 * they need to call the underlying pin controller to change GPIO config
915 * (for example set debounce time).
917 int pinctrl_gpio_set_config(struct gpio_chip
*gc
, unsigned int offset
,
918 unsigned long config
)
920 unsigned long configs
[] = { config
};
921 struct pinctrl_gpio_range
*range
;
922 struct pinctrl_dev
*pctldev
;
925 ret
= pinctrl_get_device_gpio_range(gc
, offset
, &pctldev
, &range
);
929 mutex_lock(&pctldev
->mutex
);
930 pin
= gpio_to_pin(range
, gc
, offset
);
931 ret
= pinconf_set_config(pctldev
, pin
, configs
, ARRAY_SIZE(configs
));
932 mutex_unlock(&pctldev
->mutex
);
936 EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config
);
938 static struct pinctrl_state
*find_state(struct pinctrl
*p
,
941 struct pinctrl_state
*state
;
943 list_for_each_entry(state
, &p
->states
, node
)
944 if (!strcmp(state
->name
, name
))
950 static struct pinctrl_state
*create_state(struct pinctrl
*p
,
953 struct pinctrl_state
*state
;
955 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
957 return ERR_PTR(-ENOMEM
);
960 INIT_LIST_HEAD(&state
->settings
);
962 list_add_tail(&state
->node
, &p
->states
);
967 static int add_setting(struct pinctrl
*p
, struct pinctrl_dev
*pctldev
,
968 const struct pinctrl_map
*map
)
970 struct pinctrl_state
*state
;
971 struct pinctrl_setting
*setting
;
974 state
= find_state(p
, map
->name
);
976 state
= create_state(p
, map
->name
);
978 return PTR_ERR(state
);
980 if (map
->type
== PIN_MAP_TYPE_DUMMY_STATE
)
983 setting
= kzalloc(sizeof(*setting
), GFP_KERNEL
);
987 setting
->type
= map
->type
;
990 setting
->pctldev
= pctldev
;
993 get_pinctrl_dev_from_devname(map
->ctrl_dev_name
);
994 if (!setting
->pctldev
) {
996 /* Do not defer probing of hogs (circular loop) */
997 if (!strcmp(map
->ctrl_dev_name
, map
->dev_name
))
1000 * OK let us guess that the driver is not there yet, and
1001 * let's defer obtaining this pinctrl handle to later...
1003 dev_info(p
->dev
, "unknown pinctrl device %s in map entry, deferring probe",
1004 map
->ctrl_dev_name
);
1005 return -EPROBE_DEFER
;
1008 setting
->dev_name
= map
->dev_name
;
1010 switch (map
->type
) {
1011 case PIN_MAP_TYPE_MUX_GROUP
:
1012 ret
= pinmux_map_to_setting(map
, setting
);
1014 case PIN_MAP_TYPE_CONFIGS_PIN
:
1015 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1016 ret
= pinconf_map_to_setting(map
, setting
);
1027 list_add_tail(&setting
->node
, &state
->settings
);
1032 static struct pinctrl
*find_pinctrl(struct device
*dev
)
1036 mutex_lock(&pinctrl_list_mutex
);
1037 list_for_each_entry(p
, &pinctrl_list
, node
)
1038 if (p
->dev
== dev
) {
1039 mutex_unlock(&pinctrl_list_mutex
);
1043 mutex_unlock(&pinctrl_list_mutex
);
1047 static void pinctrl_free(struct pinctrl
*p
, bool inlist
);
1049 static struct pinctrl
*create_pinctrl(struct device
*dev
,
1050 struct pinctrl_dev
*pctldev
)
1053 const char *devname
;
1054 struct pinctrl_maps
*maps_node
;
1055 const struct pinctrl_map
*map
;
1059 * create the state cookie holder struct pinctrl for each
1060 * mapping, this is what consumers will get when requesting
1061 * a pin control handle with pinctrl_get()
1063 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
1065 return ERR_PTR(-ENOMEM
);
1067 INIT_LIST_HEAD(&p
->states
);
1068 INIT_LIST_HEAD(&p
->dt_maps
);
1070 ret
= pinctrl_dt_to_map(p
, pctldev
);
1073 return ERR_PTR(ret
);
1076 devname
= dev_name(dev
);
1078 mutex_lock(&pinctrl_maps_mutex
);
1079 /* Iterate over the pin control maps to locate the right ones */
1080 for_each_pin_map(maps_node
, map
) {
1081 /* Map must be for this device */
1082 if (strcmp(map
->dev_name
, devname
))
1085 * If pctldev is not null, we are claiming hog for it,
1086 * that means, setting that is served by pctldev by itself.
1088 * Thus we must skip map that is for this device but is served
1092 strcmp(dev_name(pctldev
->dev
), map
->ctrl_dev_name
))
1095 ret
= add_setting(p
, pctldev
, map
);
1097 * At this point the adding of a setting may:
1099 * - Defer, if the pinctrl device is not yet available
1100 * - Fail, if the pinctrl device is not yet available,
1101 * AND the setting is a hog. We cannot defer that, since
1102 * the hog will kick in immediately after the device
1105 * If the error returned was not -EPROBE_DEFER then we
1106 * accumulate the errors to see if we end up with
1107 * an -EPROBE_DEFER later, as that is the worst case.
1109 if (ret
== -EPROBE_DEFER
) {
1110 pinctrl_free(p
, false);
1111 mutex_unlock(&pinctrl_maps_mutex
);
1112 return ERR_PTR(ret
);
1115 mutex_unlock(&pinctrl_maps_mutex
);
1118 /* If some other error than deferral occurred, return here */
1119 pinctrl_free(p
, false);
1120 return ERR_PTR(ret
);
1123 kref_init(&p
->users
);
1125 /* Add the pinctrl handle to the global list */
1126 mutex_lock(&pinctrl_list_mutex
);
1127 list_add_tail(&p
->node
, &pinctrl_list
);
1128 mutex_unlock(&pinctrl_list_mutex
);
1134 * pinctrl_get() - retrieves the pinctrl handle for a device
1135 * @dev: the device to obtain the handle for
1137 struct pinctrl
*pinctrl_get(struct device
*dev
)
1142 return ERR_PTR(-EINVAL
);
1145 * See if somebody else (such as the device core) has already
1146 * obtained a handle to the pinctrl for this device. In that case,
1147 * return another pointer to it.
1149 p
= find_pinctrl(dev
);
1151 dev_dbg(dev
, "obtain a copy of previously claimed pinctrl\n");
1152 kref_get(&p
->users
);
1156 return create_pinctrl(dev
, NULL
);
1158 EXPORT_SYMBOL_GPL(pinctrl_get
);
1160 static void pinctrl_free_setting(bool disable_setting
,
1161 struct pinctrl_setting
*setting
)
1163 switch (setting
->type
) {
1164 case PIN_MAP_TYPE_MUX_GROUP
:
1165 if (disable_setting
)
1166 pinmux_disable_setting(setting
);
1167 pinmux_free_setting(setting
);
1169 case PIN_MAP_TYPE_CONFIGS_PIN
:
1170 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1171 pinconf_free_setting(setting
);
1178 static void pinctrl_free(struct pinctrl
*p
, bool inlist
)
1180 struct pinctrl_state
*state
, *n1
;
1181 struct pinctrl_setting
*setting
, *n2
;
1183 mutex_lock(&pinctrl_list_mutex
);
1184 list_for_each_entry_safe(state
, n1
, &p
->states
, node
) {
1185 list_for_each_entry_safe(setting
, n2
, &state
->settings
, node
) {
1186 pinctrl_free_setting(state
== p
->state
, setting
);
1187 list_del(&setting
->node
);
1190 list_del(&state
->node
);
1194 pinctrl_dt_free_maps(p
);
1199 mutex_unlock(&pinctrl_list_mutex
);
1203 * pinctrl_release() - release the pinctrl handle
1204 * @kref: the kref in the pinctrl being released
1206 static void pinctrl_release(struct kref
*kref
)
1208 struct pinctrl
*p
= container_of(kref
, struct pinctrl
, users
);
1210 pinctrl_free(p
, true);
1214 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
1215 * @p: the pinctrl handle to release
1217 void pinctrl_put(struct pinctrl
*p
)
1219 kref_put(&p
->users
, pinctrl_release
);
1221 EXPORT_SYMBOL_GPL(pinctrl_put
);
1224 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
1225 * @p: the pinctrl handle to retrieve the state from
1226 * @name: the state name to retrieve
1228 struct pinctrl_state
*pinctrl_lookup_state(struct pinctrl
*p
,
1231 struct pinctrl_state
*state
;
1233 state
= find_state(p
, name
);
1235 if (pinctrl_dummy_state
) {
1236 /* create dummy state */
1237 dev_dbg(p
->dev
, "using pinctrl dummy state (%s)\n",
1239 state
= create_state(p
, name
);
1241 state
= ERR_PTR(-ENODEV
);
1246 EXPORT_SYMBOL_GPL(pinctrl_lookup_state
);
1248 static void pinctrl_link_add(struct pinctrl_dev
*pctldev
,
1249 struct device
*consumer
)
1251 if (pctldev
->desc
->link_consumers
)
1252 device_link_add(consumer
, pctldev
->dev
,
1253 DL_FLAG_PM_RUNTIME
|
1254 DL_FLAG_AUTOREMOVE_CONSUMER
);
1258 * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
1259 * @p: the pinctrl handle for the device that requests configuration
1260 * @state: the state handle to select/activate/program
1262 static int pinctrl_commit_state(struct pinctrl
*p
, struct pinctrl_state
*state
)
1264 struct pinctrl_setting
*setting
, *setting2
;
1265 struct pinctrl_state
*old_state
= READ_ONCE(p
->state
);
1270 * For each pinmux setting in the old state, forget SW's record
1271 * of mux owner for that pingroup. Any pingroups which are
1272 * still owned by the new state will be re-acquired by the call
1273 * to pinmux_enable_setting() in the loop below.
1275 list_for_each_entry(setting
, &old_state
->settings
, node
) {
1276 if (setting
->type
!= PIN_MAP_TYPE_MUX_GROUP
)
1278 pinmux_disable_setting(setting
);
1284 /* Apply all the settings for the new state - pinmux first */
1285 list_for_each_entry(setting
, &state
->settings
, node
) {
1286 switch (setting
->type
) {
1287 case PIN_MAP_TYPE_MUX_GROUP
:
1288 ret
= pinmux_enable_setting(setting
);
1290 case PIN_MAP_TYPE_CONFIGS_PIN
:
1291 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1300 goto unapply_new_state
;
1302 /* Do not link hogs (circular dependency) */
1303 if (p
!= setting
->pctldev
->p
)
1304 pinctrl_link_add(setting
->pctldev
, p
->dev
);
1307 /* Apply all the settings for the new state - pinconf after */
1308 list_for_each_entry(setting
, &state
->settings
, node
) {
1309 switch (setting
->type
) {
1310 case PIN_MAP_TYPE_MUX_GROUP
:
1313 case PIN_MAP_TYPE_CONFIGS_PIN
:
1314 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1315 ret
= pinconf_apply_setting(setting
);
1323 goto unapply_new_state
;
1326 /* Do not link hogs (circular dependency) */
1327 if (p
!= setting
->pctldev
->p
)
1328 pinctrl_link_add(setting
->pctldev
, p
->dev
);
1336 dev_err(p
->dev
, "Error applying setting, reverse things back\n");
1338 list_for_each_entry(setting2
, &state
->settings
, node
) {
1339 if (&setting2
->node
== &setting
->node
)
1342 * All we can do here is pinmux_disable_setting.
1343 * That means that some pins are muxed differently now
1344 * than they were before applying the setting (We can't
1345 * "unmux a pin"!), but it's not a big deal since the pins
1346 * are free to be muxed by another apply_setting.
1348 if (setting2
->type
== PIN_MAP_TYPE_MUX_GROUP
)
1349 pinmux_disable_setting(setting2
);
1352 /* There's no infinite recursive loop here because p->state is NULL */
1354 pinctrl_select_state(p
, old_state
);
1360 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
1361 * @p: the pinctrl handle for the device that requests configuration
1362 * @state: the state handle to select/activate/program
1364 int pinctrl_select_state(struct pinctrl
*p
, struct pinctrl_state
*state
)
1366 if (p
->state
== state
)
1369 return pinctrl_commit_state(p
, state
);
1371 EXPORT_SYMBOL_GPL(pinctrl_select_state
);
1373 static void devm_pinctrl_release(struct device
*dev
, void *res
)
1375 pinctrl_put(*(struct pinctrl
**)res
);
1379 * devm_pinctrl_get() - Resource managed pinctrl_get()
1380 * @dev: the device to obtain the handle for
1382 * If there is a need to explicitly destroy the returned struct pinctrl,
1383 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1385 struct pinctrl
*devm_pinctrl_get(struct device
*dev
)
1387 struct pinctrl
**ptr
, *p
;
1389 ptr
= devres_alloc(devm_pinctrl_release
, sizeof(*ptr
), GFP_KERNEL
);
1391 return ERR_PTR(-ENOMEM
);
1393 p
= pinctrl_get(dev
);
1396 devres_add(dev
, ptr
);
1403 EXPORT_SYMBOL_GPL(devm_pinctrl_get
);
1405 static int devm_pinctrl_match(struct device
*dev
, void *res
, void *data
)
1407 struct pinctrl
**p
= res
;
1413 * devm_pinctrl_put() - Resource managed pinctrl_put()
1414 * @p: the pinctrl handle to release
1416 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1417 * this function will not need to be called and the resource management
1418 * code will ensure that the resource is freed.
1420 void devm_pinctrl_put(struct pinctrl
*p
)
1422 WARN_ON(devres_release(p
->dev
, devm_pinctrl_release
,
1423 devm_pinctrl_match
, p
));
1425 EXPORT_SYMBOL_GPL(devm_pinctrl_put
);
1428 * pinctrl_register_mappings() - register a set of pin controller mappings
1429 * @maps: the pincontrol mappings table to register. Note the pinctrl-core
1430 * keeps a reference to the passed in maps, so they should _not_ be
1431 * marked with __initdata.
1432 * @num_maps: the number of maps in the mapping table
1434 int pinctrl_register_mappings(const struct pinctrl_map
*maps
,
1438 struct pinctrl_maps
*maps_node
;
1440 pr_debug("add %u pinctrl maps\n", num_maps
);
1442 /* First sanity check the new mapping */
1443 for (i
= 0; i
< num_maps
; i
++) {
1444 if (!maps
[i
].dev_name
) {
1445 pr_err("failed to register map %s (%d): no device given\n",
1450 if (!maps
[i
].name
) {
1451 pr_err("failed to register map %d: no map name given\n",
1456 if (maps
[i
].type
!= PIN_MAP_TYPE_DUMMY_STATE
&&
1457 !maps
[i
].ctrl_dev_name
) {
1458 pr_err("failed to register map %s (%d): no pin control device given\n",
1463 switch (maps
[i
].type
) {
1464 case PIN_MAP_TYPE_DUMMY_STATE
:
1466 case PIN_MAP_TYPE_MUX_GROUP
:
1467 ret
= pinmux_validate_map(&maps
[i
], i
);
1471 case PIN_MAP_TYPE_CONFIGS_PIN
:
1472 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1473 ret
= pinconf_validate_map(&maps
[i
], i
);
1478 pr_err("failed to register map %s (%d): invalid type given\n",
1484 maps_node
= kzalloc(sizeof(*maps_node
), GFP_KERNEL
);
1488 maps_node
->maps
= maps
;
1489 maps_node
->num_maps
= num_maps
;
1491 mutex_lock(&pinctrl_maps_mutex
);
1492 list_add_tail(&maps_node
->node
, &pinctrl_maps
);
1493 mutex_unlock(&pinctrl_maps_mutex
);
1497 EXPORT_SYMBOL_GPL(pinctrl_register_mappings
);
1500 * pinctrl_unregister_mappings() - unregister a set of pin controller mappings
1501 * @map: the pincontrol mappings table passed to pinctrl_register_mappings()
1502 * when registering the mappings.
1504 void pinctrl_unregister_mappings(const struct pinctrl_map
*map
)
1506 struct pinctrl_maps
*maps_node
;
1508 mutex_lock(&pinctrl_maps_mutex
);
1509 list_for_each_entry(maps_node
, &pinctrl_maps
, node
) {
1510 if (maps_node
->maps
== map
) {
1511 list_del(&maps_node
->node
);
1513 mutex_unlock(&pinctrl_maps_mutex
);
1517 mutex_unlock(&pinctrl_maps_mutex
);
1519 EXPORT_SYMBOL_GPL(pinctrl_unregister_mappings
);
1522 * pinctrl_force_sleep() - turn a given controller device into sleep state
1523 * @pctldev: pin controller device
1525 int pinctrl_force_sleep(struct pinctrl_dev
*pctldev
)
1527 if (!IS_ERR(pctldev
->p
) && !IS_ERR(pctldev
->hog_sleep
))
1528 return pinctrl_commit_state(pctldev
->p
, pctldev
->hog_sleep
);
1531 EXPORT_SYMBOL_GPL(pinctrl_force_sleep
);
1534 * pinctrl_force_default() - turn a given controller device into default state
1535 * @pctldev: pin controller device
1537 int pinctrl_force_default(struct pinctrl_dev
*pctldev
)
1539 if (!IS_ERR(pctldev
->p
) && !IS_ERR(pctldev
->hog_default
))
1540 return pinctrl_commit_state(pctldev
->p
, pctldev
->hog_default
);
1543 EXPORT_SYMBOL_GPL(pinctrl_force_default
);
1546 * pinctrl_init_done() - tell pinctrl probe is done
1548 * We'll use this time to switch the pins from "init" to "default" unless the
1549 * driver selected some other state.
1551 * @dev: device to that's done probing
1553 int pinctrl_init_done(struct device
*dev
)
1555 struct dev_pin_info
*pins
= dev
->pins
;
1561 if (IS_ERR(pins
->init_state
))
1562 return 0; /* No such state */
1564 if (pins
->p
->state
!= pins
->init_state
)
1565 return 0; /* Not at init anyway */
1567 if (IS_ERR(pins
->default_state
))
1568 return 0; /* No default state */
1570 ret
= pinctrl_select_state(pins
->p
, pins
->default_state
);
1572 dev_err(dev
, "failed to activate default pinctrl state\n");
1577 static int pinctrl_select_bound_state(struct device
*dev
,
1578 struct pinctrl_state
*state
)
1580 struct dev_pin_info
*pins
= dev
->pins
;
1584 return 0; /* No such state */
1585 ret
= pinctrl_select_state(pins
->p
, state
);
1587 dev_err(dev
, "failed to activate pinctrl state %s\n",
1593 * pinctrl_select_default_state() - select default pinctrl state
1594 * @dev: device to select default state for
1596 int pinctrl_select_default_state(struct device
*dev
)
1601 return pinctrl_select_bound_state(dev
, dev
->pins
->default_state
);
1603 EXPORT_SYMBOL_GPL(pinctrl_select_default_state
);
1608 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1609 * @dev: device to select default state for
1611 int pinctrl_pm_select_default_state(struct device
*dev
)
1613 return pinctrl_select_default_state(dev
);
1615 EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state
);
1618 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1619 * @dev: device to select sleep state for
1621 int pinctrl_pm_select_sleep_state(struct device
*dev
)
1626 return pinctrl_select_bound_state(dev
, dev
->pins
->sleep_state
);
1628 EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state
);
1631 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1632 * @dev: device to select idle state for
1634 int pinctrl_pm_select_idle_state(struct device
*dev
)
1639 return pinctrl_select_bound_state(dev
, dev
->pins
->idle_state
);
1641 EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state
);
1644 #ifdef CONFIG_DEBUG_FS
1646 static int pinctrl_pins_show(struct seq_file
*s
, void *what
)
1648 struct pinctrl_dev
*pctldev
= s
->private;
1649 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1651 #ifdef CONFIG_GPIOLIB
1652 struct pinctrl_gpio_range
*range
;
1653 struct gpio_chip
*chip
;
1657 seq_printf(s
, "registered pins: %d\n", pctldev
->desc
->npins
);
1659 mutex_lock(&pctldev
->mutex
);
1661 /* The pin number can be retrived from the pin controller descriptor */
1662 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
1663 struct pin_desc
*desc
;
1665 pin
= pctldev
->desc
->pins
[i
].number
;
1666 desc
= pin_desc_get(pctldev
, pin
);
1667 /* Pin space may be sparse */
1671 seq_printf(s
, "pin %d (%s) ", pin
, desc
->name
);
1673 #ifdef CONFIG_GPIOLIB
1675 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
1676 if ((pin
>= range
->pin_base
) &&
1677 (pin
< (range
->pin_base
+ range
->npins
))) {
1678 gpio_num
= range
->base
+ (pin
- range
->pin_base
);
1684 * FIXME: gpio_num comes from the global GPIO numberspace.
1685 * we need to get rid of the range->base eventually and
1686 * get the descriptor directly from the gpio_chip.
1688 chip
= gpiod_to_chip(gpio_to_desc(gpio_num
));
1692 seq_printf(s
, "%u:%s ", gpio_num
- chip
->gpiodev
->base
, chip
->label
);
1694 seq_puts(s
, "0:? ");
1697 /* Driver-specific info per pin */
1698 if (ops
->pin_dbg_show
)
1699 ops
->pin_dbg_show(pctldev
, s
, pin
);
1704 mutex_unlock(&pctldev
->mutex
);
1708 DEFINE_SHOW_ATTRIBUTE(pinctrl_pins
);
1710 static int pinctrl_groups_show(struct seq_file
*s
, void *what
)
1712 struct pinctrl_dev
*pctldev
= s
->private;
1713 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1714 unsigned ngroups
, selector
= 0;
1716 mutex_lock(&pctldev
->mutex
);
1718 ngroups
= ops
->get_groups_count(pctldev
);
1720 seq_puts(s
, "registered pin groups:\n");
1721 while (selector
< ngroups
) {
1722 const unsigned *pins
= NULL
;
1723 unsigned num_pins
= 0;
1724 const char *gname
= ops
->get_group_name(pctldev
, selector
);
1729 if (ops
->get_group_pins
)
1730 ret
= ops
->get_group_pins(pctldev
, selector
,
1733 seq_printf(s
, "%s [ERROR GETTING PINS]\n",
1736 seq_printf(s
, "group: %s\n", gname
);
1737 for (i
= 0; i
< num_pins
; i
++) {
1738 pname
= pin_get_name(pctldev
, pins
[i
]);
1739 if (WARN_ON(!pname
)) {
1740 mutex_unlock(&pctldev
->mutex
);
1743 seq_printf(s
, "pin %d (%s)\n", pins
[i
], pname
);
1750 mutex_unlock(&pctldev
->mutex
);
1754 DEFINE_SHOW_ATTRIBUTE(pinctrl_groups
);
1756 static int pinctrl_gpioranges_show(struct seq_file
*s
, void *what
)
1758 struct pinctrl_dev
*pctldev
= s
->private;
1759 struct pinctrl_gpio_range
*range
;
1761 seq_puts(s
, "GPIO ranges handled:\n");
1763 mutex_lock(&pctldev
->mutex
);
1765 /* Loop over the ranges */
1766 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
1769 seq_printf(s
, "%u: %s GPIOS [%u - %u] PINS {",
1770 range
->id
, range
->name
,
1771 range
->base
, (range
->base
+ range
->npins
- 1));
1772 for (a
= 0; a
< range
->npins
- 1; a
++)
1773 seq_printf(s
, "%u, ", range
->pins
[a
]);
1774 seq_printf(s
, "%u}\n", range
->pins
[a
]);
1777 seq_printf(s
, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1778 range
->id
, range
->name
,
1779 range
->base
, (range
->base
+ range
->npins
- 1),
1781 (range
->pin_base
+ range
->npins
- 1));
1784 mutex_unlock(&pctldev
->mutex
);
1788 DEFINE_SHOW_ATTRIBUTE(pinctrl_gpioranges
);
1790 static int pinctrl_devices_show(struct seq_file
*s
, void *what
)
1792 struct pinctrl_dev
*pctldev
;
1794 seq_puts(s
, "name [pinmux] [pinconf]\n");
1796 mutex_lock(&pinctrldev_list_mutex
);
1798 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
1799 seq_printf(s
, "%s ", pctldev
->desc
->name
);
1800 if (pctldev
->desc
->pmxops
)
1801 seq_puts(s
, "yes ");
1804 if (pctldev
->desc
->confops
)
1811 mutex_unlock(&pinctrldev_list_mutex
);
1815 DEFINE_SHOW_ATTRIBUTE(pinctrl_devices
);
1817 static inline const char *map_type(enum pinctrl_map_type type
)
1819 static const char * const names
[] = {
1827 if (type
>= ARRAY_SIZE(names
))
1833 static int pinctrl_maps_show(struct seq_file
*s
, void *what
)
1835 struct pinctrl_maps
*maps_node
;
1836 const struct pinctrl_map
*map
;
1838 seq_puts(s
, "Pinctrl maps:\n");
1840 mutex_lock(&pinctrl_maps_mutex
);
1841 for_each_pin_map(maps_node
, map
) {
1842 seq_printf(s
, "device %s\nstate %s\ntype %s (%d)\n",
1843 map
->dev_name
, map
->name
, map_type(map
->type
),
1846 if (map
->type
!= PIN_MAP_TYPE_DUMMY_STATE
)
1847 seq_printf(s
, "controlling device %s\n",
1848 map
->ctrl_dev_name
);
1850 switch (map
->type
) {
1851 case PIN_MAP_TYPE_MUX_GROUP
:
1852 pinmux_show_map(s
, map
);
1854 case PIN_MAP_TYPE_CONFIGS_PIN
:
1855 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1856 pinconf_show_map(s
, map
);
1864 mutex_unlock(&pinctrl_maps_mutex
);
1868 DEFINE_SHOW_ATTRIBUTE(pinctrl_maps
);
1870 static int pinctrl_show(struct seq_file
*s
, void *what
)
1873 struct pinctrl_state
*state
;
1874 struct pinctrl_setting
*setting
;
1876 seq_puts(s
, "Requested pin control handlers their pinmux maps:\n");
1878 mutex_lock(&pinctrl_list_mutex
);
1880 list_for_each_entry(p
, &pinctrl_list
, node
) {
1881 seq_printf(s
, "device: %s current state: %s\n",
1883 p
->state
? p
->state
->name
: "none");
1885 list_for_each_entry(state
, &p
->states
, node
) {
1886 seq_printf(s
, " state: %s\n", state
->name
);
1888 list_for_each_entry(setting
, &state
->settings
, node
) {
1889 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
1891 seq_printf(s
, " type: %s controller %s ",
1892 map_type(setting
->type
),
1893 pinctrl_dev_get_name(pctldev
));
1895 switch (setting
->type
) {
1896 case PIN_MAP_TYPE_MUX_GROUP
:
1897 pinmux_show_setting(s
, setting
);
1899 case PIN_MAP_TYPE_CONFIGS_PIN
:
1900 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1901 pinconf_show_setting(s
, setting
);
1910 mutex_unlock(&pinctrl_list_mutex
);
1914 DEFINE_SHOW_ATTRIBUTE(pinctrl
);
1916 static struct dentry
*debugfs_root
;
1918 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1920 struct dentry
*device_root
;
1921 const char *debugfs_name
;
1923 if (pctldev
->desc
->name
&&
1924 strcmp(dev_name(pctldev
->dev
), pctldev
->desc
->name
)) {
1925 debugfs_name
= devm_kasprintf(pctldev
->dev
, GFP_KERNEL
,
1926 "%s-%s", dev_name(pctldev
->dev
),
1927 pctldev
->desc
->name
);
1928 if (!debugfs_name
) {
1929 pr_warn("failed to determine debugfs dir name for %s\n",
1930 dev_name(pctldev
->dev
));
1934 debugfs_name
= dev_name(pctldev
->dev
);
1937 device_root
= debugfs_create_dir(debugfs_name
, debugfs_root
);
1938 pctldev
->device_root
= device_root
;
1940 if (IS_ERR(device_root
) || !device_root
) {
1941 pr_warn("failed to create debugfs directory for %s\n",
1942 dev_name(pctldev
->dev
));
1945 debugfs_create_file("pins", 0444,
1946 device_root
, pctldev
, &pinctrl_pins_fops
);
1947 debugfs_create_file("pingroups", 0444,
1948 device_root
, pctldev
, &pinctrl_groups_fops
);
1949 debugfs_create_file("gpio-ranges", 0444,
1950 device_root
, pctldev
, &pinctrl_gpioranges_fops
);
1951 if (pctldev
->desc
->pmxops
)
1952 pinmux_init_device_debugfs(device_root
, pctldev
);
1953 if (pctldev
->desc
->confops
)
1954 pinconf_init_device_debugfs(device_root
, pctldev
);
1957 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1959 debugfs_remove_recursive(pctldev
->device_root
);
1962 static void pinctrl_init_debugfs(void)
1964 debugfs_root
= debugfs_create_dir("pinctrl", NULL
);
1965 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
1966 pr_warn("failed to create debugfs directory\n");
1967 debugfs_root
= NULL
;
1971 debugfs_create_file("pinctrl-devices", 0444,
1972 debugfs_root
, NULL
, &pinctrl_devices_fops
);
1973 debugfs_create_file("pinctrl-maps", 0444,
1974 debugfs_root
, NULL
, &pinctrl_maps_fops
);
1975 debugfs_create_file("pinctrl-handles", 0444,
1976 debugfs_root
, NULL
, &pinctrl_fops
);
1979 #else /* CONFIG_DEBUG_FS */
1981 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1985 static void pinctrl_init_debugfs(void)
1989 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1995 static int pinctrl_check_ops(struct pinctrl_dev
*pctldev
)
1997 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
2000 !ops
->get_groups_count
||
2001 !ops
->get_group_name
)
2008 * pinctrl_init_controller() - init a pin controller device
2009 * @pctldesc: descriptor for this pin controller
2010 * @dev: parent device for this pin controller
2011 * @driver_data: private pin controller data for this pin controller
2013 static struct pinctrl_dev
*
2014 pinctrl_init_controller(struct pinctrl_desc
*pctldesc
, struct device
*dev
,
2017 struct pinctrl_dev
*pctldev
;
2021 return ERR_PTR(-EINVAL
);
2022 if (!pctldesc
->name
)
2023 return ERR_PTR(-EINVAL
);
2025 pctldev
= kzalloc(sizeof(*pctldev
), GFP_KERNEL
);
2027 return ERR_PTR(-ENOMEM
);
2029 /* Initialize pin control device struct */
2030 pctldev
->owner
= pctldesc
->owner
;
2031 pctldev
->desc
= pctldesc
;
2032 pctldev
->driver_data
= driver_data
;
2033 INIT_RADIX_TREE(&pctldev
->pin_desc_tree
, GFP_KERNEL
);
2034 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
2035 INIT_RADIX_TREE(&pctldev
->pin_group_tree
, GFP_KERNEL
);
2037 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
2038 INIT_RADIX_TREE(&pctldev
->pin_function_tree
, GFP_KERNEL
);
2040 INIT_LIST_HEAD(&pctldev
->gpio_ranges
);
2041 INIT_LIST_HEAD(&pctldev
->node
);
2043 mutex_init(&pctldev
->mutex
);
2045 /* check core ops for sanity */
2046 ret
= pinctrl_check_ops(pctldev
);
2048 dev_err(dev
, "pinctrl ops lacks necessary functions\n");
2052 /* If we're implementing pinmuxing, check the ops for sanity */
2053 if (pctldesc
->pmxops
) {
2054 ret
= pinmux_check_ops(pctldev
);
2059 /* If we're implementing pinconfig, check the ops for sanity */
2060 if (pctldesc
->confops
) {
2061 ret
= pinconf_check_ops(pctldev
);
2066 /* Register all the pins */
2067 dev_dbg(dev
, "try to register %d pins ...\n", pctldesc
->npins
);
2068 ret
= pinctrl_register_pins(pctldev
, pctldesc
->pins
, pctldesc
->npins
);
2070 dev_err(dev
, "error during pin registration\n");
2071 pinctrl_free_pindescs(pctldev
, pctldesc
->pins
,
2079 mutex_destroy(&pctldev
->mutex
);
2081 return ERR_PTR(ret
);
2084 static int pinctrl_claim_hogs(struct pinctrl_dev
*pctldev
)
2086 pctldev
->p
= create_pinctrl(pctldev
->dev
, pctldev
);
2087 if (PTR_ERR(pctldev
->p
) == -ENODEV
) {
2088 dev_dbg(pctldev
->dev
, "no hogs found\n");
2093 if (IS_ERR(pctldev
->p
)) {
2094 dev_err(pctldev
->dev
, "error claiming hogs: %li\n",
2095 PTR_ERR(pctldev
->p
));
2097 return PTR_ERR(pctldev
->p
);
2100 pctldev
->hog_default
=
2101 pinctrl_lookup_state(pctldev
->p
, PINCTRL_STATE_DEFAULT
);
2102 if (IS_ERR(pctldev
->hog_default
)) {
2103 dev_dbg(pctldev
->dev
,
2104 "failed to lookup the default state\n");
2106 if (pinctrl_select_state(pctldev
->p
,
2107 pctldev
->hog_default
))
2108 dev_err(pctldev
->dev
,
2109 "failed to select default state\n");
2112 pctldev
->hog_sleep
=
2113 pinctrl_lookup_state(pctldev
->p
,
2114 PINCTRL_STATE_SLEEP
);
2115 if (IS_ERR(pctldev
->hog_sleep
))
2116 dev_dbg(pctldev
->dev
,
2117 "failed to lookup the sleep state\n");
2122 int pinctrl_enable(struct pinctrl_dev
*pctldev
)
2126 error
= pinctrl_claim_hogs(pctldev
);
2128 dev_err(pctldev
->dev
, "could not claim hogs: %i\n",
2130 pinctrl_free_pindescs(pctldev
, pctldev
->desc
->pins
,
2131 pctldev
->desc
->npins
);
2132 mutex_destroy(&pctldev
->mutex
);
2138 mutex_lock(&pinctrldev_list_mutex
);
2139 list_add_tail(&pctldev
->node
, &pinctrldev_list
);
2140 mutex_unlock(&pinctrldev_list_mutex
);
2142 pinctrl_init_device_debugfs(pctldev
);
2146 EXPORT_SYMBOL_GPL(pinctrl_enable
);
2149 * pinctrl_register() - register a pin controller device
2150 * @pctldesc: descriptor for this pin controller
2151 * @dev: parent device for this pin controller
2152 * @driver_data: private pin controller data for this pin controller
2154 * Note that pinctrl_register() is known to have problems as the pin
2155 * controller driver functions are called before the driver has a
2156 * struct pinctrl_dev handle. To avoid issues later on, please use the
2157 * new pinctrl_register_and_init() below instead.
2159 struct pinctrl_dev
*pinctrl_register(struct pinctrl_desc
*pctldesc
,
2160 struct device
*dev
, void *driver_data
)
2162 struct pinctrl_dev
*pctldev
;
2165 pctldev
= pinctrl_init_controller(pctldesc
, dev
, driver_data
);
2166 if (IS_ERR(pctldev
))
2169 error
= pinctrl_enable(pctldev
);
2171 return ERR_PTR(error
);
2175 EXPORT_SYMBOL_GPL(pinctrl_register
);
2178 * pinctrl_register_and_init() - register and init pin controller device
2179 * @pctldesc: descriptor for this pin controller
2180 * @dev: parent device for this pin controller
2181 * @driver_data: private pin controller data for this pin controller
2182 * @pctldev: pin controller device
2184 * Note that pinctrl_enable() still needs to be manually called after
2185 * this once the driver is ready.
2187 int pinctrl_register_and_init(struct pinctrl_desc
*pctldesc
,
2188 struct device
*dev
, void *driver_data
,
2189 struct pinctrl_dev
**pctldev
)
2191 struct pinctrl_dev
*p
;
2193 p
= pinctrl_init_controller(pctldesc
, dev
, driver_data
);
2198 * We have pinctrl_start() call functions in the pin controller
2199 * driver with create_pinctrl() for at least dt_node_to_map(). So
2200 * let's make sure pctldev is properly initialized for the
2201 * pin controller driver before we do anything.
2207 EXPORT_SYMBOL_GPL(pinctrl_register_and_init
);
2210 * pinctrl_unregister() - unregister pinmux
2211 * @pctldev: pin controller to unregister
2213 * Called by pinmux drivers to unregister a pinmux.
2215 void pinctrl_unregister(struct pinctrl_dev
*pctldev
)
2217 struct pinctrl_gpio_range
*range
, *n
;
2222 mutex_lock(&pctldev
->mutex
);
2223 pinctrl_remove_device_debugfs(pctldev
);
2224 mutex_unlock(&pctldev
->mutex
);
2226 if (!IS_ERR_OR_NULL(pctldev
->p
))
2227 pinctrl_put(pctldev
->p
);
2229 mutex_lock(&pinctrldev_list_mutex
);
2230 mutex_lock(&pctldev
->mutex
);
2231 /* TODO: check that no pinmuxes are still active? */
2232 list_del(&pctldev
->node
);
2233 pinmux_generic_free_functions(pctldev
);
2234 pinctrl_generic_free_groups(pctldev
);
2235 /* Destroy descriptor tree */
2236 pinctrl_free_pindescs(pctldev
, pctldev
->desc
->pins
,
2237 pctldev
->desc
->npins
);
2238 /* remove gpio ranges map */
2239 list_for_each_entry_safe(range
, n
, &pctldev
->gpio_ranges
, node
)
2240 list_del(&range
->node
);
2242 mutex_unlock(&pctldev
->mutex
);
2243 mutex_destroy(&pctldev
->mutex
);
2245 mutex_unlock(&pinctrldev_list_mutex
);
2247 EXPORT_SYMBOL_GPL(pinctrl_unregister
);
2249 static void devm_pinctrl_dev_release(struct device
*dev
, void *res
)
2251 struct pinctrl_dev
*pctldev
= *(struct pinctrl_dev
**)res
;
2253 pinctrl_unregister(pctldev
);
2256 static int devm_pinctrl_dev_match(struct device
*dev
, void *res
, void *data
)
2258 struct pctldev
**r
= res
;
2260 if (WARN_ON(!r
|| !*r
))
2267 * devm_pinctrl_register() - Resource managed version of pinctrl_register().
2268 * @dev: parent device for this pin controller
2269 * @pctldesc: descriptor for this pin controller
2270 * @driver_data: private pin controller data for this pin controller
2272 * Returns an error pointer if pincontrol register failed. Otherwise
2273 * it returns valid pinctrl handle.
2275 * The pinctrl device will be automatically released when the device is unbound.
2277 struct pinctrl_dev
*devm_pinctrl_register(struct device
*dev
,
2278 struct pinctrl_desc
*pctldesc
,
2281 struct pinctrl_dev
**ptr
, *pctldev
;
2283 ptr
= devres_alloc(devm_pinctrl_dev_release
, sizeof(*ptr
), GFP_KERNEL
);
2285 return ERR_PTR(-ENOMEM
);
2287 pctldev
= pinctrl_register(pctldesc
, dev
, driver_data
);
2288 if (IS_ERR(pctldev
)) {
2294 devres_add(dev
, ptr
);
2298 EXPORT_SYMBOL_GPL(devm_pinctrl_register
);
2301 * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
2302 * @dev: parent device for this pin controller
2303 * @pctldesc: descriptor for this pin controller
2304 * @driver_data: private pin controller data for this pin controller
2305 * @pctldev: pin controller device
2307 * Returns zero on success or an error number on failure.
2309 * The pinctrl device will be automatically released when the device is unbound.
2311 int devm_pinctrl_register_and_init(struct device
*dev
,
2312 struct pinctrl_desc
*pctldesc
,
2314 struct pinctrl_dev
**pctldev
)
2316 struct pinctrl_dev
**ptr
;
2319 ptr
= devres_alloc(devm_pinctrl_dev_release
, sizeof(*ptr
), GFP_KERNEL
);
2323 error
= pinctrl_register_and_init(pctldesc
, dev
, driver_data
, pctldev
);
2330 devres_add(dev
, ptr
);
2334 EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init
);
2337 * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
2338 * @dev: device for which resource was allocated
2339 * @pctldev: the pinctrl device to unregister.
2341 void devm_pinctrl_unregister(struct device
*dev
, struct pinctrl_dev
*pctldev
)
2343 WARN_ON(devres_release(dev
, devm_pinctrl_dev_release
,
2344 devm_pinctrl_dev_match
, pctldev
));
2346 EXPORT_SYMBOL_GPL(devm_pinctrl_unregister
);
2348 static int __init
pinctrl_init(void)
2350 pr_info("initialized pinctrl subsystem\n");
2351 pinctrl_init_debugfs();
2355 /* init early since many drivers really need to initialized pinmux early */
2356 core_initcall(pinctrl_init
);