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/export.h>
18 #include <linux/init.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/err.h>
22 #include <linux/list.h>
23 #include <linux/sysfs.h>
24 #include <linux/debugfs.h>
25 #include <linux/seq_file.h>
26 #include <linux/pinctrl/consumer.h>
27 #include <linux/pinctrl/pinctrl.h>
28 #include <linux/pinctrl/machine.h>
30 #include "devicetree.h"
35 * struct pinctrl_maps - a list item containing part of the mapping table
36 * @node: mapping table list node
37 * @maps: array of mapping table entries
38 * @num_maps: the number of entries in @maps
41 struct list_head node
;
42 struct pinctrl_map
const *maps
;
46 /* Mutex taken by all entry points */
47 DEFINE_MUTEX(pinctrl_mutex
);
49 /* Global list of pin control devices (struct pinctrl_dev) */
50 LIST_HEAD(pinctrldev_list
);
52 /* List of pin controller handles (struct pinctrl) */
53 static LIST_HEAD(pinctrl_list
);
55 /* List of pinctrl maps (struct pinctrl_maps) */
56 static LIST_HEAD(pinctrl_maps
);
58 #define for_each_maps(_maps_node_, _i_, _map_) \
59 list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
60 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
61 _i_ < _maps_node_->num_maps; \
62 i++, _map_ = &_maps_node_->maps[_i_])
64 const char *pinctrl_dev_get_name(struct pinctrl_dev
*pctldev
)
66 /* We're not allowed to register devices without name */
67 return pctldev
->desc
->name
;
69 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name
);
71 void *pinctrl_dev_get_drvdata(struct pinctrl_dev
*pctldev
)
73 return pctldev
->driver_data
;
75 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata
);
78 * get_pinctrl_dev_from_devname() - look up pin controller device
79 * @devname: the name of a device instance, as returned by dev_name()
81 * Looks up a pin control device matching a certain device name or pure device
82 * pointer, the pure device pointer will take precedence.
84 struct pinctrl_dev
*get_pinctrl_dev_from_devname(const char *devname
)
86 struct pinctrl_dev
*pctldev
= NULL
;
92 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
93 if (!strcmp(dev_name(pctldev
->dev
), devname
)) {
94 /* Matched on device name */
100 return found
? pctldev
: NULL
;
104 * pin_get_from_name() - look up a pin number from a name
105 * @pctldev: the pin control device to lookup the pin on
106 * @name: the name of the pin to look up
108 int pin_get_from_name(struct pinctrl_dev
*pctldev
, const char *name
)
112 /* The pin number can be retrived from the pin controller descriptor */
113 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
114 struct pin_desc
*desc
;
116 pin
= pctldev
->desc
->pins
[i
].number
;
117 desc
= pin_desc_get(pctldev
, pin
);
118 /* Pin space may be sparse */
121 if (desc
->name
&& !strcmp(name
, desc
->name
))
129 * pin_is_valid() - check if pin exists on controller
130 * @pctldev: the pin control device to check the pin on
131 * @pin: pin to check, use the local pin controller index number
133 * This tells us whether a certain pin exist on a certain pin controller or
134 * not. Pin lists may be sparse, so some pins may not exist.
136 bool pin_is_valid(struct pinctrl_dev
*pctldev
, int pin
)
138 struct pin_desc
*pindesc
;
143 mutex_lock(&pinctrl_mutex
);
144 pindesc
= pin_desc_get(pctldev
, pin
);
145 mutex_unlock(&pinctrl_mutex
);
147 return pindesc
!= NULL
;
149 EXPORT_SYMBOL_GPL(pin_is_valid
);
151 /* Deletes a range of pin descriptors */
152 static void pinctrl_free_pindescs(struct pinctrl_dev
*pctldev
,
153 const struct pinctrl_pin_desc
*pins
,
158 for (i
= 0; i
< num_pins
; i
++) {
159 struct pin_desc
*pindesc
;
161 pindesc
= radix_tree_lookup(&pctldev
->pin_desc_tree
,
163 if (pindesc
!= NULL
) {
164 radix_tree_delete(&pctldev
->pin_desc_tree
,
166 if (pindesc
->dynamic_name
)
167 kfree(pindesc
->name
);
173 static int pinctrl_register_one_pin(struct pinctrl_dev
*pctldev
,
174 unsigned number
, const char *name
)
176 struct pin_desc
*pindesc
;
178 pindesc
= pin_desc_get(pctldev
, number
);
179 if (pindesc
!= NULL
) {
180 pr_err("pin %d already registered on %s\n", number
,
181 pctldev
->desc
->name
);
185 pindesc
= kzalloc(sizeof(*pindesc
), GFP_KERNEL
);
186 if (pindesc
== NULL
) {
187 dev_err(pctldev
->dev
, "failed to alloc struct pin_desc\n");
192 pindesc
->pctldev
= pctldev
;
194 /* Copy basic pin info */
196 pindesc
->name
= name
;
198 pindesc
->name
= kasprintf(GFP_KERNEL
, "PIN%u", number
);
199 if (pindesc
->name
== NULL
)
201 pindesc
->dynamic_name
= true;
204 radix_tree_insert(&pctldev
->pin_desc_tree
, number
, pindesc
);
205 pr_debug("registered pin %d (%s) on %s\n",
206 number
, pindesc
->name
, pctldev
->desc
->name
);
210 static int pinctrl_register_pins(struct pinctrl_dev
*pctldev
,
211 struct pinctrl_pin_desc
const *pins
,
217 for (i
= 0; i
< num_descs
; i
++) {
218 ret
= pinctrl_register_one_pin(pctldev
,
219 pins
[i
].number
, pins
[i
].name
);
228 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
229 * @pctldev: pin controller device to check
230 * @gpio: gpio pin to check taken from the global GPIO pin space
232 * Tries to match a GPIO pin number to the ranges handled by a certain pin
233 * controller, return the range or NULL
235 static struct pinctrl_gpio_range
*
236 pinctrl_match_gpio_range(struct pinctrl_dev
*pctldev
, unsigned gpio
)
238 struct pinctrl_gpio_range
*range
= NULL
;
240 /* Loop over the ranges */
241 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
242 /* Check if we're in the valid range */
243 if (gpio
>= range
->base
&&
244 gpio
< range
->base
+ range
->npins
) {
253 * pinctrl_get_device_gpio_range() - find device for GPIO range
254 * @gpio: the pin to locate the pin controller for
255 * @outdev: the pin control device if found
256 * @outrange: the GPIO range if found
258 * Find the pin controller handling a certain GPIO pin from the pinspace of
259 * the GPIO subsystem, return the device and the matching GPIO range. Returns
260 * negative if the GPIO range could not be found in any device.
262 static int pinctrl_get_device_gpio_range(unsigned gpio
,
263 struct pinctrl_dev
**outdev
,
264 struct pinctrl_gpio_range
**outrange
)
266 struct pinctrl_dev
*pctldev
= NULL
;
268 /* Loop over the pin controllers */
269 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
270 struct pinctrl_gpio_range
*range
;
272 range
= pinctrl_match_gpio_range(pctldev
, gpio
);
284 * pinctrl_add_gpio_range() - register a GPIO range for a controller
285 * @pctldev: pin controller device to add the range to
286 * @range: the GPIO range to add
288 * This adds a range of GPIOs to be handled by a certain pin controller. Call
289 * this to register handled ranges after registering your pin controller.
291 void pinctrl_add_gpio_range(struct pinctrl_dev
*pctldev
,
292 struct pinctrl_gpio_range
*range
)
294 mutex_lock(&pinctrl_mutex
);
295 list_add_tail(&range
->node
, &pctldev
->gpio_ranges
);
296 mutex_unlock(&pinctrl_mutex
);
298 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range
);
301 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
302 * @pctldev: pin controller device to remove the range from
303 * @range: the GPIO range to remove
305 void pinctrl_remove_gpio_range(struct pinctrl_dev
*pctldev
,
306 struct pinctrl_gpio_range
*range
)
308 mutex_lock(&pinctrl_mutex
);
309 list_del(&range
->node
);
310 mutex_unlock(&pinctrl_mutex
);
312 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range
);
315 * pinctrl_get_group_selector() - returns the group selector for a group
316 * @pctldev: the pin controller handling the group
317 * @pin_group: the pin group to look up
319 int pinctrl_get_group_selector(struct pinctrl_dev
*pctldev
,
320 const char *pin_group
)
322 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
323 unsigned ngroups
= pctlops
->get_groups_count(pctldev
);
324 unsigned group_selector
= 0;
326 while (group_selector
< ngroups
) {
327 const char *gname
= pctlops
->get_group_name(pctldev
,
329 if (!strcmp(gname
, pin_group
)) {
330 dev_dbg(pctldev
->dev
,
331 "found group selector %u for %s\n",
334 return group_selector
;
340 dev_err(pctldev
->dev
, "does not have pin group %s\n",
347 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
348 * @gpio: the GPIO pin number from the GPIO subsystem number space
350 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
351 * as part of their gpio_request() semantics, platforms and individual drivers
352 * shall *NOT* request GPIO pins to be muxed in.
354 int pinctrl_request_gpio(unsigned gpio
)
356 struct pinctrl_dev
*pctldev
;
357 struct pinctrl_gpio_range
*range
;
361 mutex_lock(&pinctrl_mutex
);
363 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
365 mutex_unlock(&pinctrl_mutex
);
369 /* Convert to the pin controllers number space */
370 pin
= gpio
- range
->base
+ range
->pin_base
;
372 ret
= pinmux_request_gpio(pctldev
, range
, pin
, gpio
);
374 mutex_unlock(&pinctrl_mutex
);
377 EXPORT_SYMBOL_GPL(pinctrl_request_gpio
);
380 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
381 * @gpio: the GPIO pin number from the GPIO subsystem number space
383 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
384 * as part of their gpio_free() semantics, platforms and individual drivers
385 * shall *NOT* request GPIO pins to be muxed out.
387 void pinctrl_free_gpio(unsigned gpio
)
389 struct pinctrl_dev
*pctldev
;
390 struct pinctrl_gpio_range
*range
;
394 mutex_lock(&pinctrl_mutex
);
396 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
398 mutex_unlock(&pinctrl_mutex
);
402 /* Convert to the pin controllers number space */
403 pin
= gpio
- range
->base
+ range
->pin_base
;
405 pinmux_free_gpio(pctldev
, pin
, range
);
407 mutex_unlock(&pinctrl_mutex
);
409 EXPORT_SYMBOL_GPL(pinctrl_free_gpio
);
411 static int pinctrl_gpio_direction(unsigned gpio
, bool input
)
413 struct pinctrl_dev
*pctldev
;
414 struct pinctrl_gpio_range
*range
;
418 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
422 /* Convert to the pin controllers number space */
423 pin
= gpio
- range
->base
+ range
->pin_base
;
425 return pinmux_gpio_direction(pctldev
, range
, pin
, input
);
429 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
430 * @gpio: the GPIO pin number from the GPIO subsystem number space
432 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
433 * as part of their gpio_direction_input() semantics, platforms and individual
434 * drivers shall *NOT* touch pin control GPIO calls.
436 int pinctrl_gpio_direction_input(unsigned gpio
)
439 mutex_lock(&pinctrl_mutex
);
440 ret
= pinctrl_gpio_direction(gpio
, true);
441 mutex_unlock(&pinctrl_mutex
);
444 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input
);
447 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
448 * @gpio: the GPIO pin number from the GPIO subsystem number space
450 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
451 * as part of their gpio_direction_output() semantics, platforms and individual
452 * drivers shall *NOT* touch pin control GPIO calls.
454 int pinctrl_gpio_direction_output(unsigned gpio
)
457 mutex_lock(&pinctrl_mutex
);
458 ret
= pinctrl_gpio_direction(gpio
, false);
459 mutex_unlock(&pinctrl_mutex
);
462 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output
);
464 static struct pinctrl_state
*find_state(struct pinctrl
*p
,
467 struct pinctrl_state
*state
;
469 list_for_each_entry(state
, &p
->states
, node
)
470 if (!strcmp(state
->name
, name
))
476 static struct pinctrl_state
*create_state(struct pinctrl
*p
,
479 struct pinctrl_state
*state
;
481 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
484 "failed to alloc struct pinctrl_state\n");
485 return ERR_PTR(-ENOMEM
);
489 INIT_LIST_HEAD(&state
->settings
);
491 list_add_tail(&state
->node
, &p
->states
);
496 static int add_setting(struct pinctrl
*p
, struct pinctrl_map
const *map
)
498 struct pinctrl_state
*state
;
499 struct pinctrl_setting
*setting
;
502 state
= find_state(p
, map
->name
);
504 state
= create_state(p
, map
->name
);
506 return PTR_ERR(state
);
508 if (map
->type
== PIN_MAP_TYPE_DUMMY_STATE
)
511 setting
= kzalloc(sizeof(*setting
), GFP_KERNEL
);
512 if (setting
== NULL
) {
514 "failed to alloc struct pinctrl_setting\n");
518 setting
->type
= map
->type
;
520 setting
->pctldev
= get_pinctrl_dev_from_devname(map
->ctrl_dev_name
);
521 if (setting
->pctldev
== NULL
) {
522 dev_info(p
->dev
, "unknown pinctrl device %s in map entry, deferring probe",
526 * OK let us guess that the driver is not there yet, and
527 * let's defer obtaining this pinctrl handle to later...
529 return -EPROBE_DEFER
;
533 case PIN_MAP_TYPE_MUX_GROUP
:
534 ret
= pinmux_map_to_setting(map
, setting
);
536 case PIN_MAP_TYPE_CONFIGS_PIN
:
537 case PIN_MAP_TYPE_CONFIGS_GROUP
:
538 ret
= pinconf_map_to_setting(map
, setting
);
549 list_add_tail(&setting
->node
, &state
->settings
);
554 static struct pinctrl
*find_pinctrl(struct device
*dev
)
558 list_for_each_entry(p
, &pinctrl_list
, node
)
565 static void pinctrl_put_locked(struct pinctrl
*p
, bool inlist
);
567 static struct pinctrl
*create_pinctrl(struct device
*dev
)
571 struct pinctrl_maps
*maps_node
;
573 struct pinctrl_map
const *map
;
577 * create the state cookie holder struct pinctrl for each
578 * mapping, this is what consumers will get when requesting
579 * a pin control handle with pinctrl_get()
581 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
583 dev_err(dev
, "failed to alloc struct pinctrl\n");
584 return ERR_PTR(-ENOMEM
);
587 INIT_LIST_HEAD(&p
->states
);
588 INIT_LIST_HEAD(&p
->dt_maps
);
590 ret
= pinctrl_dt_to_map(p
);
596 devname
= dev_name(dev
);
598 /* Iterate over the pin control maps to locate the right ones */
599 for_each_maps(maps_node
, i
, map
) {
600 /* Map must be for this device */
601 if (strcmp(map
->dev_name
, devname
))
604 ret
= add_setting(p
, map
);
606 pinctrl_put_locked(p
, false);
611 /* Add the pinmux to the global list */
612 list_add_tail(&p
->node
, &pinctrl_list
);
617 static struct pinctrl
*pinctrl_get_locked(struct device
*dev
)
622 return ERR_PTR(-EINVAL
);
624 p
= find_pinctrl(dev
);
626 return ERR_PTR(-EBUSY
);
628 p
= create_pinctrl(dev
);
636 * pinctrl_get() - retrieves the pinctrl handle for a device
637 * @dev: the device to obtain the handle for
639 struct pinctrl
*pinctrl_get(struct device
*dev
)
643 mutex_lock(&pinctrl_mutex
);
644 p
= pinctrl_get_locked(dev
);
645 mutex_unlock(&pinctrl_mutex
);
649 EXPORT_SYMBOL_GPL(pinctrl_get
);
651 static void pinctrl_put_locked(struct pinctrl
*p
, bool inlist
)
653 struct pinctrl_state
*state
, *n1
;
654 struct pinctrl_setting
*setting
, *n2
;
656 list_for_each_entry_safe(state
, n1
, &p
->states
, node
) {
657 list_for_each_entry_safe(setting
, n2
, &state
->settings
, node
) {
658 switch (setting
->type
) {
659 case PIN_MAP_TYPE_MUX_GROUP
:
660 if (state
== p
->state
)
661 pinmux_disable_setting(setting
);
662 pinmux_free_setting(setting
);
664 case PIN_MAP_TYPE_CONFIGS_PIN
:
665 case PIN_MAP_TYPE_CONFIGS_GROUP
:
666 pinconf_free_setting(setting
);
671 list_del(&setting
->node
);
674 list_del(&state
->node
);
678 pinctrl_dt_free_maps(p
);
686 * pinctrl_put() - release a previously claimed pinctrl handle
687 * @p: the pinctrl handle to release
689 void pinctrl_put(struct pinctrl
*p
)
691 mutex_lock(&pinctrl_mutex
);
692 pinctrl_put_locked(p
, true);
693 mutex_unlock(&pinctrl_mutex
);
695 EXPORT_SYMBOL_GPL(pinctrl_put
);
697 static struct pinctrl_state
*pinctrl_lookup_state_locked(struct pinctrl
*p
,
700 struct pinctrl_state
*state
;
702 state
= find_state(p
, name
);
704 return ERR_PTR(-ENODEV
);
710 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
711 * @p: the pinctrl handle to retrieve the state from
712 * @name: the state name to retrieve
714 struct pinctrl_state
*pinctrl_lookup_state(struct pinctrl
*p
, const char *name
)
716 struct pinctrl_state
*s
;
718 mutex_lock(&pinctrl_mutex
);
719 s
= pinctrl_lookup_state_locked(p
, name
);
720 mutex_unlock(&pinctrl_mutex
);
724 EXPORT_SYMBOL_GPL(pinctrl_lookup_state
);
726 static int pinctrl_select_state_locked(struct pinctrl
*p
,
727 struct pinctrl_state
*state
)
729 struct pinctrl_setting
*setting
, *setting2
;
732 if (p
->state
== state
)
737 * The set of groups with a mux configuration in the old state
738 * may not be identical to the set of groups with a mux setting
739 * in the new state. While this might be unusual, it's entirely
740 * possible for the "user"-supplied mapping table to be written
741 * that way. For each group that was configured in the old state
742 * but not in the new state, this code puts that group into a
743 * safe/disabled state.
745 list_for_each_entry(setting
, &p
->state
->settings
, node
) {
747 if (setting
->type
!= PIN_MAP_TYPE_MUX_GROUP
)
749 list_for_each_entry(setting2
, &state
->settings
, node
) {
750 if (setting2
->type
!= PIN_MAP_TYPE_MUX_GROUP
)
752 if (setting2
->data
.mux
.group
==
753 setting
->data
.mux
.group
) {
759 pinmux_disable_setting(setting
);
765 /* Apply all the settings for the new state */
766 list_for_each_entry(setting
, &state
->settings
, node
) {
767 switch (setting
->type
) {
768 case PIN_MAP_TYPE_MUX_GROUP
:
769 ret
= pinmux_enable_setting(setting
);
771 case PIN_MAP_TYPE_CONFIGS_PIN
:
772 case PIN_MAP_TYPE_CONFIGS_GROUP
:
773 ret
= pinconf_apply_setting(setting
);
780 /* FIXME: Difficult to return to prev state */
789 * pinctrl_select() - select/activate/program a pinctrl state to HW
790 * @p: the pinctrl handle for the device that requests configuratio
791 * @state: the state handle to select/activate/program
793 int pinctrl_select_state(struct pinctrl
*p
, struct pinctrl_state
*state
)
797 mutex_lock(&pinctrl_mutex
);
798 ret
= pinctrl_select_state_locked(p
, state
);
799 mutex_unlock(&pinctrl_mutex
);
803 EXPORT_SYMBOL_GPL(pinctrl_select_state
);
805 static void devm_pinctrl_release(struct device
*dev
, void *res
)
807 pinctrl_put(*(struct pinctrl
**)res
);
811 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
812 * @dev: the device to obtain the handle for
814 * If there is a need to explicitly destroy the returned struct pinctrl,
815 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
817 struct pinctrl
*devm_pinctrl_get(struct device
*dev
)
819 struct pinctrl
**ptr
, *p
;
821 ptr
= devres_alloc(devm_pinctrl_release
, sizeof(*ptr
), GFP_KERNEL
);
823 return ERR_PTR(-ENOMEM
);
825 p
= pinctrl_get(dev
);
828 devres_add(dev
, ptr
);
835 EXPORT_SYMBOL_GPL(devm_pinctrl_get
);
837 static int devm_pinctrl_match(struct device
*dev
, void *res
, void *data
)
839 struct pinctrl
**p
= res
;
845 * devm_pinctrl_put() - Resource managed pinctrl_put()
846 * @p: the pinctrl handle to release
848 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
849 * this function will not need to be called and the resource management
850 * code will ensure that the resource is freed.
852 void devm_pinctrl_put(struct pinctrl
*p
)
854 WARN_ON(devres_destroy(p
->dev
, devm_pinctrl_release
,
855 devm_pinctrl_match
, p
));
858 EXPORT_SYMBOL_GPL(devm_pinctrl_put
);
860 int pinctrl_register_map(struct pinctrl_map
const *maps
, unsigned num_maps
,
861 bool dup
, bool locked
)
864 struct pinctrl_maps
*maps_node
;
866 pr_debug("add %d pinmux maps\n", num_maps
);
868 /* First sanity check the new mapping */
869 for (i
= 0; i
< num_maps
; i
++) {
870 if (!maps
[i
].dev_name
) {
871 pr_err("failed to register map %s (%d): no device given\n",
877 pr_err("failed to register map %d: no map name given\n",
882 if (maps
[i
].type
!= PIN_MAP_TYPE_DUMMY_STATE
&&
883 !maps
[i
].ctrl_dev_name
) {
884 pr_err("failed to register map %s (%d): no pin control device given\n",
889 switch (maps
[i
].type
) {
890 case PIN_MAP_TYPE_DUMMY_STATE
:
892 case PIN_MAP_TYPE_MUX_GROUP
:
893 ret
= pinmux_validate_map(&maps
[i
], i
);
897 case PIN_MAP_TYPE_CONFIGS_PIN
:
898 case PIN_MAP_TYPE_CONFIGS_GROUP
:
899 ret
= pinconf_validate_map(&maps
[i
], i
);
904 pr_err("failed to register map %s (%d): invalid type given\n",
910 maps_node
= kzalloc(sizeof(*maps_node
), GFP_KERNEL
);
912 pr_err("failed to alloc struct pinctrl_maps\n");
916 maps_node
->num_maps
= num_maps
;
918 maps_node
->maps
= kmemdup(maps
, sizeof(*maps
) * num_maps
,
920 if (!maps_node
->maps
) {
921 pr_err("failed to duplicate mapping table\n");
926 maps_node
->maps
= maps
;
930 mutex_lock(&pinctrl_mutex
);
931 list_add_tail(&maps_node
->node
, &pinctrl_maps
);
933 mutex_unlock(&pinctrl_mutex
);
939 * pinctrl_register_mappings() - register a set of pin controller mappings
940 * @maps: the pincontrol mappings table to register. This should probably be
941 * marked with __initdata so it can be discarded after boot. This
942 * function will perform a shallow copy for the mapping entries.
943 * @num_maps: the number of maps in the mapping table
945 int pinctrl_register_mappings(struct pinctrl_map
const *maps
,
948 return pinctrl_register_map(maps
, num_maps
, true, false);
951 void pinctrl_unregister_map(struct pinctrl_map
const *map
)
953 struct pinctrl_maps
*maps_node
;
955 list_for_each_entry(maps_node
, &pinctrl_maps
, node
) {
956 if (maps_node
->maps
== map
) {
957 list_del(&maps_node
->node
);
963 #ifdef CONFIG_DEBUG_FS
965 static int pinctrl_pins_show(struct seq_file
*s
, void *what
)
967 struct pinctrl_dev
*pctldev
= s
->private;
968 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
971 seq_printf(s
, "registered pins: %d\n", pctldev
->desc
->npins
);
973 mutex_lock(&pinctrl_mutex
);
975 /* The pin number can be retrived from the pin controller descriptor */
976 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
977 struct pin_desc
*desc
;
979 pin
= pctldev
->desc
->pins
[i
].number
;
980 desc
= pin_desc_get(pctldev
, pin
);
981 /* Pin space may be sparse */
985 seq_printf(s
, "pin %d (%s) ", pin
,
986 desc
->name
? desc
->name
: "unnamed");
988 /* Driver-specific info per pin */
989 if (ops
->pin_dbg_show
)
990 ops
->pin_dbg_show(pctldev
, s
, pin
);
995 mutex_unlock(&pinctrl_mutex
);
1000 static int pinctrl_groups_show(struct seq_file
*s
, void *what
)
1002 struct pinctrl_dev
*pctldev
= s
->private;
1003 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1004 unsigned ngroups
, selector
= 0;
1006 ngroups
= ops
->get_groups_count(pctldev
);
1007 mutex_lock(&pinctrl_mutex
);
1009 seq_puts(s
, "registered pin groups:\n");
1010 while (selector
< ngroups
) {
1011 const unsigned *pins
;
1013 const char *gname
= ops
->get_group_name(pctldev
, selector
);
1017 ret
= ops
->get_group_pins(pctldev
, selector
,
1020 seq_printf(s
, "%s [ERROR GETTING PINS]\n",
1023 seq_printf(s
, "group: %s, pins = [ ", gname
);
1024 for (i
= 0; i
< num_pins
; i
++)
1025 seq_printf(s
, "%d ", pins
[i
]);
1031 mutex_unlock(&pinctrl_mutex
);
1036 static int pinctrl_gpioranges_show(struct seq_file
*s
, void *what
)
1038 struct pinctrl_dev
*pctldev
= s
->private;
1039 struct pinctrl_gpio_range
*range
= NULL
;
1041 seq_puts(s
, "GPIO ranges handled:\n");
1043 mutex_lock(&pinctrl_mutex
);
1045 /* Loop over the ranges */
1046 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
1047 seq_printf(s
, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1048 range
->id
, range
->name
,
1049 range
->base
, (range
->base
+ range
->npins
- 1),
1051 (range
->pin_base
+ range
->npins
- 1));
1054 mutex_unlock(&pinctrl_mutex
);
1059 static int pinctrl_devices_show(struct seq_file
*s
, void *what
)
1061 struct pinctrl_dev
*pctldev
;
1063 seq_puts(s
, "name [pinmux] [pinconf]\n");
1065 mutex_lock(&pinctrl_mutex
);
1067 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
1068 seq_printf(s
, "%s ", pctldev
->desc
->name
);
1069 if (pctldev
->desc
->pmxops
)
1070 seq_puts(s
, "yes ");
1073 if (pctldev
->desc
->confops
)
1080 mutex_unlock(&pinctrl_mutex
);
1085 static inline const char *map_type(enum pinctrl_map_type type
)
1087 static const char * const names
[] = {
1095 if (type
>= ARRAY_SIZE(names
))
1101 static int pinctrl_maps_show(struct seq_file
*s
, void *what
)
1103 struct pinctrl_maps
*maps_node
;
1105 struct pinctrl_map
const *map
;
1107 seq_puts(s
, "Pinctrl maps:\n");
1109 mutex_lock(&pinctrl_mutex
);
1111 for_each_maps(maps_node
, i
, map
) {
1112 seq_printf(s
, "device %s\nstate %s\ntype %s (%d)\n",
1113 map
->dev_name
, map
->name
, map_type(map
->type
),
1116 if (map
->type
!= PIN_MAP_TYPE_DUMMY_STATE
)
1117 seq_printf(s
, "controlling device %s\n",
1118 map
->ctrl_dev_name
);
1120 switch (map
->type
) {
1121 case PIN_MAP_TYPE_MUX_GROUP
:
1122 pinmux_show_map(s
, map
);
1124 case PIN_MAP_TYPE_CONFIGS_PIN
:
1125 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1126 pinconf_show_map(s
, map
);
1132 seq_printf(s
, "\n");
1135 mutex_unlock(&pinctrl_mutex
);
1140 static int pinctrl_show(struct seq_file
*s
, void *what
)
1143 struct pinctrl_state
*state
;
1144 struct pinctrl_setting
*setting
;
1146 seq_puts(s
, "Requested pin control handlers their pinmux maps:\n");
1148 mutex_lock(&pinctrl_mutex
);
1150 list_for_each_entry(p
, &pinctrl_list
, node
) {
1151 seq_printf(s
, "device: %s current state: %s\n",
1153 p
->state
? p
->state
->name
: "none");
1155 list_for_each_entry(state
, &p
->states
, node
) {
1156 seq_printf(s
, " state: %s\n", state
->name
);
1158 list_for_each_entry(setting
, &state
->settings
, node
) {
1159 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
1161 seq_printf(s
, " type: %s controller %s ",
1162 map_type(setting
->type
),
1163 pinctrl_dev_get_name(pctldev
));
1165 switch (setting
->type
) {
1166 case PIN_MAP_TYPE_MUX_GROUP
:
1167 pinmux_show_setting(s
, setting
);
1169 case PIN_MAP_TYPE_CONFIGS_PIN
:
1170 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1171 pinconf_show_setting(s
, setting
);
1180 mutex_unlock(&pinctrl_mutex
);
1185 static int pinctrl_pins_open(struct inode
*inode
, struct file
*file
)
1187 return single_open(file
, pinctrl_pins_show
, inode
->i_private
);
1190 static int pinctrl_groups_open(struct inode
*inode
, struct file
*file
)
1192 return single_open(file
, pinctrl_groups_show
, inode
->i_private
);
1195 static int pinctrl_gpioranges_open(struct inode
*inode
, struct file
*file
)
1197 return single_open(file
, pinctrl_gpioranges_show
, inode
->i_private
);
1200 static int pinctrl_devices_open(struct inode
*inode
, struct file
*file
)
1202 return single_open(file
, pinctrl_devices_show
, NULL
);
1205 static int pinctrl_maps_open(struct inode
*inode
, struct file
*file
)
1207 return single_open(file
, pinctrl_maps_show
, NULL
);
1210 static int pinctrl_open(struct inode
*inode
, struct file
*file
)
1212 return single_open(file
, pinctrl_show
, NULL
);
1215 static const struct file_operations pinctrl_pins_ops
= {
1216 .open
= pinctrl_pins_open
,
1218 .llseek
= seq_lseek
,
1219 .release
= single_release
,
1222 static const struct file_operations pinctrl_groups_ops
= {
1223 .open
= pinctrl_groups_open
,
1225 .llseek
= seq_lseek
,
1226 .release
= single_release
,
1229 static const struct file_operations pinctrl_gpioranges_ops
= {
1230 .open
= pinctrl_gpioranges_open
,
1232 .llseek
= seq_lseek
,
1233 .release
= single_release
,
1236 static const struct file_operations pinctrl_devices_ops
= {
1237 .open
= pinctrl_devices_open
,
1239 .llseek
= seq_lseek
,
1240 .release
= single_release
,
1243 static const struct file_operations pinctrl_maps_ops
= {
1244 .open
= pinctrl_maps_open
,
1246 .llseek
= seq_lseek
,
1247 .release
= single_release
,
1250 static const struct file_operations pinctrl_ops
= {
1251 .open
= pinctrl_open
,
1253 .llseek
= seq_lseek
,
1254 .release
= single_release
,
1257 static struct dentry
*debugfs_root
;
1259 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1261 struct dentry
*device_root
;
1263 device_root
= debugfs_create_dir(dev_name(pctldev
->dev
),
1265 pctldev
->device_root
= device_root
;
1267 if (IS_ERR(device_root
) || !device_root
) {
1268 pr_warn("failed to create debugfs directory for %s\n",
1269 dev_name(pctldev
->dev
));
1272 debugfs_create_file("pins", S_IFREG
| S_IRUGO
,
1273 device_root
, pctldev
, &pinctrl_pins_ops
);
1274 debugfs_create_file("pingroups", S_IFREG
| S_IRUGO
,
1275 device_root
, pctldev
, &pinctrl_groups_ops
);
1276 debugfs_create_file("gpio-ranges", S_IFREG
| S_IRUGO
,
1277 device_root
, pctldev
, &pinctrl_gpioranges_ops
);
1278 pinmux_init_device_debugfs(device_root
, pctldev
);
1279 pinconf_init_device_debugfs(device_root
, pctldev
);
1282 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1284 debugfs_remove_recursive(pctldev
->device_root
);
1287 static void pinctrl_init_debugfs(void)
1289 debugfs_root
= debugfs_create_dir("pinctrl", NULL
);
1290 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
1291 pr_warn("failed to create debugfs directory\n");
1292 debugfs_root
= NULL
;
1296 debugfs_create_file("pinctrl-devices", S_IFREG
| S_IRUGO
,
1297 debugfs_root
, NULL
, &pinctrl_devices_ops
);
1298 debugfs_create_file("pinctrl-maps", S_IFREG
| S_IRUGO
,
1299 debugfs_root
, NULL
, &pinctrl_maps_ops
);
1300 debugfs_create_file("pinctrl-handles", S_IFREG
| S_IRUGO
,
1301 debugfs_root
, NULL
, &pinctrl_ops
);
1304 #else /* CONFIG_DEBUG_FS */
1306 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1310 static void pinctrl_init_debugfs(void)
1314 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1320 static int pinctrl_check_ops(struct pinctrl_dev
*pctldev
)
1322 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1325 !ops
->get_groups_count
||
1326 !ops
->get_group_name
||
1327 !ops
->get_group_pins
)
1330 if (ops
->dt_node_to_map
&& !ops
->dt_free_map
)
1337 * pinctrl_register() - register a pin controller device
1338 * @pctldesc: descriptor for this pin controller
1339 * @dev: parent device for this pin controller
1340 * @driver_data: private pin controller data for this pin controller
1342 struct pinctrl_dev
*pinctrl_register(struct pinctrl_desc
*pctldesc
,
1343 struct device
*dev
, void *driver_data
)
1345 struct pinctrl_dev
*pctldev
;
1348 if (pctldesc
== NULL
)
1350 if (pctldesc
->name
== NULL
)
1353 pctldev
= kzalloc(sizeof(*pctldev
), GFP_KERNEL
);
1354 if (pctldev
== NULL
) {
1355 dev_err(dev
, "failed to alloc struct pinctrl_dev\n");
1359 /* Initialize pin control device struct */
1360 pctldev
->owner
= pctldesc
->owner
;
1361 pctldev
->desc
= pctldesc
;
1362 pctldev
->driver_data
= driver_data
;
1363 INIT_RADIX_TREE(&pctldev
->pin_desc_tree
, GFP_KERNEL
);
1364 INIT_LIST_HEAD(&pctldev
->gpio_ranges
);
1367 /* check core ops for sanity */
1368 ret
= pinctrl_check_ops(pctldev
);
1370 pr_err("%s pinctrl ops lacks necessary functions\n",
1375 /* If we're implementing pinmuxing, check the ops for sanity */
1376 if (pctldesc
->pmxops
) {
1377 ret
= pinmux_check_ops(pctldev
);
1379 pr_err("%s pinmux ops lacks necessary functions\n",
1385 /* If we're implementing pinconfig, check the ops for sanity */
1386 if (pctldesc
->confops
) {
1387 ret
= pinconf_check_ops(pctldev
);
1389 pr_err("%s pin config ops lacks necessary functions\n",
1395 /* Register all the pins */
1396 pr_debug("try to register %d pins on %s...\n",
1397 pctldesc
->npins
, pctldesc
->name
);
1398 ret
= pinctrl_register_pins(pctldev
, pctldesc
->pins
, pctldesc
->npins
);
1400 pr_err("error during pin registration\n");
1401 pinctrl_free_pindescs(pctldev
, pctldesc
->pins
,
1406 mutex_lock(&pinctrl_mutex
);
1408 list_add_tail(&pctldev
->node
, &pinctrldev_list
);
1410 pctldev
->p
= pinctrl_get_locked(pctldev
->dev
);
1411 if (!IS_ERR(pctldev
->p
)) {
1412 struct pinctrl_state
*s
=
1413 pinctrl_lookup_state_locked(pctldev
->p
,
1414 PINCTRL_STATE_DEFAULT
);
1416 pinctrl_select_state_locked(pctldev
->p
, s
);
1419 mutex_unlock(&pinctrl_mutex
);
1421 pinctrl_init_device_debugfs(pctldev
);
1429 EXPORT_SYMBOL_GPL(pinctrl_register
);
1432 * pinctrl_unregister() - unregister pinmux
1433 * @pctldev: pin controller to unregister
1435 * Called by pinmux drivers to unregister a pinmux.
1437 void pinctrl_unregister(struct pinctrl_dev
*pctldev
)
1439 if (pctldev
== NULL
)
1442 pinctrl_remove_device_debugfs(pctldev
);
1444 mutex_lock(&pinctrl_mutex
);
1446 if (!IS_ERR(pctldev
->p
))
1447 pinctrl_put_locked(pctldev
->p
, true);
1449 /* TODO: check that no pinmuxes are still active? */
1450 list_del(&pctldev
->node
);
1451 /* Destroy descriptor tree */
1452 pinctrl_free_pindescs(pctldev
, pctldev
->desc
->pins
,
1453 pctldev
->desc
->npins
);
1456 mutex_unlock(&pinctrl_mutex
);
1458 EXPORT_SYMBOL_GPL(pinctrl_unregister
);
1460 static int __init
pinctrl_init(void)
1462 pr_info("initialized pinctrl subsystem\n");
1463 pinctrl_init_debugfs();
1467 /* init early since many drivers really need to initialized pinmux early */
1468 core_initcall(pinctrl_init
);