2 * Core driver for the pin control subsystem
4 * Copyright (C) 2011-2012 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
8 * Author: Linus Walleij <linus.walleij@linaro.org>
10 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
12 * License terms: GNU General Public License (GPL) version 2
14 #define pr_fmt(fmt) "pinctrl core: " fmt
16 #include <linux/kernel.h>
17 #include <linux/kref.h>
18 #include <linux/export.h>
19 #include <linux/init.h>
20 #include <linux/device.h>
21 #include <linux/slab.h>
22 #include <linux/err.h>
23 #include <linux/list.h>
24 #include <linux/sysfs.h>
25 #include <linux/debugfs.h>
26 #include <linux/seq_file.h>
27 #include <linux/pinctrl/consumer.h>
28 #include <linux/pinctrl/pinctrl.h>
29 #include <linux/pinctrl/machine.h>
32 #include <asm-generic/gpio.h>
36 #include "devicetree.h"
41 static bool pinctrl_dummy_state
;
43 /* Mutex taken to protect pinctrl_list */
44 static DEFINE_MUTEX(pinctrl_list_mutex
);
46 /* Mutex taken to protect pinctrl_maps */
47 DEFINE_MUTEX(pinctrl_maps_mutex
);
49 /* Mutex taken to protect pinctrldev_list */
50 static DEFINE_MUTEX(pinctrldev_list_mutex
);
52 /* Global list of pin control devices (struct pinctrl_dev) */
53 static LIST_HEAD(pinctrldev_list
);
55 /* List of pin controller handles (struct pinctrl) */
56 static LIST_HEAD(pinctrl_list
);
58 /* List of pinctrl maps (struct pinctrl_maps) */
59 LIST_HEAD(pinctrl_maps
);
63 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
65 * Usually this function is called by platforms without pinctrl driver support
66 * but run with some shared drivers using pinctrl APIs.
67 * After calling this function, the pinctrl core will return successfully
68 * with creating a dummy state for the driver to keep going smoothly.
70 void pinctrl_provide_dummies(void)
72 pinctrl_dummy_state
= true;
75 const char *pinctrl_dev_get_name(struct pinctrl_dev
*pctldev
)
77 /* We're not allowed to register devices without name */
78 return pctldev
->desc
->name
;
80 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name
);
82 const char *pinctrl_dev_get_devname(struct pinctrl_dev
*pctldev
)
84 return dev_name(pctldev
->dev
);
86 EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname
);
88 void *pinctrl_dev_get_drvdata(struct pinctrl_dev
*pctldev
)
90 return pctldev
->driver_data
;
92 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata
);
95 * get_pinctrl_dev_from_devname() - look up pin controller device
96 * @devname: the name of a device instance, as returned by dev_name()
98 * Looks up a pin control device matching a certain device name or pure device
99 * pointer, the pure device pointer will take precedence.
101 struct pinctrl_dev
*get_pinctrl_dev_from_devname(const char *devname
)
103 struct pinctrl_dev
*pctldev
= NULL
;
108 mutex_lock(&pinctrldev_list_mutex
);
110 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
111 if (!strcmp(dev_name(pctldev
->dev
), devname
)) {
112 /* Matched on device name */
113 mutex_unlock(&pinctrldev_list_mutex
);
118 mutex_unlock(&pinctrldev_list_mutex
);
123 struct pinctrl_dev
*get_pinctrl_dev_from_of_node(struct device_node
*np
)
125 struct pinctrl_dev
*pctldev
;
127 mutex_lock(&pinctrldev_list_mutex
);
129 list_for_each_entry(pctldev
, &pinctrldev_list
, node
)
130 if (pctldev
->dev
->of_node
== np
) {
131 mutex_unlock(&pinctrldev_list_mutex
);
135 mutex_unlock(&pinctrldev_list_mutex
);
141 * pin_get_from_name() - look up a pin number from a name
142 * @pctldev: the pin control device to lookup the pin on
143 * @name: the name of the pin to look up
145 int pin_get_from_name(struct pinctrl_dev
*pctldev
, const char *name
)
149 /* The pin number can be retrived from the pin controller descriptor */
150 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
151 struct pin_desc
*desc
;
153 pin
= pctldev
->desc
->pins
[i
].number
;
154 desc
= pin_desc_get(pctldev
, pin
);
155 /* Pin space may be sparse */
156 if (desc
&& !strcmp(name
, desc
->name
))
164 * pin_get_name_from_id() - look up a pin name from a pin id
165 * @pctldev: the pin control device to lookup the pin on
166 * @name: the name of the pin to look up
168 const char *pin_get_name(struct pinctrl_dev
*pctldev
, const unsigned pin
)
170 const struct pin_desc
*desc
;
172 desc
= pin_desc_get(pctldev
, pin
);
174 dev_err(pctldev
->dev
, "failed to get pin(%d) name\n",
183 * pin_is_valid() - check if pin exists on controller
184 * @pctldev: the pin control device to check the pin on
185 * @pin: pin to check, use the local pin controller index number
187 * This tells us whether a certain pin exist on a certain pin controller or
188 * not. Pin lists may be sparse, so some pins may not exist.
190 bool pin_is_valid(struct pinctrl_dev
*pctldev
, int pin
)
192 struct pin_desc
*pindesc
;
197 mutex_lock(&pctldev
->mutex
);
198 pindesc
= pin_desc_get(pctldev
, pin
);
199 mutex_unlock(&pctldev
->mutex
);
201 return pindesc
!= NULL
;
203 EXPORT_SYMBOL_GPL(pin_is_valid
);
205 /* Deletes a range of pin descriptors */
206 static void pinctrl_free_pindescs(struct pinctrl_dev
*pctldev
,
207 const struct pinctrl_pin_desc
*pins
,
212 for (i
= 0; i
< num_pins
; i
++) {
213 struct pin_desc
*pindesc
;
215 pindesc
= radix_tree_lookup(&pctldev
->pin_desc_tree
,
217 if (pindesc
!= NULL
) {
218 radix_tree_delete(&pctldev
->pin_desc_tree
,
220 if (pindesc
->dynamic_name
)
221 kfree(pindesc
->name
);
227 static int pinctrl_register_one_pin(struct pinctrl_dev
*pctldev
,
228 const struct pinctrl_pin_desc
*pin
)
230 struct pin_desc
*pindesc
;
232 pindesc
= pin_desc_get(pctldev
, pin
->number
);
233 if (pindesc
!= NULL
) {
234 dev_err(pctldev
->dev
, "pin %d already registered\n",
239 pindesc
= kzalloc(sizeof(*pindesc
), GFP_KERNEL
);
244 pindesc
->pctldev
= pctldev
;
246 /* Copy basic pin info */
248 pindesc
->name
= pin
->name
;
250 pindesc
->name
= kasprintf(GFP_KERNEL
, "PIN%u", pin
->number
);
251 if (pindesc
->name
== NULL
) {
255 pindesc
->dynamic_name
= true;
258 pindesc
->drv_data
= pin
->drv_data
;
260 radix_tree_insert(&pctldev
->pin_desc_tree
, pin
->number
, pindesc
);
261 pr_debug("registered pin %d (%s) on %s\n",
262 pin
->number
, pindesc
->name
, pctldev
->desc
->name
);
266 static int pinctrl_register_pins(struct pinctrl_dev
*pctldev
,
267 struct pinctrl_pin_desc
const *pins
,
273 for (i
= 0; i
< num_descs
; i
++) {
274 ret
= pinctrl_register_one_pin(pctldev
, &pins
[i
]);
283 * gpio_to_pin() - GPIO range GPIO number to pin number translation
284 * @range: GPIO range used for the translation
285 * @gpio: gpio pin to translate to a pin number
287 * Finds the pin number for a given GPIO using the specified GPIO range
288 * as a base for translation. The distinction between linear GPIO ranges
289 * and pin list based GPIO ranges is managed correctly by this function.
291 * This function assumes the gpio is part of the specified GPIO range, use
292 * only after making sure this is the case (e.g. by calling it on the
293 * result of successful pinctrl_get_device_gpio_range calls)!
295 static inline int gpio_to_pin(struct pinctrl_gpio_range
*range
,
298 unsigned int offset
= gpio
- range
->base
;
300 return range
->pins
[offset
];
302 return range
->pin_base
+ offset
;
306 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
307 * @pctldev: pin controller device to check
308 * @gpio: gpio pin to check taken from the global GPIO pin space
310 * Tries to match a GPIO pin number to the ranges handled by a certain pin
311 * controller, return the range or NULL
313 static struct pinctrl_gpio_range
*
314 pinctrl_match_gpio_range(struct pinctrl_dev
*pctldev
, unsigned gpio
)
316 struct pinctrl_gpio_range
*range
= NULL
;
318 mutex_lock(&pctldev
->mutex
);
319 /* Loop over the ranges */
320 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
321 /* Check if we're in the valid range */
322 if (gpio
>= range
->base
&&
323 gpio
< range
->base
+ range
->npins
) {
324 mutex_unlock(&pctldev
->mutex
);
328 mutex_unlock(&pctldev
->mutex
);
333 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
334 * the same GPIO chip are in range
335 * @gpio: gpio pin to check taken from the global GPIO pin space
337 * This function is complement of pinctrl_match_gpio_range(). If the return
338 * value of pinctrl_match_gpio_range() is NULL, this function could be used
339 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
340 * of the same GPIO chip don't have back-end pinctrl interface.
341 * If the return value is true, it means that pinctrl device is ready & the
342 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
343 * is false, it means that pinctrl device may not be ready.
345 #ifdef CONFIG_GPIOLIB
346 static bool pinctrl_ready_for_gpio_range(unsigned gpio
)
348 struct pinctrl_dev
*pctldev
;
349 struct pinctrl_gpio_range
*range
= NULL
;
350 struct gpio_chip
*chip
= gpio_to_chip(gpio
);
352 if (WARN(!chip
, "no gpio_chip for gpio%i?", gpio
))
355 mutex_lock(&pinctrldev_list_mutex
);
357 /* Loop over the pin controllers */
358 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
359 /* Loop over the ranges */
360 mutex_lock(&pctldev
->mutex
);
361 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
362 /* Check if any gpio range overlapped with gpio chip */
363 if (range
->base
+ range
->npins
- 1 < chip
->base
||
364 range
->base
> chip
->base
+ chip
->ngpio
- 1)
366 mutex_unlock(&pctldev
->mutex
);
367 mutex_unlock(&pinctrldev_list_mutex
);
370 mutex_unlock(&pctldev
->mutex
);
373 mutex_unlock(&pinctrldev_list_mutex
);
378 static bool pinctrl_ready_for_gpio_range(unsigned gpio
) { return true; }
382 * pinctrl_get_device_gpio_range() - find device for GPIO range
383 * @gpio: the pin to locate the pin controller for
384 * @outdev: the pin control device if found
385 * @outrange: the GPIO range if found
387 * Find the pin controller handling a certain GPIO pin from the pinspace of
388 * the GPIO subsystem, return the device and the matching GPIO range. Returns
389 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
390 * may still have not been registered.
392 static int pinctrl_get_device_gpio_range(unsigned gpio
,
393 struct pinctrl_dev
**outdev
,
394 struct pinctrl_gpio_range
**outrange
)
396 struct pinctrl_dev
*pctldev
= NULL
;
398 mutex_lock(&pinctrldev_list_mutex
);
400 /* Loop over the pin controllers */
401 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
402 struct pinctrl_gpio_range
*range
;
404 range
= pinctrl_match_gpio_range(pctldev
, gpio
);
408 mutex_unlock(&pinctrldev_list_mutex
);
413 mutex_unlock(&pinctrldev_list_mutex
);
415 return -EPROBE_DEFER
;
419 * pinctrl_add_gpio_range() - register a GPIO range for a controller
420 * @pctldev: pin controller device to add the range to
421 * @range: the GPIO range to add
423 * This adds a range of GPIOs to be handled by a certain pin controller. Call
424 * this to register handled ranges after registering your pin controller.
426 void pinctrl_add_gpio_range(struct pinctrl_dev
*pctldev
,
427 struct pinctrl_gpio_range
*range
)
429 mutex_lock(&pctldev
->mutex
);
430 list_add_tail(&range
->node
, &pctldev
->gpio_ranges
);
431 mutex_unlock(&pctldev
->mutex
);
433 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range
);
435 void pinctrl_add_gpio_ranges(struct pinctrl_dev
*pctldev
,
436 struct pinctrl_gpio_range
*ranges
,
441 for (i
= 0; i
< nranges
; i
++)
442 pinctrl_add_gpio_range(pctldev
, &ranges
[i
]);
444 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges
);
446 struct pinctrl_dev
*pinctrl_find_and_add_gpio_range(const char *devname
,
447 struct pinctrl_gpio_range
*range
)
449 struct pinctrl_dev
*pctldev
;
451 pctldev
= get_pinctrl_dev_from_devname(devname
);
454 * If we can't find this device, let's assume that is because
455 * it has not probed yet, so the driver trying to register this
456 * range need to defer probing.
459 return ERR_PTR(-EPROBE_DEFER
);
461 pinctrl_add_gpio_range(pctldev
, range
);
465 EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range
);
467 int pinctrl_get_group_pins(struct pinctrl_dev
*pctldev
, const char *pin_group
,
468 const unsigned **pins
, unsigned *num_pins
)
470 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
473 if (!pctlops
->get_group_pins
)
476 gs
= pinctrl_get_group_selector(pctldev
, pin_group
);
480 return pctlops
->get_group_pins(pctldev
, gs
, pins
, num_pins
);
482 EXPORT_SYMBOL_GPL(pinctrl_get_group_pins
);
484 struct pinctrl_gpio_range
*
485 pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev
*pctldev
,
488 struct pinctrl_gpio_range
*range
;
490 /* Loop over the ranges */
491 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
492 /* Check if we're in the valid range */
495 for (a
= 0; a
< range
->npins
; a
++) {
496 if (range
->pins
[a
] == pin
)
499 } else if (pin
>= range
->pin_base
&&
500 pin
< range
->pin_base
+ range
->npins
)
506 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock
);
509 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
510 * @pctldev: the pin controller device to look in
511 * @pin: a controller-local number to find the range for
513 struct pinctrl_gpio_range
*
514 pinctrl_find_gpio_range_from_pin(struct pinctrl_dev
*pctldev
,
517 struct pinctrl_gpio_range
*range
;
519 mutex_lock(&pctldev
->mutex
);
520 range
= pinctrl_find_gpio_range_from_pin_nolock(pctldev
, pin
);
521 mutex_unlock(&pctldev
->mutex
);
525 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin
);
528 * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller
529 * @pctldev: pin controller device to remove the range from
530 * @range: the GPIO range to remove
532 void pinctrl_remove_gpio_range(struct pinctrl_dev
*pctldev
,
533 struct pinctrl_gpio_range
*range
)
535 mutex_lock(&pctldev
->mutex
);
536 list_del(&range
->node
);
537 mutex_unlock(&pctldev
->mutex
);
539 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range
);
541 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
544 * pinctrl_generic_get_group_count() - returns the number of pin groups
545 * @pctldev: pin controller device
547 int pinctrl_generic_get_group_count(struct pinctrl_dev
*pctldev
)
549 return pctldev
->num_groups
;
551 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count
);
554 * pinctrl_generic_get_group_name() - returns the name of a pin group
555 * @pctldev: pin controller device
556 * @selector: group number
558 const char *pinctrl_generic_get_group_name(struct pinctrl_dev
*pctldev
,
559 unsigned int selector
)
561 struct group_desc
*group
;
563 group
= radix_tree_lookup(&pctldev
->pin_group_tree
,
570 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name
);
573 * pinctrl_generic_get_group_pins() - gets the pin group pins
574 * @pctldev: pin controller device
575 * @selector: group number
576 * @pins: pins in the group
577 * @num_pins: number of pins in the group
579 int pinctrl_generic_get_group_pins(struct pinctrl_dev
*pctldev
,
580 unsigned int selector
,
581 const unsigned int **pins
,
582 unsigned int *num_pins
)
584 struct group_desc
*group
;
586 group
= radix_tree_lookup(&pctldev
->pin_group_tree
,
589 dev_err(pctldev
->dev
, "%s could not find pingroup%i\n",
595 *num_pins
= group
->num_pins
;
599 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins
);
602 * pinctrl_generic_get_group() - returns a pin group based on the number
603 * @pctldev: pin controller device
604 * @gselector: group number
606 struct group_desc
*pinctrl_generic_get_group(struct pinctrl_dev
*pctldev
,
607 unsigned int selector
)
609 struct group_desc
*group
;
611 group
= radix_tree_lookup(&pctldev
->pin_group_tree
,
618 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group
);
621 * pinctrl_generic_add_group() - adds a new pin group
622 * @pctldev: pin controller device
623 * @name: name of the pin group
624 * @pins: pins in the pin group
625 * @num_pins: number of pins in the pin group
626 * @data: pin controller driver specific data
628 * Note that the caller must take care of locking.
630 int pinctrl_generic_add_group(struct pinctrl_dev
*pctldev
, const char *name
,
631 int *pins
, int num_pins
, void *data
)
633 struct group_desc
*group
;
635 group
= devm_kzalloc(pctldev
->dev
, sizeof(*group
), GFP_KERNEL
);
641 group
->num_pins
= num_pins
;
644 radix_tree_insert(&pctldev
->pin_group_tree
, pctldev
->num_groups
,
647 pctldev
->num_groups
++;
651 EXPORT_SYMBOL_GPL(pinctrl_generic_add_group
);
654 * pinctrl_generic_remove_group() - removes a numbered pin group
655 * @pctldev: pin controller device
656 * @selector: group number
658 * Note that the caller must take care of locking.
660 int pinctrl_generic_remove_group(struct pinctrl_dev
*pctldev
,
661 unsigned int selector
)
663 struct group_desc
*group
;
665 group
= radix_tree_lookup(&pctldev
->pin_group_tree
,
670 radix_tree_delete(&pctldev
->pin_group_tree
, selector
);
671 devm_kfree(pctldev
->dev
, group
);
673 pctldev
->num_groups
--;
677 EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group
);
680 * pinctrl_generic_free_groups() - removes all pin groups
681 * @pctldev: pin controller device
683 * Note that the caller must take care of locking.
685 static void pinctrl_generic_free_groups(struct pinctrl_dev
*pctldev
)
687 struct radix_tree_iter iter
;
688 struct group_desc
*group
;
689 unsigned long *indices
;
693 indices
= devm_kzalloc(pctldev
->dev
, sizeof(*indices
) *
694 pctldev
->num_groups
, GFP_KERNEL
);
698 radix_tree_for_each_slot(slot
, &pctldev
->pin_group_tree
, &iter
, 0)
699 indices
[i
++] = iter
.index
;
701 for (i
= 0; i
< pctldev
->num_groups
; i
++) {
702 group
= radix_tree_lookup(&pctldev
->pin_group_tree
,
704 radix_tree_delete(&pctldev
->pin_group_tree
, indices
[i
]);
705 devm_kfree(pctldev
->dev
, group
);
708 pctldev
->num_groups
= 0;
712 static inline void pinctrl_generic_free_groups(struct pinctrl_dev
*pctldev
)
715 #endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
718 * pinctrl_get_group_selector() - returns the group selector for a group
719 * @pctldev: the pin controller handling the group
720 * @pin_group: the pin group to look up
722 int pinctrl_get_group_selector(struct pinctrl_dev
*pctldev
,
723 const char *pin_group
)
725 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
726 unsigned ngroups
= pctlops
->get_groups_count(pctldev
);
727 unsigned group_selector
= 0;
729 while (group_selector
< ngroups
) {
730 const char *gname
= pctlops
->get_group_name(pctldev
,
732 if (!strcmp(gname
, pin_group
)) {
733 dev_dbg(pctldev
->dev
,
734 "found group selector %u for %s\n",
737 return group_selector
;
743 dev_err(pctldev
->dev
, "does not have pin group %s\n",
750 * pinctrl_request_gpio() - request a single pin to be used as GPIO
751 * @gpio: the GPIO pin number from the GPIO subsystem number space
753 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
754 * as part of their gpio_request() semantics, platforms and individual drivers
755 * shall *NOT* request GPIO pins to be muxed in.
757 int pinctrl_request_gpio(unsigned gpio
)
759 struct pinctrl_dev
*pctldev
;
760 struct pinctrl_gpio_range
*range
;
764 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
766 if (pinctrl_ready_for_gpio_range(gpio
))
771 mutex_lock(&pctldev
->mutex
);
773 /* Convert to the pin controllers number space */
774 pin
= gpio_to_pin(range
, gpio
);
776 ret
= pinmux_request_gpio(pctldev
, range
, pin
, gpio
);
778 mutex_unlock(&pctldev
->mutex
);
782 EXPORT_SYMBOL_GPL(pinctrl_request_gpio
);
785 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
786 * @gpio: the GPIO pin number from the GPIO subsystem number space
788 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
789 * as part of their gpio_free() semantics, platforms and individual drivers
790 * shall *NOT* request GPIO pins to be muxed out.
792 void pinctrl_free_gpio(unsigned gpio
)
794 struct pinctrl_dev
*pctldev
;
795 struct pinctrl_gpio_range
*range
;
799 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
803 mutex_lock(&pctldev
->mutex
);
805 /* Convert to the pin controllers number space */
806 pin
= gpio_to_pin(range
, gpio
);
808 pinmux_free_gpio(pctldev
, pin
, range
);
810 mutex_unlock(&pctldev
->mutex
);
812 EXPORT_SYMBOL_GPL(pinctrl_free_gpio
);
814 static int pinctrl_gpio_direction(unsigned gpio
, bool input
)
816 struct pinctrl_dev
*pctldev
;
817 struct pinctrl_gpio_range
*range
;
821 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
826 mutex_lock(&pctldev
->mutex
);
828 /* Convert to the pin controllers number space */
829 pin
= gpio_to_pin(range
, gpio
);
830 ret
= pinmux_gpio_direction(pctldev
, range
, pin
, input
);
832 mutex_unlock(&pctldev
->mutex
);
838 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
839 * @gpio: the GPIO pin number from the GPIO subsystem number space
841 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
842 * as part of their gpio_direction_input() semantics, platforms and individual
843 * drivers shall *NOT* touch pin control GPIO calls.
845 int pinctrl_gpio_direction_input(unsigned gpio
)
847 return pinctrl_gpio_direction(gpio
, true);
849 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input
);
852 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
853 * @gpio: the GPIO pin number from the GPIO subsystem number space
855 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
856 * as part of their gpio_direction_output() semantics, platforms and individual
857 * drivers shall *NOT* touch pin control GPIO calls.
859 int pinctrl_gpio_direction_output(unsigned gpio
)
861 return pinctrl_gpio_direction(gpio
, false);
863 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output
);
866 * pinctrl_gpio_set_config() - Apply config to given GPIO pin
867 * @gpio: the GPIO pin number from the GPIO subsystem number space
868 * @config: the configuration to apply to the GPIO
870 * This function should *ONLY* be used from gpiolib-based GPIO drivers, if
871 * they need to call the underlying pin controller to change GPIO config
872 * (for example set debounce time).
874 int pinctrl_gpio_set_config(unsigned gpio
, unsigned long config
)
876 unsigned long configs
[] = { config
};
877 struct pinctrl_gpio_range
*range
;
878 struct pinctrl_dev
*pctldev
;
881 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
885 mutex_lock(&pctldev
->mutex
);
886 pin
= gpio_to_pin(range
, gpio
);
887 ret
= pinconf_set_config(pctldev
, pin
, configs
, ARRAY_SIZE(configs
));
888 mutex_unlock(&pctldev
->mutex
);
892 EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config
);
894 static struct pinctrl_state
*find_state(struct pinctrl
*p
,
897 struct pinctrl_state
*state
;
899 list_for_each_entry(state
, &p
->states
, node
)
900 if (!strcmp(state
->name
, name
))
906 static struct pinctrl_state
*create_state(struct pinctrl
*p
,
909 struct pinctrl_state
*state
;
911 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
913 return ERR_PTR(-ENOMEM
);
916 INIT_LIST_HEAD(&state
->settings
);
918 list_add_tail(&state
->node
, &p
->states
);
923 static int add_setting(struct pinctrl
*p
, struct pinctrl_dev
*pctldev
,
924 struct pinctrl_map
const *map
)
926 struct pinctrl_state
*state
;
927 struct pinctrl_setting
*setting
;
930 state
= find_state(p
, map
->name
);
932 state
= create_state(p
, map
->name
);
934 return PTR_ERR(state
);
936 if (map
->type
== PIN_MAP_TYPE_DUMMY_STATE
)
939 setting
= kzalloc(sizeof(*setting
), GFP_KERNEL
);
943 setting
->type
= map
->type
;
946 setting
->pctldev
= pctldev
;
949 get_pinctrl_dev_from_devname(map
->ctrl_dev_name
);
950 if (setting
->pctldev
== NULL
) {
952 /* Do not defer probing of hogs (circular loop) */
953 if (!strcmp(map
->ctrl_dev_name
, map
->dev_name
))
956 * OK let us guess that the driver is not there yet, and
957 * let's defer obtaining this pinctrl handle to later...
959 dev_info(p
->dev
, "unknown pinctrl device %s in map entry, deferring probe",
961 return -EPROBE_DEFER
;
964 setting
->dev_name
= map
->dev_name
;
967 case PIN_MAP_TYPE_MUX_GROUP
:
968 ret
= pinmux_map_to_setting(map
, setting
);
970 case PIN_MAP_TYPE_CONFIGS_PIN
:
971 case PIN_MAP_TYPE_CONFIGS_GROUP
:
972 ret
= pinconf_map_to_setting(map
, setting
);
983 list_add_tail(&setting
->node
, &state
->settings
);
988 static struct pinctrl
*find_pinctrl(struct device
*dev
)
992 mutex_lock(&pinctrl_list_mutex
);
993 list_for_each_entry(p
, &pinctrl_list
, node
)
995 mutex_unlock(&pinctrl_list_mutex
);
999 mutex_unlock(&pinctrl_list_mutex
);
1003 static void pinctrl_free(struct pinctrl
*p
, bool inlist
);
1005 static struct pinctrl
*create_pinctrl(struct device
*dev
,
1006 struct pinctrl_dev
*pctldev
)
1009 const char *devname
;
1010 struct pinctrl_maps
*maps_node
;
1012 struct pinctrl_map
const *map
;
1016 * create the state cookie holder struct pinctrl for each
1017 * mapping, this is what consumers will get when requesting
1018 * a pin control handle with pinctrl_get()
1020 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
1022 return ERR_PTR(-ENOMEM
);
1024 INIT_LIST_HEAD(&p
->states
);
1025 INIT_LIST_HEAD(&p
->dt_maps
);
1027 ret
= pinctrl_dt_to_map(p
, pctldev
);
1030 return ERR_PTR(ret
);
1033 devname
= dev_name(dev
);
1035 mutex_lock(&pinctrl_maps_mutex
);
1036 /* Iterate over the pin control maps to locate the right ones */
1037 for_each_maps(maps_node
, i
, map
) {
1038 /* Map must be for this device */
1039 if (strcmp(map
->dev_name
, devname
))
1042 ret
= add_setting(p
, pctldev
, map
);
1044 * At this point the adding of a setting may:
1046 * - Defer, if the pinctrl device is not yet available
1047 * - Fail, if the pinctrl device is not yet available,
1048 * AND the setting is a hog. We cannot defer that, since
1049 * the hog will kick in immediately after the device
1052 * If the error returned was not -EPROBE_DEFER then we
1053 * accumulate the errors to see if we end up with
1054 * an -EPROBE_DEFER later, as that is the worst case.
1056 if (ret
== -EPROBE_DEFER
) {
1057 pinctrl_free(p
, false);
1058 mutex_unlock(&pinctrl_maps_mutex
);
1059 return ERR_PTR(ret
);
1062 mutex_unlock(&pinctrl_maps_mutex
);
1065 /* If some other error than deferral occurred, return here */
1066 pinctrl_free(p
, false);
1067 return ERR_PTR(ret
);
1070 kref_init(&p
->users
);
1072 /* Add the pinctrl handle to the global list */
1073 mutex_lock(&pinctrl_list_mutex
);
1074 list_add_tail(&p
->node
, &pinctrl_list
);
1075 mutex_unlock(&pinctrl_list_mutex
);
1081 * pinctrl_get() - retrieves the pinctrl handle for a device
1082 * @dev: the device to obtain the handle for
1084 struct pinctrl
*pinctrl_get(struct device
*dev
)
1089 return ERR_PTR(-EINVAL
);
1092 * See if somebody else (such as the device core) has already
1093 * obtained a handle to the pinctrl for this device. In that case,
1094 * return another pointer to it.
1096 p
= find_pinctrl(dev
);
1098 dev_dbg(dev
, "obtain a copy of previously claimed pinctrl\n");
1099 kref_get(&p
->users
);
1103 return create_pinctrl(dev
, NULL
);
1105 EXPORT_SYMBOL_GPL(pinctrl_get
);
1107 static void pinctrl_free_setting(bool disable_setting
,
1108 struct pinctrl_setting
*setting
)
1110 switch (setting
->type
) {
1111 case PIN_MAP_TYPE_MUX_GROUP
:
1112 if (disable_setting
)
1113 pinmux_disable_setting(setting
);
1114 pinmux_free_setting(setting
);
1116 case PIN_MAP_TYPE_CONFIGS_PIN
:
1117 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1118 pinconf_free_setting(setting
);
1125 static void pinctrl_free(struct pinctrl
*p
, bool inlist
)
1127 struct pinctrl_state
*state
, *n1
;
1128 struct pinctrl_setting
*setting
, *n2
;
1130 mutex_lock(&pinctrl_list_mutex
);
1131 list_for_each_entry_safe(state
, n1
, &p
->states
, node
) {
1132 list_for_each_entry_safe(setting
, n2
, &state
->settings
, node
) {
1133 pinctrl_free_setting(state
== p
->state
, setting
);
1134 list_del(&setting
->node
);
1137 list_del(&state
->node
);
1141 pinctrl_dt_free_maps(p
);
1146 mutex_unlock(&pinctrl_list_mutex
);
1150 * pinctrl_release() - release the pinctrl handle
1151 * @kref: the kref in the pinctrl being released
1153 static void pinctrl_release(struct kref
*kref
)
1155 struct pinctrl
*p
= container_of(kref
, struct pinctrl
, users
);
1157 pinctrl_free(p
, true);
1161 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
1162 * @p: the pinctrl handle to release
1164 void pinctrl_put(struct pinctrl
*p
)
1166 kref_put(&p
->users
, pinctrl_release
);
1168 EXPORT_SYMBOL_GPL(pinctrl_put
);
1171 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
1172 * @p: the pinctrl handle to retrieve the state from
1173 * @name: the state name to retrieve
1175 struct pinctrl_state
*pinctrl_lookup_state(struct pinctrl
*p
,
1178 struct pinctrl_state
*state
;
1180 state
= find_state(p
, name
);
1182 if (pinctrl_dummy_state
) {
1183 /* create dummy state */
1184 dev_dbg(p
->dev
, "using pinctrl dummy state (%s)\n",
1186 state
= create_state(p
, name
);
1188 state
= ERR_PTR(-ENODEV
);
1193 EXPORT_SYMBOL_GPL(pinctrl_lookup_state
);
1196 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
1197 * @p: the pinctrl handle for the device that requests configuration
1198 * @state: the state handle to select/activate/program
1200 int pinctrl_select_state(struct pinctrl
*p
, struct pinctrl_state
*state
)
1202 struct pinctrl_setting
*setting
, *setting2
;
1203 struct pinctrl_state
*old_state
= p
->state
;
1206 if (p
->state
== state
)
1211 * For each pinmux setting in the old state, forget SW's record
1212 * of mux owner for that pingroup. Any pingroups which are
1213 * still owned by the new state will be re-acquired by the call
1214 * to pinmux_enable_setting() in the loop below.
1216 list_for_each_entry(setting
, &p
->state
->settings
, node
) {
1217 if (setting
->type
!= PIN_MAP_TYPE_MUX_GROUP
)
1219 pinmux_disable_setting(setting
);
1225 /* Apply all the settings for the new state */
1226 list_for_each_entry(setting
, &state
->settings
, node
) {
1227 switch (setting
->type
) {
1228 case PIN_MAP_TYPE_MUX_GROUP
:
1229 ret
= pinmux_enable_setting(setting
);
1231 case PIN_MAP_TYPE_CONFIGS_PIN
:
1232 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1233 ret
= pinconf_apply_setting(setting
);
1241 goto unapply_new_state
;
1250 dev_err(p
->dev
, "Error applying setting, reverse things back\n");
1252 list_for_each_entry(setting2
, &state
->settings
, node
) {
1253 if (&setting2
->node
== &setting
->node
)
1256 * All we can do here is pinmux_disable_setting.
1257 * That means that some pins are muxed differently now
1258 * than they were before applying the setting (We can't
1259 * "unmux a pin"!), but it's not a big deal since the pins
1260 * are free to be muxed by another apply_setting.
1262 if (setting2
->type
== PIN_MAP_TYPE_MUX_GROUP
)
1263 pinmux_disable_setting(setting2
);
1266 /* There's no infinite recursive loop here because p->state is NULL */
1268 pinctrl_select_state(p
, old_state
);
1272 EXPORT_SYMBOL_GPL(pinctrl_select_state
);
1274 static void devm_pinctrl_release(struct device
*dev
, void *res
)
1276 pinctrl_put(*(struct pinctrl
**)res
);
1280 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1281 * @dev: the device to obtain the handle for
1283 * If there is a need to explicitly destroy the returned struct pinctrl,
1284 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1286 struct pinctrl
*devm_pinctrl_get(struct device
*dev
)
1288 struct pinctrl
**ptr
, *p
;
1290 ptr
= devres_alloc(devm_pinctrl_release
, sizeof(*ptr
), GFP_KERNEL
);
1292 return ERR_PTR(-ENOMEM
);
1294 p
= pinctrl_get(dev
);
1297 devres_add(dev
, ptr
);
1304 EXPORT_SYMBOL_GPL(devm_pinctrl_get
);
1306 static int devm_pinctrl_match(struct device
*dev
, void *res
, void *data
)
1308 struct pinctrl
**p
= res
;
1314 * devm_pinctrl_put() - Resource managed pinctrl_put()
1315 * @p: the pinctrl handle to release
1317 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1318 * this function will not need to be called and the resource management
1319 * code will ensure that the resource is freed.
1321 void devm_pinctrl_put(struct pinctrl
*p
)
1323 WARN_ON(devres_release(p
->dev
, devm_pinctrl_release
,
1324 devm_pinctrl_match
, p
));
1326 EXPORT_SYMBOL_GPL(devm_pinctrl_put
);
1328 int pinctrl_register_map(struct pinctrl_map
const *maps
, unsigned num_maps
,
1332 struct pinctrl_maps
*maps_node
;
1334 pr_debug("add %u pinctrl maps\n", num_maps
);
1336 /* First sanity check the new mapping */
1337 for (i
= 0; i
< num_maps
; i
++) {
1338 if (!maps
[i
].dev_name
) {
1339 pr_err("failed to register map %s (%d): no device given\n",
1344 if (!maps
[i
].name
) {
1345 pr_err("failed to register map %d: no map name given\n",
1350 if (maps
[i
].type
!= PIN_MAP_TYPE_DUMMY_STATE
&&
1351 !maps
[i
].ctrl_dev_name
) {
1352 pr_err("failed to register map %s (%d): no pin control device given\n",
1357 switch (maps
[i
].type
) {
1358 case PIN_MAP_TYPE_DUMMY_STATE
:
1360 case PIN_MAP_TYPE_MUX_GROUP
:
1361 ret
= pinmux_validate_map(&maps
[i
], i
);
1365 case PIN_MAP_TYPE_CONFIGS_PIN
:
1366 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1367 ret
= pinconf_validate_map(&maps
[i
], i
);
1372 pr_err("failed to register map %s (%d): invalid type given\n",
1378 maps_node
= kzalloc(sizeof(*maps_node
), GFP_KERNEL
);
1382 maps_node
->num_maps
= num_maps
;
1384 maps_node
->maps
= kmemdup(maps
, sizeof(*maps
) * num_maps
,
1386 if (!maps_node
->maps
) {
1387 pr_err("failed to duplicate mapping table\n");
1392 maps_node
->maps
= maps
;
1395 mutex_lock(&pinctrl_maps_mutex
);
1396 list_add_tail(&maps_node
->node
, &pinctrl_maps
);
1397 mutex_unlock(&pinctrl_maps_mutex
);
1403 * pinctrl_register_mappings() - register a set of pin controller mappings
1404 * @maps: the pincontrol mappings table to register. This should probably be
1405 * marked with __initdata so it can be discarded after boot. This
1406 * function will perform a shallow copy for the mapping entries.
1407 * @num_maps: the number of maps in the mapping table
1409 int pinctrl_register_mappings(struct pinctrl_map
const *maps
,
1412 return pinctrl_register_map(maps
, num_maps
, true);
1415 void pinctrl_unregister_map(struct pinctrl_map
const *map
)
1417 struct pinctrl_maps
*maps_node
;
1419 mutex_lock(&pinctrl_maps_mutex
);
1420 list_for_each_entry(maps_node
, &pinctrl_maps
, node
) {
1421 if (maps_node
->maps
== map
) {
1422 list_del(&maps_node
->node
);
1424 mutex_unlock(&pinctrl_maps_mutex
);
1428 mutex_unlock(&pinctrl_maps_mutex
);
1432 * pinctrl_force_sleep() - turn a given controller device into sleep state
1433 * @pctldev: pin controller device
1435 int pinctrl_force_sleep(struct pinctrl_dev
*pctldev
)
1437 if (!IS_ERR(pctldev
->p
) && !IS_ERR(pctldev
->hog_sleep
))
1438 return pinctrl_select_state(pctldev
->p
, pctldev
->hog_sleep
);
1441 EXPORT_SYMBOL_GPL(pinctrl_force_sleep
);
1444 * pinctrl_force_default() - turn a given controller device into default state
1445 * @pctldev: pin controller device
1447 int pinctrl_force_default(struct pinctrl_dev
*pctldev
)
1449 if (!IS_ERR(pctldev
->p
) && !IS_ERR(pctldev
->hog_default
))
1450 return pinctrl_select_state(pctldev
->p
, pctldev
->hog_default
);
1453 EXPORT_SYMBOL_GPL(pinctrl_force_default
);
1456 * pinctrl_init_done() - tell pinctrl probe is done
1458 * We'll use this time to switch the pins from "init" to "default" unless the
1459 * driver selected some other state.
1461 * @dev: device to that's done probing
1463 int pinctrl_init_done(struct device
*dev
)
1465 struct dev_pin_info
*pins
= dev
->pins
;
1471 if (IS_ERR(pins
->init_state
))
1472 return 0; /* No such state */
1474 if (pins
->p
->state
!= pins
->init_state
)
1475 return 0; /* Not at init anyway */
1477 if (IS_ERR(pins
->default_state
))
1478 return 0; /* No default state */
1480 ret
= pinctrl_select_state(pins
->p
, pins
->default_state
);
1482 dev_err(dev
, "failed to activate default pinctrl state\n");
1490 * pinctrl_pm_select_state() - select pinctrl state for PM
1491 * @dev: device to select default state for
1492 * @state: state to set
1494 static int pinctrl_pm_select_state(struct device
*dev
,
1495 struct pinctrl_state
*state
)
1497 struct dev_pin_info
*pins
= dev
->pins
;
1501 return 0; /* No such state */
1502 ret
= pinctrl_select_state(pins
->p
, state
);
1504 dev_err(dev
, "failed to activate pinctrl state %s\n",
1510 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1511 * @dev: device to select default state for
1513 int pinctrl_pm_select_default_state(struct device
*dev
)
1518 return pinctrl_pm_select_state(dev
, dev
->pins
->default_state
);
1520 EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state
);
1523 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1524 * @dev: device to select sleep state for
1526 int pinctrl_pm_select_sleep_state(struct device
*dev
)
1531 return pinctrl_pm_select_state(dev
, dev
->pins
->sleep_state
);
1533 EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state
);
1536 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1537 * @dev: device to select idle state for
1539 int pinctrl_pm_select_idle_state(struct device
*dev
)
1544 return pinctrl_pm_select_state(dev
, dev
->pins
->idle_state
);
1546 EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state
);
1549 #ifdef CONFIG_DEBUG_FS
1551 static int pinctrl_pins_show(struct seq_file
*s
, void *what
)
1553 struct pinctrl_dev
*pctldev
= s
->private;
1554 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1557 seq_printf(s
, "registered pins: %d\n", pctldev
->desc
->npins
);
1559 mutex_lock(&pctldev
->mutex
);
1561 /* The pin number can be retrived from the pin controller descriptor */
1562 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
1563 struct pin_desc
*desc
;
1565 pin
= pctldev
->desc
->pins
[i
].number
;
1566 desc
= pin_desc_get(pctldev
, pin
);
1567 /* Pin space may be sparse */
1571 seq_printf(s
, "pin %d (%s) ", pin
, desc
->name
);
1573 /* Driver-specific info per pin */
1574 if (ops
->pin_dbg_show
)
1575 ops
->pin_dbg_show(pctldev
, s
, pin
);
1580 mutex_unlock(&pctldev
->mutex
);
1585 static int pinctrl_groups_show(struct seq_file
*s
, void *what
)
1587 struct pinctrl_dev
*pctldev
= s
->private;
1588 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1589 unsigned ngroups
, selector
= 0;
1591 mutex_lock(&pctldev
->mutex
);
1593 ngroups
= ops
->get_groups_count(pctldev
);
1595 seq_puts(s
, "registered pin groups:\n");
1596 while (selector
< ngroups
) {
1597 const unsigned *pins
= NULL
;
1598 unsigned num_pins
= 0;
1599 const char *gname
= ops
->get_group_name(pctldev
, selector
);
1604 if (ops
->get_group_pins
)
1605 ret
= ops
->get_group_pins(pctldev
, selector
,
1608 seq_printf(s
, "%s [ERROR GETTING PINS]\n",
1611 seq_printf(s
, "group: %s\n", gname
);
1612 for (i
= 0; i
< num_pins
; i
++) {
1613 pname
= pin_get_name(pctldev
, pins
[i
]);
1614 if (WARN_ON(!pname
)) {
1615 mutex_unlock(&pctldev
->mutex
);
1618 seq_printf(s
, "pin %d (%s)\n", pins
[i
], pname
);
1625 mutex_unlock(&pctldev
->mutex
);
1630 static int pinctrl_gpioranges_show(struct seq_file
*s
, void *what
)
1632 struct pinctrl_dev
*pctldev
= s
->private;
1633 struct pinctrl_gpio_range
*range
= NULL
;
1635 seq_puts(s
, "GPIO ranges handled:\n");
1637 mutex_lock(&pctldev
->mutex
);
1639 /* Loop over the ranges */
1640 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
1643 seq_printf(s
, "%u: %s GPIOS [%u - %u] PINS {",
1644 range
->id
, range
->name
,
1645 range
->base
, (range
->base
+ range
->npins
- 1));
1646 for (a
= 0; a
< range
->npins
- 1; a
++)
1647 seq_printf(s
, "%u, ", range
->pins
[a
]);
1648 seq_printf(s
, "%u}\n", range
->pins
[a
]);
1651 seq_printf(s
, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1652 range
->id
, range
->name
,
1653 range
->base
, (range
->base
+ range
->npins
- 1),
1655 (range
->pin_base
+ range
->npins
- 1));
1658 mutex_unlock(&pctldev
->mutex
);
1663 static int pinctrl_devices_show(struct seq_file
*s
, void *what
)
1665 struct pinctrl_dev
*pctldev
;
1667 seq_puts(s
, "name [pinmux] [pinconf]\n");
1669 mutex_lock(&pinctrldev_list_mutex
);
1671 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
1672 seq_printf(s
, "%s ", pctldev
->desc
->name
);
1673 if (pctldev
->desc
->pmxops
)
1674 seq_puts(s
, "yes ");
1677 if (pctldev
->desc
->confops
)
1684 mutex_unlock(&pinctrldev_list_mutex
);
1689 static inline const char *map_type(enum pinctrl_map_type type
)
1691 static const char * const names
[] = {
1699 if (type
>= ARRAY_SIZE(names
))
1705 static int pinctrl_maps_show(struct seq_file
*s
, void *what
)
1707 struct pinctrl_maps
*maps_node
;
1709 struct pinctrl_map
const *map
;
1711 seq_puts(s
, "Pinctrl maps:\n");
1713 mutex_lock(&pinctrl_maps_mutex
);
1714 for_each_maps(maps_node
, i
, map
) {
1715 seq_printf(s
, "device %s\nstate %s\ntype %s (%d)\n",
1716 map
->dev_name
, map
->name
, map_type(map
->type
),
1719 if (map
->type
!= PIN_MAP_TYPE_DUMMY_STATE
)
1720 seq_printf(s
, "controlling device %s\n",
1721 map
->ctrl_dev_name
);
1723 switch (map
->type
) {
1724 case PIN_MAP_TYPE_MUX_GROUP
:
1725 pinmux_show_map(s
, map
);
1727 case PIN_MAP_TYPE_CONFIGS_PIN
:
1728 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1729 pinconf_show_map(s
, map
);
1735 seq_printf(s
, "\n");
1737 mutex_unlock(&pinctrl_maps_mutex
);
1742 static int pinctrl_show(struct seq_file
*s
, void *what
)
1745 struct pinctrl_state
*state
;
1746 struct pinctrl_setting
*setting
;
1748 seq_puts(s
, "Requested pin control handlers their pinmux maps:\n");
1750 mutex_lock(&pinctrl_list_mutex
);
1752 list_for_each_entry(p
, &pinctrl_list
, node
) {
1753 seq_printf(s
, "device: %s current state: %s\n",
1755 p
->state
? p
->state
->name
: "none");
1757 list_for_each_entry(state
, &p
->states
, node
) {
1758 seq_printf(s
, " state: %s\n", state
->name
);
1760 list_for_each_entry(setting
, &state
->settings
, node
) {
1761 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
1763 seq_printf(s
, " type: %s controller %s ",
1764 map_type(setting
->type
),
1765 pinctrl_dev_get_name(pctldev
));
1767 switch (setting
->type
) {
1768 case PIN_MAP_TYPE_MUX_GROUP
:
1769 pinmux_show_setting(s
, setting
);
1771 case PIN_MAP_TYPE_CONFIGS_PIN
:
1772 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1773 pinconf_show_setting(s
, setting
);
1782 mutex_unlock(&pinctrl_list_mutex
);
1787 static int pinctrl_pins_open(struct inode
*inode
, struct file
*file
)
1789 return single_open(file
, pinctrl_pins_show
, inode
->i_private
);
1792 static int pinctrl_groups_open(struct inode
*inode
, struct file
*file
)
1794 return single_open(file
, pinctrl_groups_show
, inode
->i_private
);
1797 static int pinctrl_gpioranges_open(struct inode
*inode
, struct file
*file
)
1799 return single_open(file
, pinctrl_gpioranges_show
, inode
->i_private
);
1802 static int pinctrl_devices_open(struct inode
*inode
, struct file
*file
)
1804 return single_open(file
, pinctrl_devices_show
, NULL
);
1807 static int pinctrl_maps_open(struct inode
*inode
, struct file
*file
)
1809 return single_open(file
, pinctrl_maps_show
, NULL
);
1812 static int pinctrl_open(struct inode
*inode
, struct file
*file
)
1814 return single_open(file
, pinctrl_show
, NULL
);
1817 static const struct file_operations pinctrl_pins_ops
= {
1818 .open
= pinctrl_pins_open
,
1820 .llseek
= seq_lseek
,
1821 .release
= single_release
,
1824 static const struct file_operations pinctrl_groups_ops
= {
1825 .open
= pinctrl_groups_open
,
1827 .llseek
= seq_lseek
,
1828 .release
= single_release
,
1831 static const struct file_operations pinctrl_gpioranges_ops
= {
1832 .open
= pinctrl_gpioranges_open
,
1834 .llseek
= seq_lseek
,
1835 .release
= single_release
,
1838 static const struct file_operations pinctrl_devices_ops
= {
1839 .open
= pinctrl_devices_open
,
1841 .llseek
= seq_lseek
,
1842 .release
= single_release
,
1845 static const struct file_operations pinctrl_maps_ops
= {
1846 .open
= pinctrl_maps_open
,
1848 .llseek
= seq_lseek
,
1849 .release
= single_release
,
1852 static const struct file_operations pinctrl_ops
= {
1853 .open
= pinctrl_open
,
1855 .llseek
= seq_lseek
,
1856 .release
= single_release
,
1859 static struct dentry
*debugfs_root
;
1861 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1863 struct dentry
*device_root
;
1865 device_root
= debugfs_create_dir(dev_name(pctldev
->dev
),
1867 pctldev
->device_root
= device_root
;
1869 if (IS_ERR(device_root
) || !device_root
) {
1870 pr_warn("failed to create debugfs directory for %s\n",
1871 dev_name(pctldev
->dev
));
1874 debugfs_create_file("pins", S_IFREG
| S_IRUGO
,
1875 device_root
, pctldev
, &pinctrl_pins_ops
);
1876 debugfs_create_file("pingroups", S_IFREG
| S_IRUGO
,
1877 device_root
, pctldev
, &pinctrl_groups_ops
);
1878 debugfs_create_file("gpio-ranges", S_IFREG
| S_IRUGO
,
1879 device_root
, pctldev
, &pinctrl_gpioranges_ops
);
1880 if (pctldev
->desc
->pmxops
)
1881 pinmux_init_device_debugfs(device_root
, pctldev
);
1882 if (pctldev
->desc
->confops
)
1883 pinconf_init_device_debugfs(device_root
, pctldev
);
1886 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1888 debugfs_remove_recursive(pctldev
->device_root
);
1891 static void pinctrl_init_debugfs(void)
1893 debugfs_root
= debugfs_create_dir("pinctrl", NULL
);
1894 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
1895 pr_warn("failed to create debugfs directory\n");
1896 debugfs_root
= NULL
;
1900 debugfs_create_file("pinctrl-devices", S_IFREG
| S_IRUGO
,
1901 debugfs_root
, NULL
, &pinctrl_devices_ops
);
1902 debugfs_create_file("pinctrl-maps", S_IFREG
| S_IRUGO
,
1903 debugfs_root
, NULL
, &pinctrl_maps_ops
);
1904 debugfs_create_file("pinctrl-handles", S_IFREG
| S_IRUGO
,
1905 debugfs_root
, NULL
, &pinctrl_ops
);
1908 #else /* CONFIG_DEBUG_FS */
1910 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1914 static void pinctrl_init_debugfs(void)
1918 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1924 static int pinctrl_check_ops(struct pinctrl_dev
*pctldev
)
1926 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1929 !ops
->get_groups_count
||
1930 !ops
->get_group_name
)
1937 * pinctrl_init_controller() - init a pin controller device
1938 * @pctldesc: descriptor for this pin controller
1939 * @dev: parent device for this pin controller
1940 * @driver_data: private pin controller data for this pin controller
1942 static struct pinctrl_dev
*
1943 pinctrl_init_controller(struct pinctrl_desc
*pctldesc
, struct device
*dev
,
1946 struct pinctrl_dev
*pctldev
;
1950 return ERR_PTR(-EINVAL
);
1951 if (!pctldesc
->name
)
1952 return ERR_PTR(-EINVAL
);
1954 pctldev
= kzalloc(sizeof(*pctldev
), GFP_KERNEL
);
1956 return ERR_PTR(-ENOMEM
);
1958 /* Initialize pin control device struct */
1959 pctldev
->owner
= pctldesc
->owner
;
1960 pctldev
->desc
= pctldesc
;
1961 pctldev
->driver_data
= driver_data
;
1962 INIT_RADIX_TREE(&pctldev
->pin_desc_tree
, GFP_KERNEL
);
1963 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
1964 INIT_RADIX_TREE(&pctldev
->pin_group_tree
, GFP_KERNEL
);
1966 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
1967 INIT_RADIX_TREE(&pctldev
->pin_function_tree
, GFP_KERNEL
);
1969 INIT_LIST_HEAD(&pctldev
->gpio_ranges
);
1970 INIT_LIST_HEAD(&pctldev
->node
);
1972 mutex_init(&pctldev
->mutex
);
1974 /* check core ops for sanity */
1975 ret
= pinctrl_check_ops(pctldev
);
1977 dev_err(dev
, "pinctrl ops lacks necessary functions\n");
1981 /* If we're implementing pinmuxing, check the ops for sanity */
1982 if (pctldesc
->pmxops
) {
1983 ret
= pinmux_check_ops(pctldev
);
1988 /* If we're implementing pinconfig, check the ops for sanity */
1989 if (pctldesc
->confops
) {
1990 ret
= pinconf_check_ops(pctldev
);
1995 /* Register all the pins */
1996 dev_dbg(dev
, "try to register %d pins ...\n", pctldesc
->npins
);
1997 ret
= pinctrl_register_pins(pctldev
, pctldesc
->pins
, pctldesc
->npins
);
1999 dev_err(dev
, "error during pin registration\n");
2000 pinctrl_free_pindescs(pctldev
, pctldesc
->pins
,
2008 mutex_destroy(&pctldev
->mutex
);
2010 return ERR_PTR(ret
);
2013 static int pinctrl_claim_hogs(struct pinctrl_dev
*pctldev
)
2015 pctldev
->p
= create_pinctrl(pctldev
->dev
, pctldev
);
2016 if (PTR_ERR(pctldev
->p
) == -ENODEV
) {
2017 dev_dbg(pctldev
->dev
, "no hogs found\n");
2022 if (IS_ERR(pctldev
->p
)) {
2023 dev_err(pctldev
->dev
, "error claiming hogs: %li\n",
2024 PTR_ERR(pctldev
->p
));
2026 return PTR_ERR(pctldev
->p
);
2029 kref_get(&pctldev
->p
->users
);
2030 pctldev
->hog_default
=
2031 pinctrl_lookup_state(pctldev
->p
, PINCTRL_STATE_DEFAULT
);
2032 if (IS_ERR(pctldev
->hog_default
)) {
2033 dev_dbg(pctldev
->dev
,
2034 "failed to lookup the default state\n");
2036 if (pinctrl_select_state(pctldev
->p
,
2037 pctldev
->hog_default
))
2038 dev_err(pctldev
->dev
,
2039 "failed to select default state\n");
2042 pctldev
->hog_sleep
=
2043 pinctrl_lookup_state(pctldev
->p
,
2044 PINCTRL_STATE_SLEEP
);
2045 if (IS_ERR(pctldev
->hog_sleep
))
2046 dev_dbg(pctldev
->dev
,
2047 "failed to lookup the sleep state\n");
2052 int pinctrl_enable(struct pinctrl_dev
*pctldev
)
2056 error
= pinctrl_claim_hogs(pctldev
);
2058 dev_err(pctldev
->dev
, "could not claim hogs: %i\n",
2060 mutex_destroy(&pctldev
->mutex
);
2066 mutex_lock(&pinctrldev_list_mutex
);
2067 list_add_tail(&pctldev
->node
, &pinctrldev_list
);
2068 mutex_unlock(&pinctrldev_list_mutex
);
2070 pinctrl_init_device_debugfs(pctldev
);
2074 EXPORT_SYMBOL_GPL(pinctrl_enable
);
2077 * pinctrl_register() - register a pin controller device
2078 * @pctldesc: descriptor for this pin controller
2079 * @dev: parent device for this pin controller
2080 * @driver_data: private pin controller data for this pin controller
2082 * Note that pinctrl_register() is known to have problems as the pin
2083 * controller driver functions are called before the driver has a
2084 * struct pinctrl_dev handle. To avoid issues later on, please use the
2085 * new pinctrl_register_and_init() below instead.
2087 struct pinctrl_dev
*pinctrl_register(struct pinctrl_desc
*pctldesc
,
2088 struct device
*dev
, void *driver_data
)
2090 struct pinctrl_dev
*pctldev
;
2093 pctldev
= pinctrl_init_controller(pctldesc
, dev
, driver_data
);
2094 if (IS_ERR(pctldev
))
2097 error
= pinctrl_enable(pctldev
);
2099 return ERR_PTR(error
);
2104 EXPORT_SYMBOL_GPL(pinctrl_register
);
2107 * pinctrl_register_and_init() - register and init pin controller device
2108 * @pctldesc: descriptor for this pin controller
2109 * @dev: parent device for this pin controller
2110 * @driver_data: private pin controller data for this pin controller
2111 * @pctldev: pin controller device
2113 * Note that pinctrl_enable() still needs to be manually called after
2114 * this once the driver is ready.
2116 int pinctrl_register_and_init(struct pinctrl_desc
*pctldesc
,
2117 struct device
*dev
, void *driver_data
,
2118 struct pinctrl_dev
**pctldev
)
2120 struct pinctrl_dev
*p
;
2122 p
= pinctrl_init_controller(pctldesc
, dev
, driver_data
);
2127 * We have pinctrl_start() call functions in the pin controller
2128 * driver with create_pinctrl() for at least dt_node_to_map(). So
2129 * let's make sure pctldev is properly initialized for the
2130 * pin controller driver before we do anything.
2136 EXPORT_SYMBOL_GPL(pinctrl_register_and_init
);
2139 * pinctrl_unregister() - unregister pinmux
2140 * @pctldev: pin controller to unregister
2142 * Called by pinmux drivers to unregister a pinmux.
2144 void pinctrl_unregister(struct pinctrl_dev
*pctldev
)
2146 struct pinctrl_gpio_range
*range
, *n
;
2148 if (pctldev
== NULL
)
2151 mutex_lock(&pctldev
->mutex
);
2152 pinctrl_remove_device_debugfs(pctldev
);
2153 mutex_unlock(&pctldev
->mutex
);
2155 if (!IS_ERR_OR_NULL(pctldev
->p
))
2156 pinctrl_put(pctldev
->p
);
2158 mutex_lock(&pinctrldev_list_mutex
);
2159 mutex_lock(&pctldev
->mutex
);
2160 /* TODO: check that no pinmuxes are still active? */
2161 list_del(&pctldev
->node
);
2162 pinmux_generic_free_functions(pctldev
);
2163 pinctrl_generic_free_groups(pctldev
);
2164 /* Destroy descriptor tree */
2165 pinctrl_free_pindescs(pctldev
, pctldev
->desc
->pins
,
2166 pctldev
->desc
->npins
);
2167 /* remove gpio ranges map */
2168 list_for_each_entry_safe(range
, n
, &pctldev
->gpio_ranges
, node
)
2169 list_del(&range
->node
);
2171 mutex_unlock(&pctldev
->mutex
);
2172 mutex_destroy(&pctldev
->mutex
);
2174 mutex_unlock(&pinctrldev_list_mutex
);
2176 EXPORT_SYMBOL_GPL(pinctrl_unregister
);
2178 static void devm_pinctrl_dev_release(struct device
*dev
, void *res
)
2180 struct pinctrl_dev
*pctldev
= *(struct pinctrl_dev
**)res
;
2182 pinctrl_unregister(pctldev
);
2185 static int devm_pinctrl_dev_match(struct device
*dev
, void *res
, void *data
)
2187 struct pctldev
**r
= res
;
2189 if (WARN_ON(!r
|| !*r
))
2196 * devm_pinctrl_register() - Resource managed version of pinctrl_register().
2197 * @dev: parent device for this pin controller
2198 * @pctldesc: descriptor for this pin controller
2199 * @driver_data: private pin controller data for this pin controller
2201 * Returns an error pointer if pincontrol register failed. Otherwise
2202 * it returns valid pinctrl handle.
2204 * The pinctrl device will be automatically released when the device is unbound.
2206 struct pinctrl_dev
*devm_pinctrl_register(struct device
*dev
,
2207 struct pinctrl_desc
*pctldesc
,
2210 struct pinctrl_dev
**ptr
, *pctldev
;
2212 ptr
= devres_alloc(devm_pinctrl_dev_release
, sizeof(*ptr
), GFP_KERNEL
);
2214 return ERR_PTR(-ENOMEM
);
2216 pctldev
= pinctrl_register(pctldesc
, dev
, driver_data
);
2217 if (IS_ERR(pctldev
)) {
2223 devres_add(dev
, ptr
);
2227 EXPORT_SYMBOL_GPL(devm_pinctrl_register
);
2230 * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
2231 * @dev: parent device for this pin controller
2232 * @pctldesc: descriptor for this pin controller
2233 * @driver_data: private pin controller data for this pin controller
2235 * Returns an error pointer if pincontrol register failed. Otherwise
2236 * it returns valid pinctrl handle.
2238 * The pinctrl device will be automatically released when the device is unbound.
2240 int devm_pinctrl_register_and_init(struct device
*dev
,
2241 struct pinctrl_desc
*pctldesc
,
2243 struct pinctrl_dev
**pctldev
)
2245 struct pinctrl_dev
**ptr
;
2248 ptr
= devres_alloc(devm_pinctrl_dev_release
, sizeof(*ptr
), GFP_KERNEL
);
2252 error
= pinctrl_register_and_init(pctldesc
, dev
, driver_data
, pctldev
);
2259 devres_add(dev
, ptr
);
2263 EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init
);
2266 * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
2267 * @dev: device for which which resource was allocated
2268 * @pctldev: the pinctrl device to unregister.
2270 void devm_pinctrl_unregister(struct device
*dev
, struct pinctrl_dev
*pctldev
)
2272 WARN_ON(devres_release(dev
, devm_pinctrl_dev_release
,
2273 devm_pinctrl_dev_match
, pctldev
));
2275 EXPORT_SYMBOL_GPL(devm_pinctrl_unregister
);
2277 static int __init
pinctrl_init(void)
2279 pr_info("initialized pinctrl subsystem\n");
2280 pinctrl_init_debugfs();
2284 /* init early since many drivers really need to initialized pinmux early */
2285 core_initcall(pinctrl_init
);