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>
31 #include "devicetree.h"
36 * struct pinctrl_maps - a list item containing part of the mapping table
37 * @node: mapping table list node
38 * @maps: array of mapping table entries
39 * @num_maps: the number of entries in @maps
42 struct list_head node
;
43 struct pinctrl_map
const *maps
;
47 static bool pinctrl_dummy_state
;
49 /* Mutex taken by all entry points */
50 DEFINE_MUTEX(pinctrl_mutex
);
52 /* Global list of pin control devices (struct pinctrl_dev) */
53 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 static LIST_HEAD(pinctrl_maps
);
61 #define for_each_maps(_maps_node_, _i_, _map_) \
62 list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
63 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
64 _i_ < _maps_node_->num_maps; \
65 _i_++, _map_ = &_maps_node_->maps[_i_])
68 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
70 * Usually this function is called by platforms without pinctrl driver support
71 * but run with some shared drivers using pinctrl APIs.
72 * After calling this function, the pinctrl core will return successfully
73 * with creating a dummy state for the driver to keep going smoothly.
75 void pinctrl_provide_dummies(void)
77 pinctrl_dummy_state
= true;
80 const char *pinctrl_dev_get_name(struct pinctrl_dev
*pctldev
)
82 /* We're not allowed to register devices without name */
83 return pctldev
->desc
->name
;
85 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name
);
87 const char *pinctrl_dev_get_devname(struct pinctrl_dev
*pctldev
)
89 return dev_name(pctldev
->dev
);
91 EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname
);
93 void *pinctrl_dev_get_drvdata(struct pinctrl_dev
*pctldev
)
95 return pctldev
->driver_data
;
97 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata
);
100 * get_pinctrl_dev_from_devname() - look up pin controller device
101 * @devname: the name of a device instance, as returned by dev_name()
103 * Looks up a pin control device matching a certain device name or pure device
104 * pointer, the pure device pointer will take precedence.
106 struct pinctrl_dev
*get_pinctrl_dev_from_devname(const char *devname
)
108 struct pinctrl_dev
*pctldev
= NULL
;
114 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
115 if (!strcmp(dev_name(pctldev
->dev
), devname
)) {
116 /* Matched on device name */
122 return found
? pctldev
: NULL
;
126 * pin_get_from_name() - look up a pin number from a name
127 * @pctldev: the pin control device to lookup the pin on
128 * @name: the name of the pin to look up
130 int pin_get_from_name(struct pinctrl_dev
*pctldev
, const char *name
)
134 /* The pin number can be retrived from the pin controller descriptor */
135 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
136 struct pin_desc
*desc
;
138 pin
= pctldev
->desc
->pins
[i
].number
;
139 desc
= pin_desc_get(pctldev
, pin
);
140 /* Pin space may be sparse */
143 if (desc
->name
&& !strcmp(name
, desc
->name
))
151 * pin_get_name_from_id() - look up a pin name from a pin id
152 * @pctldev: the pin control device to lookup the pin on
153 * @name: the name of the pin to look up
155 const char *pin_get_name(struct pinctrl_dev
*pctldev
, const unsigned pin
)
157 const struct pin_desc
*desc
;
159 desc
= pin_desc_get(pctldev
, pin
);
161 dev_err(pctldev
->dev
, "failed to get pin(%d) name\n",
170 * pin_is_valid() - check if pin exists on controller
171 * @pctldev: the pin control device to check the pin on
172 * @pin: pin to check, use the local pin controller index number
174 * This tells us whether a certain pin exist on a certain pin controller or
175 * not. Pin lists may be sparse, so some pins may not exist.
177 bool pin_is_valid(struct pinctrl_dev
*pctldev
, int pin
)
179 struct pin_desc
*pindesc
;
184 mutex_lock(&pinctrl_mutex
);
185 pindesc
= pin_desc_get(pctldev
, pin
);
186 mutex_unlock(&pinctrl_mutex
);
188 return pindesc
!= NULL
;
190 EXPORT_SYMBOL_GPL(pin_is_valid
);
192 /* Deletes a range of pin descriptors */
193 static void pinctrl_free_pindescs(struct pinctrl_dev
*pctldev
,
194 const struct pinctrl_pin_desc
*pins
,
199 for (i
= 0; i
< num_pins
; i
++) {
200 struct pin_desc
*pindesc
;
202 pindesc
= radix_tree_lookup(&pctldev
->pin_desc_tree
,
204 if (pindesc
!= NULL
) {
205 radix_tree_delete(&pctldev
->pin_desc_tree
,
207 if (pindesc
->dynamic_name
)
208 kfree(pindesc
->name
);
214 static int pinctrl_register_one_pin(struct pinctrl_dev
*pctldev
,
215 unsigned number
, const char *name
)
217 struct pin_desc
*pindesc
;
219 pindesc
= pin_desc_get(pctldev
, number
);
220 if (pindesc
!= NULL
) {
221 pr_err("pin %d already registered on %s\n", number
,
222 pctldev
->desc
->name
);
226 pindesc
= kzalloc(sizeof(*pindesc
), GFP_KERNEL
);
227 if (pindesc
== NULL
) {
228 dev_err(pctldev
->dev
, "failed to alloc struct pin_desc\n");
233 pindesc
->pctldev
= pctldev
;
235 /* Copy basic pin info */
237 pindesc
->name
= name
;
239 pindesc
->name
= kasprintf(GFP_KERNEL
, "PIN%u", number
);
240 if (pindesc
->name
== NULL
) {
244 pindesc
->dynamic_name
= true;
247 radix_tree_insert(&pctldev
->pin_desc_tree
, number
, pindesc
);
248 pr_debug("registered pin %d (%s) on %s\n",
249 number
, pindesc
->name
, pctldev
->desc
->name
);
253 static int pinctrl_register_pins(struct pinctrl_dev
*pctldev
,
254 struct pinctrl_pin_desc
const *pins
,
260 for (i
= 0; i
< num_descs
; i
++) {
261 ret
= pinctrl_register_one_pin(pctldev
,
262 pins
[i
].number
, pins
[i
].name
);
271 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
272 * @pctldev: pin controller device to check
273 * @gpio: gpio pin to check taken from the global GPIO pin space
275 * Tries to match a GPIO pin number to the ranges handled by a certain pin
276 * controller, return the range or NULL
278 static struct pinctrl_gpio_range
*
279 pinctrl_match_gpio_range(struct pinctrl_dev
*pctldev
, unsigned gpio
)
281 struct pinctrl_gpio_range
*range
= NULL
;
283 /* Loop over the ranges */
284 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
285 /* Check if we're in the valid range */
286 if (gpio
>= range
->base
&&
287 gpio
< range
->base
+ range
->npins
) {
296 * pinctrl_get_device_gpio_range() - find device for GPIO range
297 * @gpio: the pin to locate the pin controller for
298 * @outdev: the pin control device if found
299 * @outrange: the GPIO range if found
301 * Find the pin controller handling a certain GPIO pin from the pinspace of
302 * the GPIO subsystem, return the device and the matching GPIO range. Returns
303 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
304 * may still have not been registered.
306 static int pinctrl_get_device_gpio_range(unsigned gpio
,
307 struct pinctrl_dev
**outdev
,
308 struct pinctrl_gpio_range
**outrange
)
310 struct pinctrl_dev
*pctldev
= NULL
;
312 /* Loop over the pin controllers */
313 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
314 struct pinctrl_gpio_range
*range
;
316 range
= pinctrl_match_gpio_range(pctldev
, gpio
);
324 return -EPROBE_DEFER
;
328 * pinctrl_add_gpio_range() - register a GPIO range for a controller
329 * @pctldev: pin controller device to add the range to
330 * @range: the GPIO range to add
332 * This adds a range of GPIOs to be handled by a certain pin controller. Call
333 * this to register handled ranges after registering your pin controller.
335 void pinctrl_add_gpio_range(struct pinctrl_dev
*pctldev
,
336 struct pinctrl_gpio_range
*range
)
338 mutex_lock(&pinctrl_mutex
);
339 list_add_tail(&range
->node
, &pctldev
->gpio_ranges
);
340 mutex_unlock(&pinctrl_mutex
);
342 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range
);
344 void pinctrl_add_gpio_ranges(struct pinctrl_dev
*pctldev
,
345 struct pinctrl_gpio_range
*ranges
,
350 for (i
= 0; i
< nranges
; i
++)
351 pinctrl_add_gpio_range(pctldev
, &ranges
[i
]);
353 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges
);
355 struct pinctrl_dev
*pinctrl_find_and_add_gpio_range(const char *devname
,
356 struct pinctrl_gpio_range
*range
)
358 struct pinctrl_dev
*pctldev
= get_pinctrl_dev_from_devname(devname
);
361 * If we can't find this device, let's assume that is because
362 * it has not probed yet, so the driver trying to register this
363 * range need to defer probing.
366 return ERR_PTR(-EPROBE_DEFER
);
368 pinctrl_add_gpio_range(pctldev
, range
);
371 EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range
);
374 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
375 * @pctldev: the pin controller device to look in
376 * @pin: a controller-local number to find the range for
378 struct pinctrl_gpio_range
*
379 pinctrl_find_gpio_range_from_pin(struct pinctrl_dev
*pctldev
,
382 struct pinctrl_gpio_range
*range
= NULL
;
384 /* Loop over the ranges */
385 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
386 /* Check if we're in the valid range */
387 if (pin
>= range
->pin_base
&&
388 pin
< range
->pin_base
+ range
->npins
) {
395 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin
);
398 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
399 * @pctldev: pin controller device to remove the range from
400 * @range: the GPIO range to remove
402 void pinctrl_remove_gpio_range(struct pinctrl_dev
*pctldev
,
403 struct pinctrl_gpio_range
*range
)
405 mutex_lock(&pinctrl_mutex
);
406 list_del(&range
->node
);
407 mutex_unlock(&pinctrl_mutex
);
409 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range
);
412 * pinctrl_get_group_selector() - returns the group selector for a group
413 * @pctldev: the pin controller handling the group
414 * @pin_group: the pin group to look up
416 int pinctrl_get_group_selector(struct pinctrl_dev
*pctldev
,
417 const char *pin_group
)
419 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
420 unsigned ngroups
= pctlops
->get_groups_count(pctldev
);
421 unsigned group_selector
= 0;
423 while (group_selector
< ngroups
) {
424 const char *gname
= pctlops
->get_group_name(pctldev
,
426 if (!strcmp(gname
, pin_group
)) {
427 dev_dbg(pctldev
->dev
,
428 "found group selector %u for %s\n",
431 return group_selector
;
437 dev_err(pctldev
->dev
, "does not have pin group %s\n",
444 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
445 * @gpio: the GPIO pin number from the GPIO subsystem number space
447 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
448 * as part of their gpio_request() semantics, platforms and individual drivers
449 * shall *NOT* request GPIO pins to be muxed in.
451 int pinctrl_request_gpio(unsigned gpio
)
453 struct pinctrl_dev
*pctldev
;
454 struct pinctrl_gpio_range
*range
;
458 mutex_lock(&pinctrl_mutex
);
460 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
462 mutex_unlock(&pinctrl_mutex
);
466 /* Convert to the pin controllers number space */
467 pin
= gpio
- range
->base
+ range
->pin_base
;
469 ret
= pinmux_request_gpio(pctldev
, range
, pin
, gpio
);
471 mutex_unlock(&pinctrl_mutex
);
474 EXPORT_SYMBOL_GPL(pinctrl_request_gpio
);
477 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
478 * @gpio: the GPIO pin number from the GPIO subsystem number space
480 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
481 * as part of their gpio_free() semantics, platforms and individual drivers
482 * shall *NOT* request GPIO pins to be muxed out.
484 void pinctrl_free_gpio(unsigned gpio
)
486 struct pinctrl_dev
*pctldev
;
487 struct pinctrl_gpio_range
*range
;
491 mutex_lock(&pinctrl_mutex
);
493 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
495 mutex_unlock(&pinctrl_mutex
);
499 /* Convert to the pin controllers number space */
500 pin
= gpio
- range
->base
+ range
->pin_base
;
502 pinmux_free_gpio(pctldev
, pin
, range
);
504 mutex_unlock(&pinctrl_mutex
);
506 EXPORT_SYMBOL_GPL(pinctrl_free_gpio
);
508 static int pinctrl_gpio_direction(unsigned gpio
, bool input
)
510 struct pinctrl_dev
*pctldev
;
511 struct pinctrl_gpio_range
*range
;
515 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
519 /* Convert to the pin controllers number space */
520 pin
= gpio
- range
->base
+ range
->pin_base
;
522 return pinmux_gpio_direction(pctldev
, range
, pin
, input
);
526 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
527 * @gpio: the GPIO pin number from the GPIO subsystem number space
529 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
530 * as part of their gpio_direction_input() semantics, platforms and individual
531 * drivers shall *NOT* touch pin control GPIO calls.
533 int pinctrl_gpio_direction_input(unsigned gpio
)
536 mutex_lock(&pinctrl_mutex
);
537 ret
= pinctrl_gpio_direction(gpio
, true);
538 mutex_unlock(&pinctrl_mutex
);
541 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input
);
544 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
545 * @gpio: the GPIO pin number from the GPIO subsystem number space
547 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
548 * as part of their gpio_direction_output() semantics, platforms and individual
549 * drivers shall *NOT* touch pin control GPIO calls.
551 int pinctrl_gpio_direction_output(unsigned gpio
)
554 mutex_lock(&pinctrl_mutex
);
555 ret
= pinctrl_gpio_direction(gpio
, false);
556 mutex_unlock(&pinctrl_mutex
);
559 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output
);
561 static struct pinctrl_state
*find_state(struct pinctrl
*p
,
564 struct pinctrl_state
*state
;
566 list_for_each_entry(state
, &p
->states
, node
)
567 if (!strcmp(state
->name
, name
))
573 static struct pinctrl_state
*create_state(struct pinctrl
*p
,
576 struct pinctrl_state
*state
;
578 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
581 "failed to alloc struct pinctrl_state\n");
582 return ERR_PTR(-ENOMEM
);
586 INIT_LIST_HEAD(&state
->settings
);
588 list_add_tail(&state
->node
, &p
->states
);
593 static int add_setting(struct pinctrl
*p
, struct pinctrl_map
const *map
)
595 struct pinctrl_state
*state
;
596 struct pinctrl_setting
*setting
;
599 state
= find_state(p
, map
->name
);
601 state
= create_state(p
, map
->name
);
603 return PTR_ERR(state
);
605 if (map
->type
== PIN_MAP_TYPE_DUMMY_STATE
)
608 setting
= kzalloc(sizeof(*setting
), GFP_KERNEL
);
609 if (setting
== NULL
) {
611 "failed to alloc struct pinctrl_setting\n");
615 setting
->type
= map
->type
;
617 setting
->pctldev
= get_pinctrl_dev_from_devname(map
->ctrl_dev_name
);
618 if (setting
->pctldev
== NULL
) {
620 /* Do not defer probing of hogs (circular loop) */
621 if (!strcmp(map
->ctrl_dev_name
, map
->dev_name
))
624 * OK let us guess that the driver is not there yet, and
625 * let's defer obtaining this pinctrl handle to later...
627 dev_info(p
->dev
, "unknown pinctrl device %s in map entry, deferring probe",
629 return -EPROBE_DEFER
;
632 setting
->dev_name
= map
->dev_name
;
635 case PIN_MAP_TYPE_MUX_GROUP
:
636 ret
= pinmux_map_to_setting(map
, setting
);
638 case PIN_MAP_TYPE_CONFIGS_PIN
:
639 case PIN_MAP_TYPE_CONFIGS_GROUP
:
640 ret
= pinconf_map_to_setting(map
, setting
);
651 list_add_tail(&setting
->node
, &state
->settings
);
656 static struct pinctrl
*find_pinctrl(struct device
*dev
)
660 list_for_each_entry(p
, &pinctrl_list
, node
)
667 static void pinctrl_put_locked(struct pinctrl
*p
, bool inlist
);
669 static struct pinctrl
*create_pinctrl(struct device
*dev
)
673 struct pinctrl_maps
*maps_node
;
675 struct pinctrl_map
const *map
;
679 * create the state cookie holder struct pinctrl for each
680 * mapping, this is what consumers will get when requesting
681 * a pin control handle with pinctrl_get()
683 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
685 dev_err(dev
, "failed to alloc struct pinctrl\n");
686 return ERR_PTR(-ENOMEM
);
689 INIT_LIST_HEAD(&p
->states
);
690 INIT_LIST_HEAD(&p
->dt_maps
);
692 ret
= pinctrl_dt_to_map(p
);
698 devname
= dev_name(dev
);
700 /* Iterate over the pin control maps to locate the right ones */
701 for_each_maps(maps_node
, i
, map
) {
702 /* Map must be for this device */
703 if (strcmp(map
->dev_name
, devname
))
706 ret
= add_setting(p
, map
);
708 * At this point the adding of a setting may:
710 * - Defer, if the pinctrl device is not yet available
711 * - Fail, if the pinctrl device is not yet available,
712 * AND the setting is a hog. We cannot defer that, since
713 * the hog will kick in immediately after the device
716 * If the error returned was not -EPROBE_DEFER then we
717 * accumulate the errors to see if we end up with
718 * an -EPROBE_DEFER later, as that is the worst case.
720 if (ret
== -EPROBE_DEFER
) {
721 pinctrl_put_locked(p
, false);
726 /* If some other error than deferral occured, return here */
727 pinctrl_put_locked(p
, false);
731 kref_init(&p
->users
);
733 /* Add the pinctrl handle to the global list */
734 list_add_tail(&p
->node
, &pinctrl_list
);
739 static struct pinctrl
*pinctrl_get_locked(struct device
*dev
)
744 return ERR_PTR(-EINVAL
);
747 * See if somebody else (such as the device core) has already
748 * obtained a handle to the pinctrl for this device. In that case,
749 * return another pointer to it.
751 p
= find_pinctrl(dev
);
753 dev_dbg(dev
, "obtain a copy of previously claimed pinctrl\n");
758 return create_pinctrl(dev
);
762 * pinctrl_get() - retrieves the pinctrl handle for a device
763 * @dev: the device to obtain the handle for
765 struct pinctrl
*pinctrl_get(struct device
*dev
)
769 mutex_lock(&pinctrl_mutex
);
770 p
= pinctrl_get_locked(dev
);
771 mutex_unlock(&pinctrl_mutex
);
775 EXPORT_SYMBOL_GPL(pinctrl_get
);
777 static void pinctrl_put_locked(struct pinctrl
*p
, bool inlist
)
779 struct pinctrl_state
*state
, *n1
;
780 struct pinctrl_setting
*setting
, *n2
;
782 list_for_each_entry_safe(state
, n1
, &p
->states
, node
) {
783 list_for_each_entry_safe(setting
, n2
, &state
->settings
, node
) {
784 switch (setting
->type
) {
785 case PIN_MAP_TYPE_MUX_GROUP
:
786 if (state
== p
->state
)
787 pinmux_disable_setting(setting
);
788 pinmux_free_setting(setting
);
790 case PIN_MAP_TYPE_CONFIGS_PIN
:
791 case PIN_MAP_TYPE_CONFIGS_GROUP
:
792 pinconf_free_setting(setting
);
797 list_del(&setting
->node
);
800 list_del(&state
->node
);
804 pinctrl_dt_free_maps(p
);
812 * pinctrl_release() - release the pinctrl handle
813 * @kref: the kref in the pinctrl being released
815 void pinctrl_release(struct kref
*kref
)
817 struct pinctrl
*p
= container_of(kref
, struct pinctrl
, users
);
819 pinctrl_put_locked(p
, true);
823 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
824 * @p: the pinctrl handle to release
826 void pinctrl_put(struct pinctrl
*p
)
828 mutex_lock(&pinctrl_mutex
);
829 kref_put(&p
->users
, pinctrl_release
);
830 mutex_unlock(&pinctrl_mutex
);
832 EXPORT_SYMBOL_GPL(pinctrl_put
);
834 static struct pinctrl_state
*pinctrl_lookup_state_locked(struct pinctrl
*p
,
837 struct pinctrl_state
*state
;
839 state
= find_state(p
, name
);
841 if (pinctrl_dummy_state
) {
842 /* create dummy state */
843 dev_dbg(p
->dev
, "using pinctrl dummy state (%s)\n",
845 state
= create_state(p
, name
);
847 state
= ERR_PTR(-ENODEV
);
854 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
855 * @p: the pinctrl handle to retrieve the state from
856 * @name: the state name to retrieve
858 struct pinctrl_state
*pinctrl_lookup_state(struct pinctrl
*p
, const char *name
)
860 struct pinctrl_state
*s
;
862 mutex_lock(&pinctrl_mutex
);
863 s
= pinctrl_lookup_state_locked(p
, name
);
864 mutex_unlock(&pinctrl_mutex
);
868 EXPORT_SYMBOL_GPL(pinctrl_lookup_state
);
870 static int pinctrl_select_state_locked(struct pinctrl
*p
,
871 struct pinctrl_state
*state
)
873 struct pinctrl_setting
*setting
, *setting2
;
876 if (p
->state
== state
)
881 * The set of groups with a mux configuration in the old state
882 * may not be identical to the set of groups with a mux setting
883 * in the new state. While this might be unusual, it's entirely
884 * possible for the "user"-supplied mapping table to be written
885 * that way. For each group that was configured in the old state
886 * but not in the new state, this code puts that group into a
887 * safe/disabled state.
889 list_for_each_entry(setting
, &p
->state
->settings
, node
) {
891 if (setting
->type
!= PIN_MAP_TYPE_MUX_GROUP
)
893 list_for_each_entry(setting2
, &state
->settings
, node
) {
894 if (setting2
->type
!= PIN_MAP_TYPE_MUX_GROUP
)
896 if (setting2
->data
.mux
.group
==
897 setting
->data
.mux
.group
) {
903 pinmux_disable_setting(setting
);
909 /* Apply all the settings for the new state */
910 list_for_each_entry(setting
, &state
->settings
, node
) {
911 switch (setting
->type
) {
912 case PIN_MAP_TYPE_MUX_GROUP
:
913 ret
= pinmux_enable_setting(setting
);
915 case PIN_MAP_TYPE_CONFIGS_PIN
:
916 case PIN_MAP_TYPE_CONFIGS_GROUP
:
917 ret
= pinconf_apply_setting(setting
);
924 /* FIXME: Difficult to return to prev state */
933 * pinctrl_select() - select/activate/program a pinctrl state to HW
934 * @p: the pinctrl handle for the device that requests configuratio
935 * @state: the state handle to select/activate/program
937 int pinctrl_select_state(struct pinctrl
*p
, struct pinctrl_state
*state
)
941 mutex_lock(&pinctrl_mutex
);
942 ret
= pinctrl_select_state_locked(p
, state
);
943 mutex_unlock(&pinctrl_mutex
);
947 EXPORT_SYMBOL_GPL(pinctrl_select_state
);
949 static void devm_pinctrl_release(struct device
*dev
, void *res
)
951 pinctrl_put(*(struct pinctrl
**)res
);
955 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
956 * @dev: the device to obtain the handle for
958 * If there is a need to explicitly destroy the returned struct pinctrl,
959 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
961 struct pinctrl
*devm_pinctrl_get(struct device
*dev
)
963 struct pinctrl
**ptr
, *p
;
965 ptr
= devres_alloc(devm_pinctrl_release
, sizeof(*ptr
), GFP_KERNEL
);
967 return ERR_PTR(-ENOMEM
);
969 p
= pinctrl_get(dev
);
972 devres_add(dev
, ptr
);
979 EXPORT_SYMBOL_GPL(devm_pinctrl_get
);
981 static int devm_pinctrl_match(struct device
*dev
, void *res
, void *data
)
983 struct pinctrl
**p
= res
;
989 * devm_pinctrl_put() - Resource managed pinctrl_put()
990 * @p: the pinctrl handle to release
992 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
993 * this function will not need to be called and the resource management
994 * code will ensure that the resource is freed.
996 void devm_pinctrl_put(struct pinctrl
*p
)
998 WARN_ON(devres_destroy(p
->dev
, devm_pinctrl_release
,
999 devm_pinctrl_match
, p
));
1002 EXPORT_SYMBOL_GPL(devm_pinctrl_put
);
1004 int pinctrl_register_map(struct pinctrl_map
const *maps
, unsigned num_maps
,
1005 bool dup
, bool locked
)
1008 struct pinctrl_maps
*maps_node
;
1010 pr_debug("add %d pinmux maps\n", num_maps
);
1012 /* First sanity check the new mapping */
1013 for (i
= 0; i
< num_maps
; i
++) {
1014 if (!maps
[i
].dev_name
) {
1015 pr_err("failed to register map %s (%d): no device given\n",
1020 if (!maps
[i
].name
) {
1021 pr_err("failed to register map %d: no map name given\n",
1026 if (maps
[i
].type
!= PIN_MAP_TYPE_DUMMY_STATE
&&
1027 !maps
[i
].ctrl_dev_name
) {
1028 pr_err("failed to register map %s (%d): no pin control device given\n",
1033 switch (maps
[i
].type
) {
1034 case PIN_MAP_TYPE_DUMMY_STATE
:
1036 case PIN_MAP_TYPE_MUX_GROUP
:
1037 ret
= pinmux_validate_map(&maps
[i
], i
);
1041 case PIN_MAP_TYPE_CONFIGS_PIN
:
1042 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1043 ret
= pinconf_validate_map(&maps
[i
], i
);
1048 pr_err("failed to register map %s (%d): invalid type given\n",
1054 maps_node
= kzalloc(sizeof(*maps_node
), GFP_KERNEL
);
1056 pr_err("failed to alloc struct pinctrl_maps\n");
1060 maps_node
->num_maps
= num_maps
;
1062 maps_node
->maps
= kmemdup(maps
, sizeof(*maps
) * num_maps
,
1064 if (!maps_node
->maps
) {
1065 pr_err("failed to duplicate mapping table\n");
1070 maps_node
->maps
= maps
;
1074 mutex_lock(&pinctrl_mutex
);
1075 list_add_tail(&maps_node
->node
, &pinctrl_maps
);
1077 mutex_unlock(&pinctrl_mutex
);
1083 * pinctrl_register_mappings() - register a set of pin controller mappings
1084 * @maps: the pincontrol mappings table to register. This should probably be
1085 * marked with __initdata so it can be discarded after boot. This
1086 * function will perform a shallow copy for the mapping entries.
1087 * @num_maps: the number of maps in the mapping table
1089 int pinctrl_register_mappings(struct pinctrl_map
const *maps
,
1092 return pinctrl_register_map(maps
, num_maps
, true, false);
1095 void pinctrl_unregister_map(struct pinctrl_map
const *map
)
1097 struct pinctrl_maps
*maps_node
;
1099 list_for_each_entry(maps_node
, &pinctrl_maps
, node
) {
1100 if (maps_node
->maps
== map
) {
1101 list_del(&maps_node
->node
);
1108 * pinctrl_force_sleep() - turn a given controller device into sleep state
1109 * @pctldev: pin controller device
1111 int pinctrl_force_sleep(struct pinctrl_dev
*pctldev
)
1113 if (!IS_ERR(pctldev
->p
) && !IS_ERR(pctldev
->hog_sleep
))
1114 return pinctrl_select_state(pctldev
->p
, pctldev
->hog_sleep
);
1117 EXPORT_SYMBOL_GPL(pinctrl_force_sleep
);
1120 * pinctrl_force_default() - turn a given controller device into default state
1121 * @pctldev: pin controller device
1123 int pinctrl_force_default(struct pinctrl_dev
*pctldev
)
1125 if (!IS_ERR(pctldev
->p
) && !IS_ERR(pctldev
->hog_default
))
1126 return pinctrl_select_state(pctldev
->p
, pctldev
->hog_default
);
1129 EXPORT_SYMBOL_GPL(pinctrl_force_default
);
1131 #ifdef CONFIG_DEBUG_FS
1133 static int pinctrl_pins_show(struct seq_file
*s
, void *what
)
1135 struct pinctrl_dev
*pctldev
= s
->private;
1136 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1139 seq_printf(s
, "registered pins: %d\n", pctldev
->desc
->npins
);
1141 mutex_lock(&pinctrl_mutex
);
1143 /* The pin number can be retrived from the pin controller descriptor */
1144 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
1145 struct pin_desc
*desc
;
1147 pin
= pctldev
->desc
->pins
[i
].number
;
1148 desc
= pin_desc_get(pctldev
, pin
);
1149 /* Pin space may be sparse */
1153 seq_printf(s
, "pin %d (%s) ", pin
,
1154 desc
->name
? desc
->name
: "unnamed");
1156 /* Driver-specific info per pin */
1157 if (ops
->pin_dbg_show
)
1158 ops
->pin_dbg_show(pctldev
, s
, pin
);
1163 mutex_unlock(&pinctrl_mutex
);
1168 static int pinctrl_groups_show(struct seq_file
*s
, void *what
)
1170 struct pinctrl_dev
*pctldev
= s
->private;
1171 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1172 unsigned ngroups
, selector
= 0;
1174 ngroups
= ops
->get_groups_count(pctldev
);
1175 mutex_lock(&pinctrl_mutex
);
1177 seq_puts(s
, "registered pin groups:\n");
1178 while (selector
< ngroups
) {
1179 const unsigned *pins
;
1181 const char *gname
= ops
->get_group_name(pctldev
, selector
);
1186 ret
= ops
->get_group_pins(pctldev
, selector
,
1189 seq_printf(s
, "%s [ERROR GETTING PINS]\n",
1192 seq_printf(s
, "group: %s\n", gname
);
1193 for (i
= 0; i
< num_pins
; i
++) {
1194 pname
= pin_get_name(pctldev
, pins
[i
]);
1195 if (WARN_ON(!pname
)) {
1196 mutex_unlock(&pinctrl_mutex
);
1199 seq_printf(s
, "pin %d (%s)\n", pins
[i
], pname
);
1206 mutex_unlock(&pinctrl_mutex
);
1211 static int pinctrl_gpioranges_show(struct seq_file
*s
, void *what
)
1213 struct pinctrl_dev
*pctldev
= s
->private;
1214 struct pinctrl_gpio_range
*range
= NULL
;
1216 seq_puts(s
, "GPIO ranges handled:\n");
1218 mutex_lock(&pinctrl_mutex
);
1220 /* Loop over the ranges */
1221 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
1222 seq_printf(s
, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1223 range
->id
, range
->name
,
1224 range
->base
, (range
->base
+ range
->npins
- 1),
1226 (range
->pin_base
+ range
->npins
- 1));
1229 mutex_unlock(&pinctrl_mutex
);
1234 static int pinctrl_devices_show(struct seq_file
*s
, void *what
)
1236 struct pinctrl_dev
*pctldev
;
1238 seq_puts(s
, "name [pinmux] [pinconf]\n");
1240 mutex_lock(&pinctrl_mutex
);
1242 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
1243 seq_printf(s
, "%s ", pctldev
->desc
->name
);
1244 if (pctldev
->desc
->pmxops
)
1245 seq_puts(s
, "yes ");
1248 if (pctldev
->desc
->confops
)
1255 mutex_unlock(&pinctrl_mutex
);
1260 static inline const char *map_type(enum pinctrl_map_type type
)
1262 static const char * const names
[] = {
1270 if (type
>= ARRAY_SIZE(names
))
1276 static int pinctrl_maps_show(struct seq_file
*s
, void *what
)
1278 struct pinctrl_maps
*maps_node
;
1280 struct pinctrl_map
const *map
;
1282 seq_puts(s
, "Pinctrl maps:\n");
1284 mutex_lock(&pinctrl_mutex
);
1286 for_each_maps(maps_node
, i
, map
) {
1287 seq_printf(s
, "device %s\nstate %s\ntype %s (%d)\n",
1288 map
->dev_name
, map
->name
, map_type(map
->type
),
1291 if (map
->type
!= PIN_MAP_TYPE_DUMMY_STATE
)
1292 seq_printf(s
, "controlling device %s\n",
1293 map
->ctrl_dev_name
);
1295 switch (map
->type
) {
1296 case PIN_MAP_TYPE_MUX_GROUP
:
1297 pinmux_show_map(s
, map
);
1299 case PIN_MAP_TYPE_CONFIGS_PIN
:
1300 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1301 pinconf_show_map(s
, map
);
1307 seq_printf(s
, "\n");
1310 mutex_unlock(&pinctrl_mutex
);
1315 static int pinctrl_show(struct seq_file
*s
, void *what
)
1318 struct pinctrl_state
*state
;
1319 struct pinctrl_setting
*setting
;
1321 seq_puts(s
, "Requested pin control handlers their pinmux maps:\n");
1323 mutex_lock(&pinctrl_mutex
);
1325 list_for_each_entry(p
, &pinctrl_list
, node
) {
1326 seq_printf(s
, "device: %s current state: %s\n",
1328 p
->state
? p
->state
->name
: "none");
1330 list_for_each_entry(state
, &p
->states
, node
) {
1331 seq_printf(s
, " state: %s\n", state
->name
);
1333 list_for_each_entry(setting
, &state
->settings
, node
) {
1334 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
1336 seq_printf(s
, " type: %s controller %s ",
1337 map_type(setting
->type
),
1338 pinctrl_dev_get_name(pctldev
));
1340 switch (setting
->type
) {
1341 case PIN_MAP_TYPE_MUX_GROUP
:
1342 pinmux_show_setting(s
, setting
);
1344 case PIN_MAP_TYPE_CONFIGS_PIN
:
1345 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1346 pinconf_show_setting(s
, setting
);
1355 mutex_unlock(&pinctrl_mutex
);
1360 static int pinctrl_pins_open(struct inode
*inode
, struct file
*file
)
1362 return single_open(file
, pinctrl_pins_show
, inode
->i_private
);
1365 static int pinctrl_groups_open(struct inode
*inode
, struct file
*file
)
1367 return single_open(file
, pinctrl_groups_show
, inode
->i_private
);
1370 static int pinctrl_gpioranges_open(struct inode
*inode
, struct file
*file
)
1372 return single_open(file
, pinctrl_gpioranges_show
, inode
->i_private
);
1375 static int pinctrl_devices_open(struct inode
*inode
, struct file
*file
)
1377 return single_open(file
, pinctrl_devices_show
, NULL
);
1380 static int pinctrl_maps_open(struct inode
*inode
, struct file
*file
)
1382 return single_open(file
, pinctrl_maps_show
, NULL
);
1385 static int pinctrl_open(struct inode
*inode
, struct file
*file
)
1387 return single_open(file
, pinctrl_show
, NULL
);
1390 static const struct file_operations pinctrl_pins_ops
= {
1391 .open
= pinctrl_pins_open
,
1393 .llseek
= seq_lseek
,
1394 .release
= single_release
,
1397 static const struct file_operations pinctrl_groups_ops
= {
1398 .open
= pinctrl_groups_open
,
1400 .llseek
= seq_lseek
,
1401 .release
= single_release
,
1404 static const struct file_operations pinctrl_gpioranges_ops
= {
1405 .open
= pinctrl_gpioranges_open
,
1407 .llseek
= seq_lseek
,
1408 .release
= single_release
,
1411 static const struct file_operations pinctrl_devices_ops
= {
1412 .open
= pinctrl_devices_open
,
1414 .llseek
= seq_lseek
,
1415 .release
= single_release
,
1418 static const struct file_operations pinctrl_maps_ops
= {
1419 .open
= pinctrl_maps_open
,
1421 .llseek
= seq_lseek
,
1422 .release
= single_release
,
1425 static const struct file_operations pinctrl_ops
= {
1426 .open
= pinctrl_open
,
1428 .llseek
= seq_lseek
,
1429 .release
= single_release
,
1432 static struct dentry
*debugfs_root
;
1434 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1436 struct dentry
*device_root
;
1438 device_root
= debugfs_create_dir(dev_name(pctldev
->dev
),
1440 pctldev
->device_root
= device_root
;
1442 if (IS_ERR(device_root
) || !device_root
) {
1443 pr_warn("failed to create debugfs directory for %s\n",
1444 dev_name(pctldev
->dev
));
1447 debugfs_create_file("pins", S_IFREG
| S_IRUGO
,
1448 device_root
, pctldev
, &pinctrl_pins_ops
);
1449 debugfs_create_file("pingroups", S_IFREG
| S_IRUGO
,
1450 device_root
, pctldev
, &pinctrl_groups_ops
);
1451 debugfs_create_file("gpio-ranges", S_IFREG
| S_IRUGO
,
1452 device_root
, pctldev
, &pinctrl_gpioranges_ops
);
1453 pinmux_init_device_debugfs(device_root
, pctldev
);
1454 pinconf_init_device_debugfs(device_root
, pctldev
);
1457 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1459 debugfs_remove_recursive(pctldev
->device_root
);
1462 static void pinctrl_init_debugfs(void)
1464 debugfs_root
= debugfs_create_dir("pinctrl", NULL
);
1465 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
1466 pr_warn("failed to create debugfs directory\n");
1467 debugfs_root
= NULL
;
1471 debugfs_create_file("pinctrl-devices", S_IFREG
| S_IRUGO
,
1472 debugfs_root
, NULL
, &pinctrl_devices_ops
);
1473 debugfs_create_file("pinctrl-maps", S_IFREG
| S_IRUGO
,
1474 debugfs_root
, NULL
, &pinctrl_maps_ops
);
1475 debugfs_create_file("pinctrl-handles", S_IFREG
| S_IRUGO
,
1476 debugfs_root
, NULL
, &pinctrl_ops
);
1479 #else /* CONFIG_DEBUG_FS */
1481 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1485 static void pinctrl_init_debugfs(void)
1489 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1495 static int pinctrl_check_ops(struct pinctrl_dev
*pctldev
)
1497 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1500 !ops
->get_groups_count
||
1501 !ops
->get_group_name
||
1502 !ops
->get_group_pins
)
1505 if (ops
->dt_node_to_map
&& !ops
->dt_free_map
)
1512 * pinctrl_register() - register a pin controller device
1513 * @pctldesc: descriptor for this pin controller
1514 * @dev: parent device for this pin controller
1515 * @driver_data: private pin controller data for this pin controller
1517 struct pinctrl_dev
*pinctrl_register(struct pinctrl_desc
*pctldesc
,
1518 struct device
*dev
, void *driver_data
)
1520 struct pinctrl_dev
*pctldev
;
1525 if (!pctldesc
->name
)
1528 pctldev
= kzalloc(sizeof(*pctldev
), GFP_KERNEL
);
1529 if (pctldev
== NULL
) {
1530 dev_err(dev
, "failed to alloc struct pinctrl_dev\n");
1534 /* Initialize pin control device struct */
1535 pctldev
->owner
= pctldesc
->owner
;
1536 pctldev
->desc
= pctldesc
;
1537 pctldev
->driver_data
= driver_data
;
1538 INIT_RADIX_TREE(&pctldev
->pin_desc_tree
, GFP_KERNEL
);
1539 INIT_LIST_HEAD(&pctldev
->gpio_ranges
);
1542 /* check core ops for sanity */
1543 if (pinctrl_check_ops(pctldev
)) {
1544 dev_err(dev
, "pinctrl ops lacks necessary functions\n");
1548 /* If we're implementing pinmuxing, check the ops for sanity */
1549 if (pctldesc
->pmxops
) {
1550 if (pinmux_check_ops(pctldev
))
1554 /* If we're implementing pinconfig, check the ops for sanity */
1555 if (pctldesc
->confops
) {
1556 if (pinconf_check_ops(pctldev
))
1560 /* Register all the pins */
1561 dev_dbg(dev
, "try to register %d pins ...\n", pctldesc
->npins
);
1562 ret
= pinctrl_register_pins(pctldev
, pctldesc
->pins
, pctldesc
->npins
);
1564 dev_err(dev
, "error during pin registration\n");
1565 pinctrl_free_pindescs(pctldev
, pctldesc
->pins
,
1570 mutex_lock(&pinctrl_mutex
);
1572 list_add_tail(&pctldev
->node
, &pinctrldev_list
);
1574 pctldev
->p
= pinctrl_get_locked(pctldev
->dev
);
1575 if (!IS_ERR(pctldev
->p
)) {
1576 pctldev
->hog_default
=
1577 pinctrl_lookup_state_locked(pctldev
->p
,
1578 PINCTRL_STATE_DEFAULT
);
1579 if (IS_ERR(pctldev
->hog_default
)) {
1580 dev_dbg(dev
, "failed to lookup the default state\n");
1582 if (pinctrl_select_state_locked(pctldev
->p
,
1583 pctldev
->hog_default
))
1585 "failed to select default state\n");
1588 pctldev
->hog_sleep
=
1589 pinctrl_lookup_state_locked(pctldev
->p
,
1590 PINCTRL_STATE_SLEEP
);
1591 if (IS_ERR(pctldev
->hog_sleep
))
1592 dev_dbg(dev
, "failed to lookup the sleep state\n");
1595 mutex_unlock(&pinctrl_mutex
);
1597 pinctrl_init_device_debugfs(pctldev
);
1605 EXPORT_SYMBOL_GPL(pinctrl_register
);
1608 * pinctrl_unregister() - unregister pinmux
1609 * @pctldev: pin controller to unregister
1611 * Called by pinmux drivers to unregister a pinmux.
1613 void pinctrl_unregister(struct pinctrl_dev
*pctldev
)
1615 struct pinctrl_gpio_range
*range
, *n
;
1616 if (pctldev
== NULL
)
1619 pinctrl_remove_device_debugfs(pctldev
);
1621 mutex_lock(&pinctrl_mutex
);
1623 if (!IS_ERR(pctldev
->p
))
1624 pinctrl_put_locked(pctldev
->p
, true);
1626 /* TODO: check that no pinmuxes are still active? */
1627 list_del(&pctldev
->node
);
1628 /* Destroy descriptor tree */
1629 pinctrl_free_pindescs(pctldev
, pctldev
->desc
->pins
,
1630 pctldev
->desc
->npins
);
1631 /* remove gpio ranges map */
1632 list_for_each_entry_safe(range
, n
, &pctldev
->gpio_ranges
, node
)
1633 list_del(&range
->node
);
1637 mutex_unlock(&pinctrl_mutex
);
1639 EXPORT_SYMBOL_GPL(pinctrl_unregister
);
1641 static int __init
pinctrl_init(void)
1643 pr_info("initialized pinctrl subsystem\n");
1644 pinctrl_init_debugfs();
1648 /* init early since many drivers really need to initialized pinmux early */
1649 core_initcall(pinctrl_init
);