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>
30 #include <asm-generic/gpio.h>
32 #include "devicetree.h"
37 static bool pinctrl_dummy_state
;
39 /* Mutex taken by all entry points */
40 DEFINE_MUTEX(pinctrl_mutex
);
42 /* Global list of pin control devices (struct pinctrl_dev) */
43 LIST_HEAD(pinctrldev_list
);
45 /* List of pin controller handles (struct pinctrl) */
46 static LIST_HEAD(pinctrl_list
);
48 /* List of pinctrl maps (struct pinctrl_maps) */
49 LIST_HEAD(pinctrl_maps
);
53 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
55 * Usually this function is called by platforms without pinctrl driver support
56 * but run with some shared drivers using pinctrl APIs.
57 * After calling this function, the pinctrl core will return successfully
58 * with creating a dummy state for the driver to keep going smoothly.
60 void pinctrl_provide_dummies(void)
62 pinctrl_dummy_state
= true;
65 const char *pinctrl_dev_get_name(struct pinctrl_dev
*pctldev
)
67 /* We're not allowed to register devices without name */
68 return pctldev
->desc
->name
;
70 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name
);
72 const char *pinctrl_dev_get_devname(struct pinctrl_dev
*pctldev
)
74 return dev_name(pctldev
->dev
);
76 EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname
);
78 void *pinctrl_dev_get_drvdata(struct pinctrl_dev
*pctldev
)
80 return pctldev
->driver_data
;
82 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata
);
85 * get_pinctrl_dev_from_devname() - look up pin controller device
86 * @devname: the name of a device instance, as returned by dev_name()
88 * Looks up a pin control device matching a certain device name or pure device
89 * pointer, the pure device pointer will take precedence.
91 struct pinctrl_dev
*get_pinctrl_dev_from_devname(const char *devname
)
93 struct pinctrl_dev
*pctldev
= NULL
;
99 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
100 if (!strcmp(dev_name(pctldev
->dev
), devname
)) {
101 /* Matched on device name */
107 return found
? pctldev
: NULL
;
111 * pin_get_from_name() - look up a pin number from a name
112 * @pctldev: the pin control device to lookup the pin on
113 * @name: the name of the pin to look up
115 int pin_get_from_name(struct pinctrl_dev
*pctldev
, const char *name
)
119 /* The pin number can be retrived from the pin controller descriptor */
120 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
121 struct pin_desc
*desc
;
123 pin
= pctldev
->desc
->pins
[i
].number
;
124 desc
= pin_desc_get(pctldev
, pin
);
125 /* Pin space may be sparse */
128 if (desc
->name
&& !strcmp(name
, desc
->name
))
136 * pin_get_name_from_id() - look up a pin name from a pin id
137 * @pctldev: the pin control device to lookup the pin on
138 * @name: the name of the pin to look up
140 const char *pin_get_name(struct pinctrl_dev
*pctldev
, const unsigned pin
)
142 const struct pin_desc
*desc
;
144 desc
= pin_desc_get(pctldev
, pin
);
146 dev_err(pctldev
->dev
, "failed to get pin(%d) name\n",
155 * pin_is_valid() - check if pin exists on controller
156 * @pctldev: the pin control device to check the pin on
157 * @pin: pin to check, use the local pin controller index number
159 * This tells us whether a certain pin exist on a certain pin controller or
160 * not. Pin lists may be sparse, so some pins may not exist.
162 bool pin_is_valid(struct pinctrl_dev
*pctldev
, int pin
)
164 struct pin_desc
*pindesc
;
169 mutex_lock(&pinctrl_mutex
);
170 pindesc
= pin_desc_get(pctldev
, pin
);
171 mutex_unlock(&pinctrl_mutex
);
173 return pindesc
!= NULL
;
175 EXPORT_SYMBOL_GPL(pin_is_valid
);
177 /* Deletes a range of pin descriptors */
178 static void pinctrl_free_pindescs(struct pinctrl_dev
*pctldev
,
179 const struct pinctrl_pin_desc
*pins
,
184 for (i
= 0; i
< num_pins
; i
++) {
185 struct pin_desc
*pindesc
;
187 pindesc
= radix_tree_lookup(&pctldev
->pin_desc_tree
,
189 if (pindesc
!= NULL
) {
190 radix_tree_delete(&pctldev
->pin_desc_tree
,
192 if (pindesc
->dynamic_name
)
193 kfree(pindesc
->name
);
199 static int pinctrl_register_one_pin(struct pinctrl_dev
*pctldev
,
200 unsigned number
, const char *name
)
202 struct pin_desc
*pindesc
;
204 pindesc
= pin_desc_get(pctldev
, number
);
205 if (pindesc
!= NULL
) {
206 pr_err("pin %d already registered on %s\n", number
,
207 pctldev
->desc
->name
);
211 pindesc
= kzalloc(sizeof(*pindesc
), GFP_KERNEL
);
212 if (pindesc
== NULL
) {
213 dev_err(pctldev
->dev
, "failed to alloc struct pin_desc\n");
218 pindesc
->pctldev
= pctldev
;
220 /* Copy basic pin info */
222 pindesc
->name
= name
;
224 pindesc
->name
= kasprintf(GFP_KERNEL
, "PIN%u", number
);
225 if (pindesc
->name
== NULL
) {
229 pindesc
->dynamic_name
= true;
232 radix_tree_insert(&pctldev
->pin_desc_tree
, number
, pindesc
);
233 pr_debug("registered pin %d (%s) on %s\n",
234 number
, pindesc
->name
, pctldev
->desc
->name
);
238 static int pinctrl_register_pins(struct pinctrl_dev
*pctldev
,
239 struct pinctrl_pin_desc
const *pins
,
245 for (i
= 0; i
< num_descs
; i
++) {
246 ret
= pinctrl_register_one_pin(pctldev
,
247 pins
[i
].number
, pins
[i
].name
);
256 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
257 * @pctldev: pin controller device to check
258 * @gpio: gpio pin to check taken from the global GPIO pin space
260 * Tries to match a GPIO pin number to the ranges handled by a certain pin
261 * controller, return the range or NULL
263 static struct pinctrl_gpio_range
*
264 pinctrl_match_gpio_range(struct pinctrl_dev
*pctldev
, unsigned gpio
)
266 struct pinctrl_gpio_range
*range
= NULL
;
268 /* Loop over the ranges */
269 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
270 /* Check if we're in the valid range */
271 if (gpio
>= range
->base
&&
272 gpio
< range
->base
+ range
->npins
) {
281 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
282 * the same GPIO chip are in range
283 * @gpio: gpio pin to check taken from the global GPIO pin space
285 * This function is complement of pinctrl_match_gpio_range(). If the return
286 * value of pinctrl_match_gpio_range() is NULL, this function could be used
287 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
288 * of the same GPIO chip don't have back-end pinctrl interface.
289 * If the return value is true, it means that pinctrl device is ready & the
290 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
291 * is false, it means that pinctrl device may not be ready.
293 static bool pinctrl_ready_for_gpio_range(unsigned gpio
)
295 struct pinctrl_dev
*pctldev
;
296 struct pinctrl_gpio_range
*range
= NULL
;
297 struct gpio_chip
*chip
= gpio_to_chip(gpio
);
299 /* Loop over the pin controllers */
300 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
301 /* Loop over the ranges */
302 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
303 /* Check if any gpio range overlapped with gpio chip */
304 if (range
->base
+ range
->npins
- 1 < chip
->base
||
305 range
->base
> chip
->base
+ chip
->ngpio
- 1)
314 * pinctrl_get_device_gpio_range() - find device for GPIO range
315 * @gpio: the pin to locate the pin controller for
316 * @outdev: the pin control device if found
317 * @outrange: the GPIO range if found
319 * Find the pin controller handling a certain GPIO pin from the pinspace of
320 * the GPIO subsystem, return the device and the matching GPIO range. Returns
321 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
322 * may still have not been registered.
324 static int pinctrl_get_device_gpio_range(unsigned gpio
,
325 struct pinctrl_dev
**outdev
,
326 struct pinctrl_gpio_range
**outrange
)
328 struct pinctrl_dev
*pctldev
= NULL
;
330 /* Loop over the pin controllers */
331 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
332 struct pinctrl_gpio_range
*range
;
334 range
= pinctrl_match_gpio_range(pctldev
, gpio
);
342 return -EPROBE_DEFER
;
346 * pinctrl_add_gpio_range() - register a GPIO range for a controller
347 * @pctldev: pin controller device to add the range to
348 * @range: the GPIO range to add
350 * This adds a range of GPIOs to be handled by a certain pin controller. Call
351 * this to register handled ranges after registering your pin controller.
353 void pinctrl_add_gpio_range(struct pinctrl_dev
*pctldev
,
354 struct pinctrl_gpio_range
*range
)
356 mutex_lock(&pinctrl_mutex
);
357 list_add_tail(&range
->node
, &pctldev
->gpio_ranges
);
358 mutex_unlock(&pinctrl_mutex
);
360 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range
);
362 void pinctrl_add_gpio_ranges(struct pinctrl_dev
*pctldev
,
363 struct pinctrl_gpio_range
*ranges
,
368 for (i
= 0; i
< nranges
; i
++)
369 pinctrl_add_gpio_range(pctldev
, &ranges
[i
]);
371 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges
);
373 struct pinctrl_dev
*pinctrl_find_and_add_gpio_range(const char *devname
,
374 struct pinctrl_gpio_range
*range
)
376 struct pinctrl_dev
*pctldev
= get_pinctrl_dev_from_devname(devname
);
379 * If we can't find this device, let's assume that is because
380 * it has not probed yet, so the driver trying to register this
381 * range need to defer probing.
384 return ERR_PTR(-EPROBE_DEFER
);
386 pinctrl_add_gpio_range(pctldev
, range
);
389 EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range
);
392 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
393 * @pctldev: the pin controller device to look in
394 * @pin: a controller-local number to find the range for
396 struct pinctrl_gpio_range
*
397 pinctrl_find_gpio_range_from_pin(struct pinctrl_dev
*pctldev
,
400 struct pinctrl_gpio_range
*range
= NULL
;
402 /* Loop over the ranges */
403 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
404 /* Check if we're in the valid range */
405 if (pin
>= range
->pin_base
&&
406 pin
< range
->pin_base
+ range
->npins
) {
413 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin
);
416 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
417 * @pctldev: pin controller device to remove the range from
418 * @range: the GPIO range to remove
420 void pinctrl_remove_gpio_range(struct pinctrl_dev
*pctldev
,
421 struct pinctrl_gpio_range
*range
)
423 mutex_lock(&pinctrl_mutex
);
424 list_del(&range
->node
);
425 mutex_unlock(&pinctrl_mutex
);
427 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range
);
430 * pinctrl_get_group_selector() - returns the group selector for a group
431 * @pctldev: the pin controller handling the group
432 * @pin_group: the pin group to look up
434 int pinctrl_get_group_selector(struct pinctrl_dev
*pctldev
,
435 const char *pin_group
)
437 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
438 unsigned ngroups
= pctlops
->get_groups_count(pctldev
);
439 unsigned group_selector
= 0;
441 while (group_selector
< ngroups
) {
442 const char *gname
= pctlops
->get_group_name(pctldev
,
444 if (!strcmp(gname
, pin_group
)) {
445 dev_dbg(pctldev
->dev
,
446 "found group selector %u for %s\n",
449 return group_selector
;
455 dev_err(pctldev
->dev
, "does not have pin group %s\n",
462 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
463 * @gpio: the GPIO pin number from the GPIO subsystem number space
465 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
466 * as part of their gpio_request() semantics, platforms and individual drivers
467 * shall *NOT* request GPIO pins to be muxed in.
469 int pinctrl_request_gpio(unsigned gpio
)
471 struct pinctrl_dev
*pctldev
;
472 struct pinctrl_gpio_range
*range
;
476 mutex_lock(&pinctrl_mutex
);
478 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
480 if (pinctrl_ready_for_gpio_range(gpio
))
482 mutex_unlock(&pinctrl_mutex
);
486 /* Convert to the pin controllers number space */
487 pin
= gpio
- range
->base
+ range
->pin_base
;
489 ret
= pinmux_request_gpio(pctldev
, range
, pin
, gpio
);
491 mutex_unlock(&pinctrl_mutex
);
494 EXPORT_SYMBOL_GPL(pinctrl_request_gpio
);
497 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
498 * @gpio: the GPIO pin number from the GPIO subsystem number space
500 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
501 * as part of their gpio_free() semantics, platforms and individual drivers
502 * shall *NOT* request GPIO pins to be muxed out.
504 void pinctrl_free_gpio(unsigned gpio
)
506 struct pinctrl_dev
*pctldev
;
507 struct pinctrl_gpio_range
*range
;
511 mutex_lock(&pinctrl_mutex
);
513 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
515 mutex_unlock(&pinctrl_mutex
);
519 /* Convert to the pin controllers number space */
520 pin
= gpio
- range
->base
+ range
->pin_base
;
522 pinmux_free_gpio(pctldev
, pin
, range
);
524 mutex_unlock(&pinctrl_mutex
);
526 EXPORT_SYMBOL_GPL(pinctrl_free_gpio
);
528 static int pinctrl_gpio_direction(unsigned gpio
, bool input
)
530 struct pinctrl_dev
*pctldev
;
531 struct pinctrl_gpio_range
*range
;
535 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
539 /* Convert to the pin controllers number space */
540 pin
= gpio
- range
->base
+ range
->pin_base
;
542 return pinmux_gpio_direction(pctldev
, range
, pin
, input
);
546 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
547 * @gpio: the GPIO pin number from the GPIO subsystem number space
549 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
550 * as part of their gpio_direction_input() semantics, platforms and individual
551 * drivers shall *NOT* touch pin control GPIO calls.
553 int pinctrl_gpio_direction_input(unsigned gpio
)
556 mutex_lock(&pinctrl_mutex
);
557 ret
= pinctrl_gpio_direction(gpio
, true);
558 mutex_unlock(&pinctrl_mutex
);
561 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input
);
564 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
565 * @gpio: the GPIO pin number from the GPIO subsystem number space
567 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
568 * as part of their gpio_direction_output() semantics, platforms and individual
569 * drivers shall *NOT* touch pin control GPIO calls.
571 int pinctrl_gpio_direction_output(unsigned gpio
)
574 mutex_lock(&pinctrl_mutex
);
575 ret
= pinctrl_gpio_direction(gpio
, false);
576 mutex_unlock(&pinctrl_mutex
);
579 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output
);
581 static struct pinctrl_state
*find_state(struct pinctrl
*p
,
584 struct pinctrl_state
*state
;
586 list_for_each_entry(state
, &p
->states
, node
)
587 if (!strcmp(state
->name
, name
))
593 static struct pinctrl_state
*create_state(struct pinctrl
*p
,
596 struct pinctrl_state
*state
;
598 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
601 "failed to alloc struct pinctrl_state\n");
602 return ERR_PTR(-ENOMEM
);
606 INIT_LIST_HEAD(&state
->settings
);
608 list_add_tail(&state
->node
, &p
->states
);
613 static int add_setting(struct pinctrl
*p
, struct pinctrl_map
const *map
)
615 struct pinctrl_state
*state
;
616 struct pinctrl_setting
*setting
;
619 state
= find_state(p
, map
->name
);
621 state
= create_state(p
, map
->name
);
623 return PTR_ERR(state
);
625 if (map
->type
== PIN_MAP_TYPE_DUMMY_STATE
)
628 setting
= kzalloc(sizeof(*setting
), GFP_KERNEL
);
629 if (setting
== NULL
) {
631 "failed to alloc struct pinctrl_setting\n");
635 setting
->type
= map
->type
;
637 setting
->pctldev
= get_pinctrl_dev_from_devname(map
->ctrl_dev_name
);
638 if (setting
->pctldev
== NULL
) {
640 /* Do not defer probing of hogs (circular loop) */
641 if (!strcmp(map
->ctrl_dev_name
, map
->dev_name
))
644 * OK let us guess that the driver is not there yet, and
645 * let's defer obtaining this pinctrl handle to later...
647 dev_info(p
->dev
, "unknown pinctrl device %s in map entry, deferring probe",
649 return -EPROBE_DEFER
;
652 setting
->dev_name
= map
->dev_name
;
655 case PIN_MAP_TYPE_MUX_GROUP
:
656 ret
= pinmux_map_to_setting(map
, setting
);
658 case PIN_MAP_TYPE_CONFIGS_PIN
:
659 case PIN_MAP_TYPE_CONFIGS_GROUP
:
660 ret
= pinconf_map_to_setting(map
, setting
);
671 list_add_tail(&setting
->node
, &state
->settings
);
676 static struct pinctrl
*find_pinctrl(struct device
*dev
)
680 list_for_each_entry(p
, &pinctrl_list
, node
)
687 static void pinctrl_put_locked(struct pinctrl
*p
, bool inlist
);
689 static struct pinctrl
*create_pinctrl(struct device
*dev
)
693 struct pinctrl_maps
*maps_node
;
695 struct pinctrl_map
const *map
;
699 * create the state cookie holder struct pinctrl for each
700 * mapping, this is what consumers will get when requesting
701 * a pin control handle with pinctrl_get()
703 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
705 dev_err(dev
, "failed to alloc struct pinctrl\n");
706 return ERR_PTR(-ENOMEM
);
709 INIT_LIST_HEAD(&p
->states
);
710 INIT_LIST_HEAD(&p
->dt_maps
);
712 ret
= pinctrl_dt_to_map(p
);
718 devname
= dev_name(dev
);
720 /* Iterate over the pin control maps to locate the right ones */
721 for_each_maps(maps_node
, i
, map
) {
722 /* Map must be for this device */
723 if (strcmp(map
->dev_name
, devname
))
726 ret
= add_setting(p
, map
);
728 * At this point the adding of a setting may:
730 * - Defer, if the pinctrl device is not yet available
731 * - Fail, if the pinctrl device is not yet available,
732 * AND the setting is a hog. We cannot defer that, since
733 * the hog will kick in immediately after the device
736 * If the error returned was not -EPROBE_DEFER then we
737 * accumulate the errors to see if we end up with
738 * an -EPROBE_DEFER later, as that is the worst case.
740 if (ret
== -EPROBE_DEFER
) {
741 pinctrl_put_locked(p
, false);
746 /* If some other error than deferral occured, return here */
747 pinctrl_put_locked(p
, false);
751 kref_init(&p
->users
);
753 /* Add the pinctrl handle to the global list */
754 list_add_tail(&p
->node
, &pinctrl_list
);
759 static struct pinctrl
*pinctrl_get_locked(struct device
*dev
)
764 return ERR_PTR(-EINVAL
);
767 * See if somebody else (such as the device core) has already
768 * obtained a handle to the pinctrl for this device. In that case,
769 * return another pointer to it.
771 p
= find_pinctrl(dev
);
773 dev_dbg(dev
, "obtain a copy of previously claimed pinctrl\n");
778 return create_pinctrl(dev
);
782 * pinctrl_get() - retrieves the pinctrl handle for a device
783 * @dev: the device to obtain the handle for
785 struct pinctrl
*pinctrl_get(struct device
*dev
)
789 mutex_lock(&pinctrl_mutex
);
790 p
= pinctrl_get_locked(dev
);
791 mutex_unlock(&pinctrl_mutex
);
795 EXPORT_SYMBOL_GPL(pinctrl_get
);
797 static void pinctrl_free_setting(bool disable_setting
,
798 struct pinctrl_setting
*setting
)
800 switch (setting
->type
) {
801 case PIN_MAP_TYPE_MUX_GROUP
:
803 pinmux_disable_setting(setting
);
804 pinmux_free_setting(setting
);
806 case PIN_MAP_TYPE_CONFIGS_PIN
:
807 case PIN_MAP_TYPE_CONFIGS_GROUP
:
808 pinconf_free_setting(setting
);
815 static void pinctrl_put_locked(struct pinctrl
*p
, bool inlist
)
817 struct pinctrl_state
*state
, *n1
;
818 struct pinctrl_setting
*setting
, *n2
;
820 list_for_each_entry_safe(state
, n1
, &p
->states
, node
) {
821 list_for_each_entry_safe(setting
, n2
, &state
->settings
, node
) {
822 pinctrl_free_setting(state
== p
->state
, setting
);
823 list_del(&setting
->node
);
826 list_del(&state
->node
);
830 pinctrl_dt_free_maps(p
);
838 * pinctrl_release() - release the pinctrl handle
839 * @kref: the kref in the pinctrl being released
841 static void pinctrl_release(struct kref
*kref
)
843 struct pinctrl
*p
= container_of(kref
, struct pinctrl
, users
);
845 pinctrl_put_locked(p
, true);
849 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
850 * @p: the pinctrl handle to release
852 void pinctrl_put(struct pinctrl
*p
)
854 mutex_lock(&pinctrl_mutex
);
855 kref_put(&p
->users
, pinctrl_release
);
856 mutex_unlock(&pinctrl_mutex
);
858 EXPORT_SYMBOL_GPL(pinctrl_put
);
860 static struct pinctrl_state
*pinctrl_lookup_state_locked(struct pinctrl
*p
,
863 struct pinctrl_state
*state
;
865 state
= find_state(p
, name
);
867 if (pinctrl_dummy_state
) {
868 /* create dummy state */
869 dev_dbg(p
->dev
, "using pinctrl dummy state (%s)\n",
871 state
= create_state(p
, name
);
873 state
= ERR_PTR(-ENODEV
);
880 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
881 * @p: the pinctrl handle to retrieve the state from
882 * @name: the state name to retrieve
884 struct pinctrl_state
*pinctrl_lookup_state(struct pinctrl
*p
, const char *name
)
886 struct pinctrl_state
*s
;
888 mutex_lock(&pinctrl_mutex
);
889 s
= pinctrl_lookup_state_locked(p
, name
);
890 mutex_unlock(&pinctrl_mutex
);
894 EXPORT_SYMBOL_GPL(pinctrl_lookup_state
);
896 static int pinctrl_select_state_locked(struct pinctrl
*p
,
897 struct pinctrl_state
*state
)
899 struct pinctrl_setting
*setting
, *setting2
;
902 if (p
->state
== state
)
907 * The set of groups with a mux configuration in the old state
908 * may not be identical to the set of groups with a mux setting
909 * in the new state. While this might be unusual, it's entirely
910 * possible for the "user"-supplied mapping table to be written
911 * that way. For each group that was configured in the old state
912 * but not in the new state, this code puts that group into a
913 * safe/disabled state.
915 list_for_each_entry(setting
, &p
->state
->settings
, node
) {
917 if (setting
->type
!= PIN_MAP_TYPE_MUX_GROUP
)
919 list_for_each_entry(setting2
, &state
->settings
, node
) {
920 if (setting2
->type
!= PIN_MAP_TYPE_MUX_GROUP
)
922 if (setting2
->data
.mux
.group
==
923 setting
->data
.mux
.group
) {
929 pinmux_disable_setting(setting
);
935 /* Apply all the settings for the new state */
936 list_for_each_entry(setting
, &state
->settings
, node
) {
937 switch (setting
->type
) {
938 case PIN_MAP_TYPE_MUX_GROUP
:
939 ret
= pinmux_enable_setting(setting
);
941 case PIN_MAP_TYPE_CONFIGS_PIN
:
942 case PIN_MAP_TYPE_CONFIGS_GROUP
:
943 ret
= pinconf_apply_setting(setting
);
950 /* FIXME: Difficult to return to prev state */
959 * pinctrl_select() - select/activate/program a pinctrl state to HW
960 * @p: the pinctrl handle for the device that requests configuratio
961 * @state: the state handle to select/activate/program
963 int pinctrl_select_state(struct pinctrl
*p
, struct pinctrl_state
*state
)
967 mutex_lock(&pinctrl_mutex
);
968 ret
= pinctrl_select_state_locked(p
, state
);
969 mutex_unlock(&pinctrl_mutex
);
973 EXPORT_SYMBOL_GPL(pinctrl_select_state
);
975 static void devm_pinctrl_release(struct device
*dev
, void *res
)
977 pinctrl_put(*(struct pinctrl
**)res
);
981 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
982 * @dev: the device to obtain the handle for
984 * If there is a need to explicitly destroy the returned struct pinctrl,
985 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
987 struct pinctrl
*devm_pinctrl_get(struct device
*dev
)
989 struct pinctrl
**ptr
, *p
;
991 ptr
= devres_alloc(devm_pinctrl_release
, sizeof(*ptr
), GFP_KERNEL
);
993 return ERR_PTR(-ENOMEM
);
995 p
= pinctrl_get(dev
);
998 devres_add(dev
, ptr
);
1005 EXPORT_SYMBOL_GPL(devm_pinctrl_get
);
1007 static int devm_pinctrl_match(struct device
*dev
, void *res
, void *data
)
1009 struct pinctrl
**p
= res
;
1015 * devm_pinctrl_put() - Resource managed pinctrl_put()
1016 * @p: the pinctrl handle to release
1018 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1019 * this function will not need to be called and the resource management
1020 * code will ensure that the resource is freed.
1022 void devm_pinctrl_put(struct pinctrl
*p
)
1024 WARN_ON(devres_release(p
->dev
, devm_pinctrl_release
,
1025 devm_pinctrl_match
, p
));
1027 EXPORT_SYMBOL_GPL(devm_pinctrl_put
);
1029 int pinctrl_register_map(struct pinctrl_map
const *maps
, unsigned num_maps
,
1030 bool dup
, bool locked
)
1033 struct pinctrl_maps
*maps_node
;
1035 pr_debug("add %d pinmux maps\n", num_maps
);
1037 /* First sanity check the new mapping */
1038 for (i
= 0; i
< num_maps
; i
++) {
1039 if (!maps
[i
].dev_name
) {
1040 pr_err("failed to register map %s (%d): no device given\n",
1045 if (!maps
[i
].name
) {
1046 pr_err("failed to register map %d: no map name given\n",
1051 if (maps
[i
].type
!= PIN_MAP_TYPE_DUMMY_STATE
&&
1052 !maps
[i
].ctrl_dev_name
) {
1053 pr_err("failed to register map %s (%d): no pin control device given\n",
1058 switch (maps
[i
].type
) {
1059 case PIN_MAP_TYPE_DUMMY_STATE
:
1061 case PIN_MAP_TYPE_MUX_GROUP
:
1062 ret
= pinmux_validate_map(&maps
[i
], i
);
1066 case PIN_MAP_TYPE_CONFIGS_PIN
:
1067 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1068 ret
= pinconf_validate_map(&maps
[i
], i
);
1073 pr_err("failed to register map %s (%d): invalid type given\n",
1079 maps_node
= kzalloc(sizeof(*maps_node
), GFP_KERNEL
);
1081 pr_err("failed to alloc struct pinctrl_maps\n");
1085 maps_node
->num_maps
= num_maps
;
1087 maps_node
->maps
= kmemdup(maps
, sizeof(*maps
) * num_maps
,
1089 if (!maps_node
->maps
) {
1090 pr_err("failed to duplicate mapping table\n");
1095 maps_node
->maps
= maps
;
1099 mutex_lock(&pinctrl_mutex
);
1100 list_add_tail(&maps_node
->node
, &pinctrl_maps
);
1102 mutex_unlock(&pinctrl_mutex
);
1108 * pinctrl_register_mappings() - register a set of pin controller mappings
1109 * @maps: the pincontrol mappings table to register. This should probably be
1110 * marked with __initdata so it can be discarded after boot. This
1111 * function will perform a shallow copy for the mapping entries.
1112 * @num_maps: the number of maps in the mapping table
1114 int pinctrl_register_mappings(struct pinctrl_map
const *maps
,
1117 return pinctrl_register_map(maps
, num_maps
, true, false);
1120 void pinctrl_unregister_map(struct pinctrl_map
const *map
)
1122 struct pinctrl_maps
*maps_node
;
1124 list_for_each_entry(maps_node
, &pinctrl_maps
, node
) {
1125 if (maps_node
->maps
== map
) {
1126 list_del(&maps_node
->node
);
1133 * pinctrl_force_sleep() - turn a given controller device into sleep state
1134 * @pctldev: pin controller device
1136 int pinctrl_force_sleep(struct pinctrl_dev
*pctldev
)
1138 if (!IS_ERR(pctldev
->p
) && !IS_ERR(pctldev
->hog_sleep
))
1139 return pinctrl_select_state(pctldev
->p
, pctldev
->hog_sleep
);
1142 EXPORT_SYMBOL_GPL(pinctrl_force_sleep
);
1145 * pinctrl_force_default() - turn a given controller device into default state
1146 * @pctldev: pin controller device
1148 int pinctrl_force_default(struct pinctrl_dev
*pctldev
)
1150 if (!IS_ERR(pctldev
->p
) && !IS_ERR(pctldev
->hog_default
))
1151 return pinctrl_select_state(pctldev
->p
, pctldev
->hog_default
);
1154 EXPORT_SYMBOL_GPL(pinctrl_force_default
);
1156 #ifdef CONFIG_DEBUG_FS
1158 static int pinctrl_pins_show(struct seq_file
*s
, void *what
)
1160 struct pinctrl_dev
*pctldev
= s
->private;
1161 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1164 seq_printf(s
, "registered pins: %d\n", pctldev
->desc
->npins
);
1166 mutex_lock(&pinctrl_mutex
);
1168 /* The pin number can be retrived from the pin controller descriptor */
1169 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
1170 struct pin_desc
*desc
;
1172 pin
= pctldev
->desc
->pins
[i
].number
;
1173 desc
= pin_desc_get(pctldev
, pin
);
1174 /* Pin space may be sparse */
1178 seq_printf(s
, "pin %d (%s) ", pin
,
1179 desc
->name
? desc
->name
: "unnamed");
1181 /* Driver-specific info per pin */
1182 if (ops
->pin_dbg_show
)
1183 ops
->pin_dbg_show(pctldev
, s
, pin
);
1188 mutex_unlock(&pinctrl_mutex
);
1193 static int pinctrl_groups_show(struct seq_file
*s
, void *what
)
1195 struct pinctrl_dev
*pctldev
= s
->private;
1196 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1197 unsigned ngroups
, selector
= 0;
1199 ngroups
= ops
->get_groups_count(pctldev
);
1200 mutex_lock(&pinctrl_mutex
);
1202 seq_puts(s
, "registered pin groups:\n");
1203 while (selector
< ngroups
) {
1204 const unsigned *pins
;
1206 const char *gname
= ops
->get_group_name(pctldev
, selector
);
1211 ret
= ops
->get_group_pins(pctldev
, selector
,
1214 seq_printf(s
, "%s [ERROR GETTING PINS]\n",
1217 seq_printf(s
, "group: %s\n", gname
);
1218 for (i
= 0; i
< num_pins
; i
++) {
1219 pname
= pin_get_name(pctldev
, pins
[i
]);
1220 if (WARN_ON(!pname
)) {
1221 mutex_unlock(&pinctrl_mutex
);
1224 seq_printf(s
, "pin %d (%s)\n", pins
[i
], pname
);
1231 mutex_unlock(&pinctrl_mutex
);
1236 static int pinctrl_gpioranges_show(struct seq_file
*s
, void *what
)
1238 struct pinctrl_dev
*pctldev
= s
->private;
1239 struct pinctrl_gpio_range
*range
= NULL
;
1241 seq_puts(s
, "GPIO ranges handled:\n");
1243 mutex_lock(&pinctrl_mutex
);
1245 /* Loop over the ranges */
1246 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
1247 seq_printf(s
, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1248 range
->id
, range
->name
,
1249 range
->base
, (range
->base
+ range
->npins
- 1),
1251 (range
->pin_base
+ range
->npins
- 1));
1254 mutex_unlock(&pinctrl_mutex
);
1259 static int pinctrl_devices_show(struct seq_file
*s
, void *what
)
1261 struct pinctrl_dev
*pctldev
;
1263 seq_puts(s
, "name [pinmux] [pinconf]\n");
1265 mutex_lock(&pinctrl_mutex
);
1267 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
1268 seq_printf(s
, "%s ", pctldev
->desc
->name
);
1269 if (pctldev
->desc
->pmxops
)
1270 seq_puts(s
, "yes ");
1273 if (pctldev
->desc
->confops
)
1280 mutex_unlock(&pinctrl_mutex
);
1285 static inline const char *map_type(enum pinctrl_map_type type
)
1287 static const char * const names
[] = {
1295 if (type
>= ARRAY_SIZE(names
))
1301 static int pinctrl_maps_show(struct seq_file
*s
, void *what
)
1303 struct pinctrl_maps
*maps_node
;
1305 struct pinctrl_map
const *map
;
1307 seq_puts(s
, "Pinctrl maps:\n");
1309 mutex_lock(&pinctrl_mutex
);
1311 for_each_maps(maps_node
, i
, map
) {
1312 seq_printf(s
, "device %s\nstate %s\ntype %s (%d)\n",
1313 map
->dev_name
, map
->name
, map_type(map
->type
),
1316 if (map
->type
!= PIN_MAP_TYPE_DUMMY_STATE
)
1317 seq_printf(s
, "controlling device %s\n",
1318 map
->ctrl_dev_name
);
1320 switch (map
->type
) {
1321 case PIN_MAP_TYPE_MUX_GROUP
:
1322 pinmux_show_map(s
, map
);
1324 case PIN_MAP_TYPE_CONFIGS_PIN
:
1325 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1326 pinconf_show_map(s
, map
);
1332 seq_printf(s
, "\n");
1335 mutex_unlock(&pinctrl_mutex
);
1340 static int pinctrl_show(struct seq_file
*s
, void *what
)
1343 struct pinctrl_state
*state
;
1344 struct pinctrl_setting
*setting
;
1346 seq_puts(s
, "Requested pin control handlers their pinmux maps:\n");
1348 mutex_lock(&pinctrl_mutex
);
1350 list_for_each_entry(p
, &pinctrl_list
, node
) {
1351 seq_printf(s
, "device: %s current state: %s\n",
1353 p
->state
? p
->state
->name
: "none");
1355 list_for_each_entry(state
, &p
->states
, node
) {
1356 seq_printf(s
, " state: %s\n", state
->name
);
1358 list_for_each_entry(setting
, &state
->settings
, node
) {
1359 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
1361 seq_printf(s
, " type: %s controller %s ",
1362 map_type(setting
->type
),
1363 pinctrl_dev_get_name(pctldev
));
1365 switch (setting
->type
) {
1366 case PIN_MAP_TYPE_MUX_GROUP
:
1367 pinmux_show_setting(s
, setting
);
1369 case PIN_MAP_TYPE_CONFIGS_PIN
:
1370 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1371 pinconf_show_setting(s
, setting
);
1380 mutex_unlock(&pinctrl_mutex
);
1385 static int pinctrl_pins_open(struct inode
*inode
, struct file
*file
)
1387 return single_open(file
, pinctrl_pins_show
, inode
->i_private
);
1390 static int pinctrl_groups_open(struct inode
*inode
, struct file
*file
)
1392 return single_open(file
, pinctrl_groups_show
, inode
->i_private
);
1395 static int pinctrl_gpioranges_open(struct inode
*inode
, struct file
*file
)
1397 return single_open(file
, pinctrl_gpioranges_show
, inode
->i_private
);
1400 static int pinctrl_devices_open(struct inode
*inode
, struct file
*file
)
1402 return single_open(file
, pinctrl_devices_show
, NULL
);
1405 static int pinctrl_maps_open(struct inode
*inode
, struct file
*file
)
1407 return single_open(file
, pinctrl_maps_show
, NULL
);
1410 static int pinctrl_open(struct inode
*inode
, struct file
*file
)
1412 return single_open(file
, pinctrl_show
, NULL
);
1415 static const struct file_operations pinctrl_pins_ops
= {
1416 .open
= pinctrl_pins_open
,
1418 .llseek
= seq_lseek
,
1419 .release
= single_release
,
1422 static const struct file_operations pinctrl_groups_ops
= {
1423 .open
= pinctrl_groups_open
,
1425 .llseek
= seq_lseek
,
1426 .release
= single_release
,
1429 static const struct file_operations pinctrl_gpioranges_ops
= {
1430 .open
= pinctrl_gpioranges_open
,
1432 .llseek
= seq_lseek
,
1433 .release
= single_release
,
1436 static const struct file_operations pinctrl_devices_ops
= {
1437 .open
= pinctrl_devices_open
,
1439 .llseek
= seq_lseek
,
1440 .release
= single_release
,
1443 static const struct file_operations pinctrl_maps_ops
= {
1444 .open
= pinctrl_maps_open
,
1446 .llseek
= seq_lseek
,
1447 .release
= single_release
,
1450 static const struct file_operations pinctrl_ops
= {
1451 .open
= pinctrl_open
,
1453 .llseek
= seq_lseek
,
1454 .release
= single_release
,
1457 static struct dentry
*debugfs_root
;
1459 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1461 struct dentry
*device_root
;
1463 device_root
= debugfs_create_dir(dev_name(pctldev
->dev
),
1465 pctldev
->device_root
= device_root
;
1467 if (IS_ERR(device_root
) || !device_root
) {
1468 pr_warn("failed to create debugfs directory for %s\n",
1469 dev_name(pctldev
->dev
));
1472 debugfs_create_file("pins", S_IFREG
| S_IRUGO
,
1473 device_root
, pctldev
, &pinctrl_pins_ops
);
1474 debugfs_create_file("pingroups", S_IFREG
| S_IRUGO
,
1475 device_root
, pctldev
, &pinctrl_groups_ops
);
1476 debugfs_create_file("gpio-ranges", S_IFREG
| S_IRUGO
,
1477 device_root
, pctldev
, &pinctrl_gpioranges_ops
);
1478 pinmux_init_device_debugfs(device_root
, pctldev
);
1479 pinconf_init_device_debugfs(device_root
, pctldev
);
1482 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1484 debugfs_remove_recursive(pctldev
->device_root
);
1487 static void pinctrl_init_debugfs(void)
1489 debugfs_root
= debugfs_create_dir("pinctrl", NULL
);
1490 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
1491 pr_warn("failed to create debugfs directory\n");
1492 debugfs_root
= NULL
;
1496 debugfs_create_file("pinctrl-devices", S_IFREG
| S_IRUGO
,
1497 debugfs_root
, NULL
, &pinctrl_devices_ops
);
1498 debugfs_create_file("pinctrl-maps", S_IFREG
| S_IRUGO
,
1499 debugfs_root
, NULL
, &pinctrl_maps_ops
);
1500 debugfs_create_file("pinctrl-handles", S_IFREG
| S_IRUGO
,
1501 debugfs_root
, NULL
, &pinctrl_ops
);
1504 #else /* CONFIG_DEBUG_FS */
1506 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1510 static void pinctrl_init_debugfs(void)
1514 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1520 static int pinctrl_check_ops(struct pinctrl_dev
*pctldev
)
1522 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1525 !ops
->get_groups_count
||
1526 !ops
->get_group_name
||
1527 !ops
->get_group_pins
)
1530 if (ops
->dt_node_to_map
&& !ops
->dt_free_map
)
1537 * pinctrl_register() - register a pin controller device
1538 * @pctldesc: descriptor for this pin controller
1539 * @dev: parent device for this pin controller
1540 * @driver_data: private pin controller data for this pin controller
1542 struct pinctrl_dev
*pinctrl_register(struct pinctrl_desc
*pctldesc
,
1543 struct device
*dev
, void *driver_data
)
1545 struct pinctrl_dev
*pctldev
;
1550 if (!pctldesc
->name
)
1553 pctldev
= kzalloc(sizeof(*pctldev
), GFP_KERNEL
);
1554 if (pctldev
== NULL
) {
1555 dev_err(dev
, "failed to alloc struct pinctrl_dev\n");
1559 /* Initialize pin control device struct */
1560 pctldev
->owner
= pctldesc
->owner
;
1561 pctldev
->desc
= pctldesc
;
1562 pctldev
->driver_data
= driver_data
;
1563 INIT_RADIX_TREE(&pctldev
->pin_desc_tree
, GFP_KERNEL
);
1564 INIT_LIST_HEAD(&pctldev
->gpio_ranges
);
1567 /* check core ops for sanity */
1568 if (pinctrl_check_ops(pctldev
)) {
1569 dev_err(dev
, "pinctrl ops lacks necessary functions\n");
1573 /* If we're implementing pinmuxing, check the ops for sanity */
1574 if (pctldesc
->pmxops
) {
1575 if (pinmux_check_ops(pctldev
))
1579 /* If we're implementing pinconfig, check the ops for sanity */
1580 if (pctldesc
->confops
) {
1581 if (pinconf_check_ops(pctldev
))
1585 /* Register all the pins */
1586 dev_dbg(dev
, "try to register %d pins ...\n", pctldesc
->npins
);
1587 ret
= pinctrl_register_pins(pctldev
, pctldesc
->pins
, pctldesc
->npins
);
1589 dev_err(dev
, "error during pin registration\n");
1590 pinctrl_free_pindescs(pctldev
, pctldesc
->pins
,
1595 mutex_lock(&pinctrl_mutex
);
1597 list_add_tail(&pctldev
->node
, &pinctrldev_list
);
1599 pctldev
->p
= pinctrl_get_locked(pctldev
->dev
);
1600 if (!IS_ERR(pctldev
->p
)) {
1601 pctldev
->hog_default
=
1602 pinctrl_lookup_state_locked(pctldev
->p
,
1603 PINCTRL_STATE_DEFAULT
);
1604 if (IS_ERR(pctldev
->hog_default
)) {
1605 dev_dbg(dev
, "failed to lookup the default state\n");
1607 if (pinctrl_select_state_locked(pctldev
->p
,
1608 pctldev
->hog_default
))
1610 "failed to select default state\n");
1613 pctldev
->hog_sleep
=
1614 pinctrl_lookup_state_locked(pctldev
->p
,
1615 PINCTRL_STATE_SLEEP
);
1616 if (IS_ERR(pctldev
->hog_sleep
))
1617 dev_dbg(dev
, "failed to lookup the sleep state\n");
1620 mutex_unlock(&pinctrl_mutex
);
1622 pinctrl_init_device_debugfs(pctldev
);
1630 EXPORT_SYMBOL_GPL(pinctrl_register
);
1633 * pinctrl_unregister() - unregister pinmux
1634 * @pctldev: pin controller to unregister
1636 * Called by pinmux drivers to unregister a pinmux.
1638 void pinctrl_unregister(struct pinctrl_dev
*pctldev
)
1640 struct pinctrl_gpio_range
*range
, *n
;
1641 if (pctldev
== NULL
)
1644 pinctrl_remove_device_debugfs(pctldev
);
1646 mutex_lock(&pinctrl_mutex
);
1648 if (!IS_ERR(pctldev
->p
))
1649 pinctrl_put_locked(pctldev
->p
, true);
1651 /* TODO: check that no pinmuxes are still active? */
1652 list_del(&pctldev
->node
);
1653 /* Destroy descriptor tree */
1654 pinctrl_free_pindescs(pctldev
, pctldev
->desc
->pins
,
1655 pctldev
->desc
->npins
);
1656 /* remove gpio ranges map */
1657 list_for_each_entry_safe(range
, n
, &pctldev
->gpio_ranges
, node
)
1658 list_del(&range
->node
);
1662 mutex_unlock(&pinctrl_mutex
);
1664 EXPORT_SYMBOL_GPL(pinctrl_unregister
);
1666 static int __init
pinctrl_init(void)
1668 pr_info("initialized pinctrl subsystem\n");
1669 pinctrl_init_debugfs();
1673 /* init early since many drivers really need to initialized pinmux early */
1674 core_initcall(pinctrl_init
);