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 static bool pinctrl_dummy_state
;
48 /* Mutex taken by all entry points */
49 DEFINE_MUTEX(pinctrl_mutex
);
51 /* Global list of pin control devices (struct pinctrl_dev) */
52 LIST_HEAD(pinctrldev_list
);
54 /* List of pin controller handles (struct pinctrl) */
55 static LIST_HEAD(pinctrl_list
);
57 /* List of pinctrl maps (struct pinctrl_maps) */
58 static LIST_HEAD(pinctrl_maps
);
60 #define for_each_maps(_maps_node_, _i_, _map_) \
61 list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
62 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
63 _i_ < _maps_node_->num_maps; \
64 _i_++, _map_ = &_maps_node_->maps[_i_])
67 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
69 * Usually this function is called by platforms without pinctrl driver support
70 * but run with some shared drivers using pinctrl APIs.
71 * After calling this function, the pinctrl core will return successfully
72 * with creating a dummy state for the driver to keep going smoothly.
74 void pinctrl_provide_dummies(void)
76 pinctrl_dummy_state
= true;
79 const char *pinctrl_dev_get_name(struct pinctrl_dev
*pctldev
)
81 /* We're not allowed to register devices without name */
82 return pctldev
->desc
->name
;
84 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name
);
86 void *pinctrl_dev_get_drvdata(struct pinctrl_dev
*pctldev
)
88 return pctldev
->driver_data
;
90 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata
);
93 * get_pinctrl_dev_from_devname() - look up pin controller device
94 * @devname: the name of a device instance, as returned by dev_name()
96 * Looks up a pin control device matching a certain device name or pure device
97 * pointer, the pure device pointer will take precedence.
99 struct pinctrl_dev
*get_pinctrl_dev_from_devname(const char *devname
)
101 struct pinctrl_dev
*pctldev
= NULL
;
107 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
108 if (!strcmp(dev_name(pctldev
->dev
), devname
)) {
109 /* Matched on device name */
115 return found
? pctldev
: NULL
;
119 * pin_get_from_name() - look up a pin number from a name
120 * @pctldev: the pin control device to lookup the pin on
121 * @name: the name of the pin to look up
123 int pin_get_from_name(struct pinctrl_dev
*pctldev
, const char *name
)
127 /* The pin number can be retrived from the pin controller descriptor */
128 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
129 struct pin_desc
*desc
;
131 pin
= pctldev
->desc
->pins
[i
].number
;
132 desc
= pin_desc_get(pctldev
, pin
);
133 /* Pin space may be sparse */
136 if (desc
->name
&& !strcmp(name
, desc
->name
))
144 * pin_get_name_from_id() - look up a pin name from a pin id
145 * @pctldev: the pin control device to lookup the pin on
146 * @name: the name of the pin to look up
148 const char *pin_get_name(struct pinctrl_dev
*pctldev
, const unsigned pin
)
150 const struct pin_desc
*desc
;
152 desc
= pin_desc_get(pctldev
, pin
);
154 dev_err(pctldev
->dev
, "failed to get pin(%d) name\n",
163 * pin_is_valid() - check if pin exists on controller
164 * @pctldev: the pin control device to check the pin on
165 * @pin: pin to check, use the local pin controller index number
167 * This tells us whether a certain pin exist on a certain pin controller or
168 * not. Pin lists may be sparse, so some pins may not exist.
170 bool pin_is_valid(struct pinctrl_dev
*pctldev
, int pin
)
172 struct pin_desc
*pindesc
;
177 mutex_lock(&pinctrl_mutex
);
178 pindesc
= pin_desc_get(pctldev
, pin
);
179 mutex_unlock(&pinctrl_mutex
);
181 return pindesc
!= NULL
;
183 EXPORT_SYMBOL_GPL(pin_is_valid
);
185 /* Deletes a range of pin descriptors */
186 static void pinctrl_free_pindescs(struct pinctrl_dev
*pctldev
,
187 const struct pinctrl_pin_desc
*pins
,
192 for (i
= 0; i
< num_pins
; i
++) {
193 struct pin_desc
*pindesc
;
195 pindesc
= radix_tree_lookup(&pctldev
->pin_desc_tree
,
197 if (pindesc
!= NULL
) {
198 radix_tree_delete(&pctldev
->pin_desc_tree
,
200 if (pindesc
->dynamic_name
)
201 kfree(pindesc
->name
);
207 static int pinctrl_register_one_pin(struct pinctrl_dev
*pctldev
,
208 unsigned number
, const char *name
)
210 struct pin_desc
*pindesc
;
212 pindesc
= pin_desc_get(pctldev
, number
);
213 if (pindesc
!= NULL
) {
214 pr_err("pin %d already registered on %s\n", number
,
215 pctldev
->desc
->name
);
219 pindesc
= kzalloc(sizeof(*pindesc
), GFP_KERNEL
);
220 if (pindesc
== NULL
) {
221 dev_err(pctldev
->dev
, "failed to alloc struct pin_desc\n");
226 pindesc
->pctldev
= pctldev
;
228 /* Copy basic pin info */
230 pindesc
->name
= name
;
232 pindesc
->name
= kasprintf(GFP_KERNEL
, "PIN%u", number
);
233 if (pindesc
->name
== NULL
) {
237 pindesc
->dynamic_name
= true;
240 radix_tree_insert(&pctldev
->pin_desc_tree
, number
, pindesc
);
241 pr_debug("registered pin %d (%s) on %s\n",
242 number
, pindesc
->name
, pctldev
->desc
->name
);
246 static int pinctrl_register_pins(struct pinctrl_dev
*pctldev
,
247 struct pinctrl_pin_desc
const *pins
,
253 for (i
= 0; i
< num_descs
; i
++) {
254 ret
= pinctrl_register_one_pin(pctldev
,
255 pins
[i
].number
, pins
[i
].name
);
264 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
265 * @pctldev: pin controller device to check
266 * @gpio: gpio pin to check taken from the global GPIO pin space
268 * Tries to match a GPIO pin number to the ranges handled by a certain pin
269 * controller, return the range or NULL
271 static struct pinctrl_gpio_range
*
272 pinctrl_match_gpio_range(struct pinctrl_dev
*pctldev
, unsigned gpio
)
274 struct pinctrl_gpio_range
*range
= NULL
;
276 /* Loop over the ranges */
277 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
278 /* Check if we're in the valid range */
279 if (gpio
>= range
->base
&&
280 gpio
< range
->base
+ range
->npins
) {
289 * pinctrl_get_device_gpio_range() - find device for GPIO range
290 * @gpio: the pin to locate the pin controller for
291 * @outdev: the pin control device if found
292 * @outrange: the GPIO range if found
294 * Find the pin controller handling a certain GPIO pin from the pinspace of
295 * the GPIO subsystem, return the device and the matching GPIO range. Returns
296 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
297 * may still have not been registered.
299 static int pinctrl_get_device_gpio_range(unsigned gpio
,
300 struct pinctrl_dev
**outdev
,
301 struct pinctrl_gpio_range
**outrange
)
303 struct pinctrl_dev
*pctldev
= NULL
;
305 /* Loop over the pin controllers */
306 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
307 struct pinctrl_gpio_range
*range
;
309 range
= pinctrl_match_gpio_range(pctldev
, gpio
);
317 return -EPROBE_DEFER
;
321 * pinctrl_add_gpio_range() - register a GPIO range for a controller
322 * @pctldev: pin controller device to add the range to
323 * @range: the GPIO range to add
325 * This adds a range of GPIOs to be handled by a certain pin controller. Call
326 * this to register handled ranges after registering your pin controller.
328 void pinctrl_add_gpio_range(struct pinctrl_dev
*pctldev
,
329 struct pinctrl_gpio_range
*range
)
331 mutex_lock(&pinctrl_mutex
);
332 list_add_tail(&range
->node
, &pctldev
->gpio_ranges
);
333 mutex_unlock(&pinctrl_mutex
);
335 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range
);
337 void pinctrl_add_gpio_ranges(struct pinctrl_dev
*pctldev
,
338 struct pinctrl_gpio_range
*ranges
,
343 for (i
= 0; i
< nranges
; i
++)
344 pinctrl_add_gpio_range(pctldev
, &ranges
[i
]);
346 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges
);
348 struct pinctrl_dev
*find_pinctrl_and_add_gpio_range(const char *devname
,
349 struct pinctrl_gpio_range
*range
)
351 struct pinctrl_dev
*pctldev
= get_pinctrl_dev_from_devname(devname
);
356 pinctrl_add_gpio_range(pctldev
, range
);
359 EXPORT_SYMBOL_GPL(find_pinctrl_and_add_gpio_range
);
362 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
363 * @pctldev: pin controller device to remove the range from
364 * @range: the GPIO range to remove
366 void pinctrl_remove_gpio_range(struct pinctrl_dev
*pctldev
,
367 struct pinctrl_gpio_range
*range
)
369 mutex_lock(&pinctrl_mutex
);
370 list_del(&range
->node
);
371 mutex_unlock(&pinctrl_mutex
);
373 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range
);
376 * pinctrl_get_group_selector() - returns the group selector for a group
377 * @pctldev: the pin controller handling the group
378 * @pin_group: the pin group to look up
380 int pinctrl_get_group_selector(struct pinctrl_dev
*pctldev
,
381 const char *pin_group
)
383 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
384 unsigned ngroups
= pctlops
->get_groups_count(pctldev
);
385 unsigned group_selector
= 0;
387 while (group_selector
< ngroups
) {
388 const char *gname
= pctlops
->get_group_name(pctldev
,
390 if (!strcmp(gname
, pin_group
)) {
391 dev_dbg(pctldev
->dev
,
392 "found group selector %u for %s\n",
395 return group_selector
;
401 dev_err(pctldev
->dev
, "does not have pin group %s\n",
408 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
409 * @gpio: the GPIO pin number from the GPIO subsystem number space
411 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
412 * as part of their gpio_request() semantics, platforms and individual drivers
413 * shall *NOT* request GPIO pins to be muxed in.
415 int pinctrl_request_gpio(unsigned gpio
)
417 struct pinctrl_dev
*pctldev
;
418 struct pinctrl_gpio_range
*range
;
422 mutex_lock(&pinctrl_mutex
);
424 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
426 mutex_unlock(&pinctrl_mutex
);
430 /* Convert to the pin controllers number space */
431 pin
= gpio
- range
->base
+ range
->pin_base
;
433 ret
= pinmux_request_gpio(pctldev
, range
, pin
, gpio
);
435 mutex_unlock(&pinctrl_mutex
);
438 EXPORT_SYMBOL_GPL(pinctrl_request_gpio
);
441 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
442 * @gpio: the GPIO pin number from the GPIO subsystem number space
444 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
445 * as part of their gpio_free() semantics, platforms and individual drivers
446 * shall *NOT* request GPIO pins to be muxed out.
448 void pinctrl_free_gpio(unsigned gpio
)
450 struct pinctrl_dev
*pctldev
;
451 struct pinctrl_gpio_range
*range
;
455 mutex_lock(&pinctrl_mutex
);
457 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
459 mutex_unlock(&pinctrl_mutex
);
463 /* Convert to the pin controllers number space */
464 pin
= gpio
- range
->base
+ range
->pin_base
;
466 pinmux_free_gpio(pctldev
, pin
, range
);
468 mutex_unlock(&pinctrl_mutex
);
470 EXPORT_SYMBOL_GPL(pinctrl_free_gpio
);
472 static int pinctrl_gpio_direction(unsigned gpio
, bool input
)
474 struct pinctrl_dev
*pctldev
;
475 struct pinctrl_gpio_range
*range
;
479 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
483 /* Convert to the pin controllers number space */
484 pin
= gpio
- range
->base
+ range
->pin_base
;
486 return pinmux_gpio_direction(pctldev
, range
, pin
, input
);
490 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
491 * @gpio: the GPIO pin number from the GPIO subsystem number space
493 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
494 * as part of their gpio_direction_input() semantics, platforms and individual
495 * drivers shall *NOT* touch pin control GPIO calls.
497 int pinctrl_gpio_direction_input(unsigned gpio
)
500 mutex_lock(&pinctrl_mutex
);
501 ret
= pinctrl_gpio_direction(gpio
, true);
502 mutex_unlock(&pinctrl_mutex
);
505 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input
);
508 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
509 * @gpio: the GPIO pin number from the GPIO subsystem number space
511 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
512 * as part of their gpio_direction_output() semantics, platforms and individual
513 * drivers shall *NOT* touch pin control GPIO calls.
515 int pinctrl_gpio_direction_output(unsigned gpio
)
518 mutex_lock(&pinctrl_mutex
);
519 ret
= pinctrl_gpio_direction(gpio
, false);
520 mutex_unlock(&pinctrl_mutex
);
523 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output
);
525 static struct pinctrl_state
*find_state(struct pinctrl
*p
,
528 struct pinctrl_state
*state
;
530 list_for_each_entry(state
, &p
->states
, node
)
531 if (!strcmp(state
->name
, name
))
537 static struct pinctrl_state
*create_state(struct pinctrl
*p
,
540 struct pinctrl_state
*state
;
542 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
545 "failed to alloc struct pinctrl_state\n");
546 return ERR_PTR(-ENOMEM
);
550 INIT_LIST_HEAD(&state
->settings
);
552 list_add_tail(&state
->node
, &p
->states
);
557 static int add_setting(struct pinctrl
*p
, struct pinctrl_map
const *map
)
559 struct pinctrl_state
*state
;
560 struct pinctrl_setting
*setting
;
563 state
= find_state(p
, map
->name
);
565 state
= create_state(p
, map
->name
);
567 return PTR_ERR(state
);
569 if (map
->type
== PIN_MAP_TYPE_DUMMY_STATE
)
572 setting
= kzalloc(sizeof(*setting
), GFP_KERNEL
);
573 if (setting
== NULL
) {
575 "failed to alloc struct pinctrl_setting\n");
579 setting
->type
= map
->type
;
581 setting
->pctldev
= get_pinctrl_dev_from_devname(map
->ctrl_dev_name
);
582 if (setting
->pctldev
== NULL
) {
583 dev_info(p
->dev
, "unknown pinctrl device %s in map entry, deferring probe",
587 * OK let us guess that the driver is not there yet, and
588 * let's defer obtaining this pinctrl handle to later...
590 return -EPROBE_DEFER
;
593 setting
->dev_name
= map
->dev_name
;
596 case PIN_MAP_TYPE_MUX_GROUP
:
597 ret
= pinmux_map_to_setting(map
, setting
);
599 case PIN_MAP_TYPE_CONFIGS_PIN
:
600 case PIN_MAP_TYPE_CONFIGS_GROUP
:
601 ret
= pinconf_map_to_setting(map
, setting
);
612 list_add_tail(&setting
->node
, &state
->settings
);
617 static struct pinctrl
*find_pinctrl(struct device
*dev
)
621 list_for_each_entry(p
, &pinctrl_list
, node
)
628 static void pinctrl_put_locked(struct pinctrl
*p
, bool inlist
);
630 static struct pinctrl
*create_pinctrl(struct device
*dev
)
634 struct pinctrl_maps
*maps_node
;
636 struct pinctrl_map
const *map
;
640 * create the state cookie holder struct pinctrl for each
641 * mapping, this is what consumers will get when requesting
642 * a pin control handle with pinctrl_get()
644 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
646 dev_err(dev
, "failed to alloc struct pinctrl\n");
647 return ERR_PTR(-ENOMEM
);
650 INIT_LIST_HEAD(&p
->states
);
651 INIT_LIST_HEAD(&p
->dt_maps
);
653 ret
= pinctrl_dt_to_map(p
);
659 devname
= dev_name(dev
);
661 /* Iterate over the pin control maps to locate the right ones */
662 for_each_maps(maps_node
, i
, map
) {
663 /* Map must be for this device */
664 if (strcmp(map
->dev_name
, devname
))
667 ret
= add_setting(p
, map
);
669 pinctrl_put_locked(p
, false);
674 /* Add the pinmux to the global list */
675 list_add_tail(&p
->node
, &pinctrl_list
);
680 static struct pinctrl
*pinctrl_get_locked(struct device
*dev
)
685 return ERR_PTR(-EINVAL
);
687 p
= find_pinctrl(dev
);
689 return ERR_PTR(-EBUSY
);
691 return create_pinctrl(dev
);
695 * pinctrl_get() - retrieves the pinctrl handle for a device
696 * @dev: the device to obtain the handle for
698 struct pinctrl
*pinctrl_get(struct device
*dev
)
702 mutex_lock(&pinctrl_mutex
);
703 p
= pinctrl_get_locked(dev
);
704 mutex_unlock(&pinctrl_mutex
);
708 EXPORT_SYMBOL_GPL(pinctrl_get
);
710 static void pinctrl_put_locked(struct pinctrl
*p
, bool inlist
)
712 struct pinctrl_state
*state
, *n1
;
713 struct pinctrl_setting
*setting
, *n2
;
715 list_for_each_entry_safe(state
, n1
, &p
->states
, node
) {
716 list_for_each_entry_safe(setting
, n2
, &state
->settings
, node
) {
717 switch (setting
->type
) {
718 case PIN_MAP_TYPE_MUX_GROUP
:
719 if (state
== p
->state
)
720 pinmux_disable_setting(setting
);
721 pinmux_free_setting(setting
);
723 case PIN_MAP_TYPE_CONFIGS_PIN
:
724 case PIN_MAP_TYPE_CONFIGS_GROUP
:
725 pinconf_free_setting(setting
);
730 list_del(&setting
->node
);
733 list_del(&state
->node
);
737 pinctrl_dt_free_maps(p
);
745 * pinctrl_put() - release a previously claimed pinctrl handle
746 * @p: the pinctrl handle to release
748 void pinctrl_put(struct pinctrl
*p
)
750 mutex_lock(&pinctrl_mutex
);
751 pinctrl_put_locked(p
, true);
752 mutex_unlock(&pinctrl_mutex
);
754 EXPORT_SYMBOL_GPL(pinctrl_put
);
756 static struct pinctrl_state
*pinctrl_lookup_state_locked(struct pinctrl
*p
,
759 struct pinctrl_state
*state
;
761 state
= find_state(p
, name
);
763 if (pinctrl_dummy_state
) {
764 /* create dummy state */
765 dev_dbg(p
->dev
, "using pinctrl dummy state (%s)\n",
767 state
= create_state(p
, name
);
769 state
= ERR_PTR(-ENODEV
);
776 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
777 * @p: the pinctrl handle to retrieve the state from
778 * @name: the state name to retrieve
780 struct pinctrl_state
*pinctrl_lookup_state(struct pinctrl
*p
, const char *name
)
782 struct pinctrl_state
*s
;
784 mutex_lock(&pinctrl_mutex
);
785 s
= pinctrl_lookup_state_locked(p
, name
);
786 mutex_unlock(&pinctrl_mutex
);
790 EXPORT_SYMBOL_GPL(pinctrl_lookup_state
);
792 static int pinctrl_select_state_locked(struct pinctrl
*p
,
793 struct pinctrl_state
*state
)
795 struct pinctrl_setting
*setting
, *setting2
;
798 if (p
->state
== state
)
803 * The set of groups with a mux configuration in the old state
804 * may not be identical to the set of groups with a mux setting
805 * in the new state. While this might be unusual, it's entirely
806 * possible for the "user"-supplied mapping table to be written
807 * that way. For each group that was configured in the old state
808 * but not in the new state, this code puts that group into a
809 * safe/disabled state.
811 list_for_each_entry(setting
, &p
->state
->settings
, node
) {
813 if (setting
->type
!= PIN_MAP_TYPE_MUX_GROUP
)
815 list_for_each_entry(setting2
, &state
->settings
, node
) {
816 if (setting2
->type
!= PIN_MAP_TYPE_MUX_GROUP
)
818 if (setting2
->data
.mux
.group
==
819 setting
->data
.mux
.group
) {
825 pinmux_disable_setting(setting
);
831 /* Apply all the settings for the new state */
832 list_for_each_entry(setting
, &state
->settings
, node
) {
833 switch (setting
->type
) {
834 case PIN_MAP_TYPE_MUX_GROUP
:
835 ret
= pinmux_enable_setting(setting
);
837 case PIN_MAP_TYPE_CONFIGS_PIN
:
838 case PIN_MAP_TYPE_CONFIGS_GROUP
:
839 ret
= pinconf_apply_setting(setting
);
846 /* FIXME: Difficult to return to prev state */
855 * pinctrl_select() - select/activate/program a pinctrl state to HW
856 * @p: the pinctrl handle for the device that requests configuratio
857 * @state: the state handle to select/activate/program
859 int pinctrl_select_state(struct pinctrl
*p
, struct pinctrl_state
*state
)
863 mutex_lock(&pinctrl_mutex
);
864 ret
= pinctrl_select_state_locked(p
, state
);
865 mutex_unlock(&pinctrl_mutex
);
869 EXPORT_SYMBOL_GPL(pinctrl_select_state
);
871 static void devm_pinctrl_release(struct device
*dev
, void *res
)
873 pinctrl_put(*(struct pinctrl
**)res
);
877 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
878 * @dev: the device to obtain the handle for
880 * If there is a need to explicitly destroy the returned struct pinctrl,
881 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
883 struct pinctrl
*devm_pinctrl_get(struct device
*dev
)
885 struct pinctrl
**ptr
, *p
;
887 ptr
= devres_alloc(devm_pinctrl_release
, sizeof(*ptr
), GFP_KERNEL
);
889 return ERR_PTR(-ENOMEM
);
891 p
= pinctrl_get(dev
);
894 devres_add(dev
, ptr
);
901 EXPORT_SYMBOL_GPL(devm_pinctrl_get
);
903 static int devm_pinctrl_match(struct device
*dev
, void *res
, void *data
)
905 struct pinctrl
**p
= res
;
911 * devm_pinctrl_put() - Resource managed pinctrl_put()
912 * @p: the pinctrl handle to release
914 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
915 * this function will not need to be called and the resource management
916 * code will ensure that the resource is freed.
918 void devm_pinctrl_put(struct pinctrl
*p
)
920 WARN_ON(devres_destroy(p
->dev
, devm_pinctrl_release
,
921 devm_pinctrl_match
, p
));
924 EXPORT_SYMBOL_GPL(devm_pinctrl_put
);
926 int pinctrl_register_map(struct pinctrl_map
const *maps
, unsigned num_maps
,
927 bool dup
, bool locked
)
930 struct pinctrl_maps
*maps_node
;
932 pr_debug("add %d pinmux maps\n", num_maps
);
934 /* First sanity check the new mapping */
935 for (i
= 0; i
< num_maps
; i
++) {
936 if (!maps
[i
].dev_name
) {
937 pr_err("failed to register map %s (%d): no device given\n",
943 pr_err("failed to register map %d: no map name given\n",
948 if (maps
[i
].type
!= PIN_MAP_TYPE_DUMMY_STATE
&&
949 !maps
[i
].ctrl_dev_name
) {
950 pr_err("failed to register map %s (%d): no pin control device given\n",
955 switch (maps
[i
].type
) {
956 case PIN_MAP_TYPE_DUMMY_STATE
:
958 case PIN_MAP_TYPE_MUX_GROUP
:
959 ret
= pinmux_validate_map(&maps
[i
], i
);
963 case PIN_MAP_TYPE_CONFIGS_PIN
:
964 case PIN_MAP_TYPE_CONFIGS_GROUP
:
965 ret
= pinconf_validate_map(&maps
[i
], i
);
970 pr_err("failed to register map %s (%d): invalid type given\n",
976 maps_node
= kzalloc(sizeof(*maps_node
), GFP_KERNEL
);
978 pr_err("failed to alloc struct pinctrl_maps\n");
982 maps_node
->num_maps
= num_maps
;
984 maps_node
->maps
= kmemdup(maps
, sizeof(*maps
) * num_maps
,
986 if (!maps_node
->maps
) {
987 pr_err("failed to duplicate mapping table\n");
992 maps_node
->maps
= maps
;
996 mutex_lock(&pinctrl_mutex
);
997 list_add_tail(&maps_node
->node
, &pinctrl_maps
);
999 mutex_unlock(&pinctrl_mutex
);
1005 * pinctrl_register_mappings() - register a set of pin controller mappings
1006 * @maps: the pincontrol mappings table to register. This should probably be
1007 * marked with __initdata so it can be discarded after boot. This
1008 * function will perform a shallow copy for the mapping entries.
1009 * @num_maps: the number of maps in the mapping table
1011 int pinctrl_register_mappings(struct pinctrl_map
const *maps
,
1014 return pinctrl_register_map(maps
, num_maps
, true, false);
1017 void pinctrl_unregister_map(struct pinctrl_map
const *map
)
1019 struct pinctrl_maps
*maps_node
;
1021 list_for_each_entry(maps_node
, &pinctrl_maps
, node
) {
1022 if (maps_node
->maps
== map
) {
1023 list_del(&maps_node
->node
);
1029 #ifdef CONFIG_DEBUG_FS
1031 static int pinctrl_pins_show(struct seq_file
*s
, void *what
)
1033 struct pinctrl_dev
*pctldev
= s
->private;
1034 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1037 seq_printf(s
, "registered pins: %d\n", pctldev
->desc
->npins
);
1039 mutex_lock(&pinctrl_mutex
);
1041 /* The pin number can be retrived from the pin controller descriptor */
1042 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
1043 struct pin_desc
*desc
;
1045 pin
= pctldev
->desc
->pins
[i
].number
;
1046 desc
= pin_desc_get(pctldev
, pin
);
1047 /* Pin space may be sparse */
1051 seq_printf(s
, "pin %d (%s) ", pin
,
1052 desc
->name
? desc
->name
: "unnamed");
1054 /* Driver-specific info per pin */
1055 if (ops
->pin_dbg_show
)
1056 ops
->pin_dbg_show(pctldev
, s
, pin
);
1061 mutex_unlock(&pinctrl_mutex
);
1066 static int pinctrl_groups_show(struct seq_file
*s
, void *what
)
1068 struct pinctrl_dev
*pctldev
= s
->private;
1069 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1070 unsigned ngroups
, selector
= 0;
1072 ngroups
= ops
->get_groups_count(pctldev
);
1073 mutex_lock(&pinctrl_mutex
);
1075 seq_puts(s
, "registered pin groups:\n");
1076 while (selector
< ngroups
) {
1077 const unsigned *pins
;
1079 const char *gname
= ops
->get_group_name(pctldev
, selector
);
1084 ret
= ops
->get_group_pins(pctldev
, selector
,
1087 seq_printf(s
, "%s [ERROR GETTING PINS]\n",
1090 seq_printf(s
, "group: %s\n", gname
);
1091 for (i
= 0; i
< num_pins
; i
++) {
1092 pname
= pin_get_name(pctldev
, pins
[i
]);
1093 if (WARN_ON(!pname
)) {
1094 mutex_unlock(&pinctrl_mutex
);
1097 seq_printf(s
, "pin %d (%s)\n", pins
[i
], pname
);
1104 mutex_unlock(&pinctrl_mutex
);
1109 static int pinctrl_gpioranges_show(struct seq_file
*s
, void *what
)
1111 struct pinctrl_dev
*pctldev
= s
->private;
1112 struct pinctrl_gpio_range
*range
= NULL
;
1114 seq_puts(s
, "GPIO ranges handled:\n");
1116 mutex_lock(&pinctrl_mutex
);
1118 /* Loop over the ranges */
1119 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
1120 seq_printf(s
, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1121 range
->id
, range
->name
,
1122 range
->base
, (range
->base
+ range
->npins
- 1),
1124 (range
->pin_base
+ range
->npins
- 1));
1127 mutex_unlock(&pinctrl_mutex
);
1132 static int pinctrl_devices_show(struct seq_file
*s
, void *what
)
1134 struct pinctrl_dev
*pctldev
;
1136 seq_puts(s
, "name [pinmux] [pinconf]\n");
1138 mutex_lock(&pinctrl_mutex
);
1140 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
1141 seq_printf(s
, "%s ", pctldev
->desc
->name
);
1142 if (pctldev
->desc
->pmxops
)
1143 seq_puts(s
, "yes ");
1146 if (pctldev
->desc
->confops
)
1153 mutex_unlock(&pinctrl_mutex
);
1158 static inline const char *map_type(enum pinctrl_map_type type
)
1160 static const char * const names
[] = {
1168 if (type
>= ARRAY_SIZE(names
))
1174 static int pinctrl_maps_show(struct seq_file
*s
, void *what
)
1176 struct pinctrl_maps
*maps_node
;
1178 struct pinctrl_map
const *map
;
1180 seq_puts(s
, "Pinctrl maps:\n");
1182 mutex_lock(&pinctrl_mutex
);
1184 for_each_maps(maps_node
, i
, map
) {
1185 seq_printf(s
, "device %s\nstate %s\ntype %s (%d)\n",
1186 map
->dev_name
, map
->name
, map_type(map
->type
),
1189 if (map
->type
!= PIN_MAP_TYPE_DUMMY_STATE
)
1190 seq_printf(s
, "controlling device %s\n",
1191 map
->ctrl_dev_name
);
1193 switch (map
->type
) {
1194 case PIN_MAP_TYPE_MUX_GROUP
:
1195 pinmux_show_map(s
, map
);
1197 case PIN_MAP_TYPE_CONFIGS_PIN
:
1198 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1199 pinconf_show_map(s
, map
);
1205 seq_printf(s
, "\n");
1208 mutex_unlock(&pinctrl_mutex
);
1213 static int pinctrl_show(struct seq_file
*s
, void *what
)
1216 struct pinctrl_state
*state
;
1217 struct pinctrl_setting
*setting
;
1219 seq_puts(s
, "Requested pin control handlers their pinmux maps:\n");
1221 mutex_lock(&pinctrl_mutex
);
1223 list_for_each_entry(p
, &pinctrl_list
, node
) {
1224 seq_printf(s
, "device: %s current state: %s\n",
1226 p
->state
? p
->state
->name
: "none");
1228 list_for_each_entry(state
, &p
->states
, node
) {
1229 seq_printf(s
, " state: %s\n", state
->name
);
1231 list_for_each_entry(setting
, &state
->settings
, node
) {
1232 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
1234 seq_printf(s
, " type: %s controller %s ",
1235 map_type(setting
->type
),
1236 pinctrl_dev_get_name(pctldev
));
1238 switch (setting
->type
) {
1239 case PIN_MAP_TYPE_MUX_GROUP
:
1240 pinmux_show_setting(s
, setting
);
1242 case PIN_MAP_TYPE_CONFIGS_PIN
:
1243 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1244 pinconf_show_setting(s
, setting
);
1253 mutex_unlock(&pinctrl_mutex
);
1258 static int pinctrl_pins_open(struct inode
*inode
, struct file
*file
)
1260 return single_open(file
, pinctrl_pins_show
, inode
->i_private
);
1263 static int pinctrl_groups_open(struct inode
*inode
, struct file
*file
)
1265 return single_open(file
, pinctrl_groups_show
, inode
->i_private
);
1268 static int pinctrl_gpioranges_open(struct inode
*inode
, struct file
*file
)
1270 return single_open(file
, pinctrl_gpioranges_show
, inode
->i_private
);
1273 static int pinctrl_devices_open(struct inode
*inode
, struct file
*file
)
1275 return single_open(file
, pinctrl_devices_show
, NULL
);
1278 static int pinctrl_maps_open(struct inode
*inode
, struct file
*file
)
1280 return single_open(file
, pinctrl_maps_show
, NULL
);
1283 static int pinctrl_open(struct inode
*inode
, struct file
*file
)
1285 return single_open(file
, pinctrl_show
, NULL
);
1288 static const struct file_operations pinctrl_pins_ops
= {
1289 .open
= pinctrl_pins_open
,
1291 .llseek
= seq_lseek
,
1292 .release
= single_release
,
1295 static const struct file_operations pinctrl_groups_ops
= {
1296 .open
= pinctrl_groups_open
,
1298 .llseek
= seq_lseek
,
1299 .release
= single_release
,
1302 static const struct file_operations pinctrl_gpioranges_ops
= {
1303 .open
= pinctrl_gpioranges_open
,
1305 .llseek
= seq_lseek
,
1306 .release
= single_release
,
1309 static const struct file_operations pinctrl_devices_ops
= {
1310 .open
= pinctrl_devices_open
,
1312 .llseek
= seq_lseek
,
1313 .release
= single_release
,
1316 static const struct file_operations pinctrl_maps_ops
= {
1317 .open
= pinctrl_maps_open
,
1319 .llseek
= seq_lseek
,
1320 .release
= single_release
,
1323 static const struct file_operations pinctrl_ops
= {
1324 .open
= pinctrl_open
,
1326 .llseek
= seq_lseek
,
1327 .release
= single_release
,
1330 static struct dentry
*debugfs_root
;
1332 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1334 struct dentry
*device_root
;
1336 device_root
= debugfs_create_dir(dev_name(pctldev
->dev
),
1338 pctldev
->device_root
= device_root
;
1340 if (IS_ERR(device_root
) || !device_root
) {
1341 pr_warn("failed to create debugfs directory for %s\n",
1342 dev_name(pctldev
->dev
));
1345 debugfs_create_file("pins", S_IFREG
| S_IRUGO
,
1346 device_root
, pctldev
, &pinctrl_pins_ops
);
1347 debugfs_create_file("pingroups", S_IFREG
| S_IRUGO
,
1348 device_root
, pctldev
, &pinctrl_groups_ops
);
1349 debugfs_create_file("gpio-ranges", S_IFREG
| S_IRUGO
,
1350 device_root
, pctldev
, &pinctrl_gpioranges_ops
);
1351 pinmux_init_device_debugfs(device_root
, pctldev
);
1352 pinconf_init_device_debugfs(device_root
, pctldev
);
1355 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1357 debugfs_remove_recursive(pctldev
->device_root
);
1360 static void pinctrl_init_debugfs(void)
1362 debugfs_root
= debugfs_create_dir("pinctrl", NULL
);
1363 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
1364 pr_warn("failed to create debugfs directory\n");
1365 debugfs_root
= NULL
;
1369 debugfs_create_file("pinctrl-devices", S_IFREG
| S_IRUGO
,
1370 debugfs_root
, NULL
, &pinctrl_devices_ops
);
1371 debugfs_create_file("pinctrl-maps", S_IFREG
| S_IRUGO
,
1372 debugfs_root
, NULL
, &pinctrl_maps_ops
);
1373 debugfs_create_file("pinctrl-handles", S_IFREG
| S_IRUGO
,
1374 debugfs_root
, NULL
, &pinctrl_ops
);
1377 #else /* CONFIG_DEBUG_FS */
1379 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1383 static void pinctrl_init_debugfs(void)
1387 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1393 static int pinctrl_check_ops(struct pinctrl_dev
*pctldev
)
1395 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1398 !ops
->get_groups_count
||
1399 !ops
->get_group_name
||
1400 !ops
->get_group_pins
)
1403 if (ops
->dt_node_to_map
&& !ops
->dt_free_map
)
1410 * pinctrl_register() - register a pin controller device
1411 * @pctldesc: descriptor for this pin controller
1412 * @dev: parent device for this pin controller
1413 * @driver_data: private pin controller data for this pin controller
1415 struct pinctrl_dev
*pinctrl_register(struct pinctrl_desc
*pctldesc
,
1416 struct device
*dev
, void *driver_data
)
1418 struct pinctrl_dev
*pctldev
;
1423 if (!pctldesc
->name
)
1426 pctldev
= kzalloc(sizeof(*pctldev
), GFP_KERNEL
);
1427 if (pctldev
== NULL
) {
1428 dev_err(dev
, "failed to alloc struct pinctrl_dev\n");
1432 /* Initialize pin control device struct */
1433 pctldev
->owner
= pctldesc
->owner
;
1434 pctldev
->desc
= pctldesc
;
1435 pctldev
->driver_data
= driver_data
;
1436 INIT_RADIX_TREE(&pctldev
->pin_desc_tree
, GFP_KERNEL
);
1437 INIT_LIST_HEAD(&pctldev
->gpio_ranges
);
1440 /* check core ops for sanity */
1441 if (pinctrl_check_ops(pctldev
)) {
1442 dev_err(dev
, "pinctrl ops lacks necessary functions\n");
1446 /* If we're implementing pinmuxing, check the ops for sanity */
1447 if (pctldesc
->pmxops
) {
1448 if (pinmux_check_ops(pctldev
))
1452 /* If we're implementing pinconfig, check the ops for sanity */
1453 if (pctldesc
->confops
) {
1454 if (pinconf_check_ops(pctldev
))
1458 /* Register all the pins */
1459 dev_dbg(dev
, "try to register %d pins ...\n", pctldesc
->npins
);
1460 ret
= pinctrl_register_pins(pctldev
, pctldesc
->pins
, pctldesc
->npins
);
1462 dev_err(dev
, "error during pin registration\n");
1463 pinctrl_free_pindescs(pctldev
, pctldesc
->pins
,
1468 mutex_lock(&pinctrl_mutex
);
1470 list_add_tail(&pctldev
->node
, &pinctrldev_list
);
1472 pctldev
->p
= pinctrl_get_locked(pctldev
->dev
);
1473 if (!IS_ERR(pctldev
->p
)) {
1474 struct pinctrl_state
*s
=
1475 pinctrl_lookup_state_locked(pctldev
->p
,
1476 PINCTRL_STATE_DEFAULT
);
1478 dev_dbg(dev
, "failed to lookup the default state\n");
1480 if (pinctrl_select_state_locked(pctldev
->p
, s
))
1482 "failed to select default state\n");
1486 mutex_unlock(&pinctrl_mutex
);
1488 pinctrl_init_device_debugfs(pctldev
);
1496 EXPORT_SYMBOL_GPL(pinctrl_register
);
1499 * pinctrl_unregister() - unregister pinmux
1500 * @pctldev: pin controller to unregister
1502 * Called by pinmux drivers to unregister a pinmux.
1504 void pinctrl_unregister(struct pinctrl_dev
*pctldev
)
1506 struct pinctrl_gpio_range
*range
, *n
;
1507 if (pctldev
== NULL
)
1510 pinctrl_remove_device_debugfs(pctldev
);
1512 mutex_lock(&pinctrl_mutex
);
1514 if (!IS_ERR(pctldev
->p
))
1515 pinctrl_put_locked(pctldev
->p
, true);
1517 /* TODO: check that no pinmuxes are still active? */
1518 list_del(&pctldev
->node
);
1519 /* Destroy descriptor tree */
1520 pinctrl_free_pindescs(pctldev
, pctldev
->desc
->pins
,
1521 pctldev
->desc
->npins
);
1522 /* remove gpio ranges map */
1523 list_for_each_entry_safe(range
, n
, &pctldev
->gpio_ranges
, node
)
1524 list_del(&range
->node
);
1528 mutex_unlock(&pinctrl_mutex
);
1530 EXPORT_SYMBOL_GPL(pinctrl_unregister
);
1532 static int __init
pinctrl_init(void)
1534 pr_info("initialized pinctrl subsystem\n");
1535 pinctrl_init_debugfs();
1539 /* init early since many drivers really need to initialized pinmux early */
1540 core_initcall(pinctrl_init
);