2 * Core driver for the pin control subsystem
4 * Copyright (C) 2011-2012 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core
8 * Author: Linus Walleij <linus.walleij@linaro.org>
10 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
12 * License terms: GNU General Public License (GPL) version 2
14 #define pr_fmt(fmt) "pinctrl core: " fmt
16 #include <linux/kernel.h>
17 #include <linux/kref.h>
18 #include <linux/export.h>
19 #include <linux/init.h>
20 #include <linux/device.h>
21 #include <linux/slab.h>
22 #include <linux/err.h>
23 #include <linux/list.h>
24 #include <linux/sysfs.h>
25 #include <linux/debugfs.h>
26 #include <linux/seq_file.h>
27 #include <linux/pinctrl/consumer.h>
28 #include <linux/pinctrl/pinctrl.h>
29 #include <linux/pinctrl/machine.h>
32 #include <asm-generic/gpio.h>
36 #include "devicetree.h"
41 static bool pinctrl_dummy_state
;
43 /* Mutex taken to protect pinctrl_list */
44 static DEFINE_MUTEX(pinctrl_list_mutex
);
46 /* Mutex taken to protect pinctrl_maps */
47 DEFINE_MUTEX(pinctrl_maps_mutex
);
49 /* Mutex taken to protect pinctrldev_list */
50 static DEFINE_MUTEX(pinctrldev_list_mutex
);
52 /* Global list of pin control devices (struct pinctrl_dev) */
53 static LIST_HEAD(pinctrldev_list
);
55 /* List of pin controller handles (struct pinctrl) */
56 static LIST_HEAD(pinctrl_list
);
58 /* List of pinctrl maps (struct pinctrl_maps) */
59 LIST_HEAD(pinctrl_maps
);
63 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
65 * Usually this function is called by platforms without pinctrl driver support
66 * but run with some shared drivers using pinctrl APIs.
67 * After calling this function, the pinctrl core will return successfully
68 * with creating a dummy state for the driver to keep going smoothly.
70 void pinctrl_provide_dummies(void)
72 pinctrl_dummy_state
= true;
75 const char *pinctrl_dev_get_name(struct pinctrl_dev
*pctldev
)
77 /* We're not allowed to register devices without name */
78 return pctldev
->desc
->name
;
80 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name
);
82 const char *pinctrl_dev_get_devname(struct pinctrl_dev
*pctldev
)
84 return dev_name(pctldev
->dev
);
86 EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname
);
88 void *pinctrl_dev_get_drvdata(struct pinctrl_dev
*pctldev
)
90 return pctldev
->driver_data
;
92 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata
);
95 * get_pinctrl_dev_from_devname() - look up pin controller device
96 * @devname: the name of a device instance, as returned by dev_name()
98 * Looks up a pin control device matching a certain device name or pure device
99 * pointer, the pure device pointer will take precedence.
101 struct pinctrl_dev
*get_pinctrl_dev_from_devname(const char *devname
)
103 struct pinctrl_dev
*pctldev
= NULL
;
108 mutex_lock(&pinctrldev_list_mutex
);
110 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
111 if (!strcmp(dev_name(pctldev
->dev
), devname
)) {
112 /* Matched on device name */
113 mutex_unlock(&pinctrldev_list_mutex
);
118 mutex_unlock(&pinctrldev_list_mutex
);
123 struct pinctrl_dev
*get_pinctrl_dev_from_of_node(struct device_node
*np
)
125 struct pinctrl_dev
*pctldev
;
127 mutex_lock(&pinctrldev_list_mutex
);
129 list_for_each_entry(pctldev
, &pinctrldev_list
, node
)
130 if (pctldev
->dev
->of_node
== np
) {
131 mutex_unlock(&pinctrldev_list_mutex
);
135 mutex_unlock(&pinctrldev_list_mutex
);
141 * pin_get_from_name() - look up a pin number from a name
142 * @pctldev: the pin control device to lookup the pin on
143 * @name: the name of the pin to look up
145 int pin_get_from_name(struct pinctrl_dev
*pctldev
, const char *name
)
149 /* The pin number can be retrived from the pin controller descriptor */
150 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
151 struct pin_desc
*desc
;
153 pin
= pctldev
->desc
->pins
[i
].number
;
154 desc
= pin_desc_get(pctldev
, pin
);
155 /* Pin space may be sparse */
156 if (desc
&& !strcmp(name
, desc
->name
))
164 * pin_get_name_from_id() - look up a pin name from a pin id
165 * @pctldev: the pin control device to lookup the pin on
166 * @name: the name of the pin to look up
168 const char *pin_get_name(struct pinctrl_dev
*pctldev
, const unsigned pin
)
170 const struct pin_desc
*desc
;
172 desc
= pin_desc_get(pctldev
, pin
);
174 dev_err(pctldev
->dev
, "failed to get pin(%d) name\n",
183 * pin_is_valid() - check if pin exists on controller
184 * @pctldev: the pin control device to check the pin on
185 * @pin: pin to check, use the local pin controller index number
187 * This tells us whether a certain pin exist on a certain pin controller or
188 * not. Pin lists may be sparse, so some pins may not exist.
190 bool pin_is_valid(struct pinctrl_dev
*pctldev
, int pin
)
192 struct pin_desc
*pindesc
;
197 mutex_lock(&pctldev
->mutex
);
198 pindesc
= pin_desc_get(pctldev
, pin
);
199 mutex_unlock(&pctldev
->mutex
);
201 return pindesc
!= NULL
;
203 EXPORT_SYMBOL_GPL(pin_is_valid
);
205 /* Deletes a range of pin descriptors */
206 static void pinctrl_free_pindescs(struct pinctrl_dev
*pctldev
,
207 const struct pinctrl_pin_desc
*pins
,
212 for (i
= 0; i
< num_pins
; i
++) {
213 struct pin_desc
*pindesc
;
215 pindesc
= radix_tree_lookup(&pctldev
->pin_desc_tree
,
217 if (pindesc
!= NULL
) {
218 radix_tree_delete(&pctldev
->pin_desc_tree
,
220 if (pindesc
->dynamic_name
)
221 kfree(pindesc
->name
);
227 static int pinctrl_register_one_pin(struct pinctrl_dev
*pctldev
,
228 unsigned number
, const char *name
)
230 struct pin_desc
*pindesc
;
232 pindesc
= pin_desc_get(pctldev
, number
);
233 if (pindesc
!= NULL
) {
234 dev_err(pctldev
->dev
, "pin %d already registered\n", number
);
238 pindesc
= kzalloc(sizeof(*pindesc
), GFP_KERNEL
);
239 if (pindesc
== NULL
) {
240 dev_err(pctldev
->dev
, "failed to alloc struct pin_desc\n");
245 pindesc
->pctldev
= pctldev
;
247 /* Copy basic pin info */
249 pindesc
->name
= name
;
251 pindesc
->name
= kasprintf(GFP_KERNEL
, "PIN%u", number
);
252 if (pindesc
->name
== NULL
) {
256 pindesc
->dynamic_name
= true;
259 radix_tree_insert(&pctldev
->pin_desc_tree
, number
, pindesc
);
260 pr_debug("registered pin %d (%s) on %s\n",
261 number
, pindesc
->name
, pctldev
->desc
->name
);
265 static int pinctrl_register_pins(struct pinctrl_dev
*pctldev
,
266 struct pinctrl_pin_desc
const *pins
,
272 for (i
= 0; i
< num_descs
; i
++) {
273 ret
= pinctrl_register_one_pin(pctldev
,
274 pins
[i
].number
, pins
[i
].name
);
283 * gpio_to_pin() - GPIO range GPIO number to pin number translation
284 * @range: GPIO range used for the translation
285 * @gpio: gpio pin to translate to a pin number
287 * Finds the pin number for a given GPIO using the specified GPIO range
288 * as a base for translation. The distinction between linear GPIO ranges
289 * and pin list based GPIO ranges is managed correctly by this function.
291 * This function assumes the gpio is part of the specified GPIO range, use
292 * only after making sure this is the case (e.g. by calling it on the
293 * result of successful pinctrl_get_device_gpio_range calls)!
295 static inline int gpio_to_pin(struct pinctrl_gpio_range
*range
,
298 unsigned int offset
= gpio
- range
->base
;
300 return range
->pins
[offset
];
302 return range
->pin_base
+ offset
;
306 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
307 * @pctldev: pin controller device to check
308 * @gpio: gpio pin to check taken from the global GPIO pin space
310 * Tries to match a GPIO pin number to the ranges handled by a certain pin
311 * controller, return the range or NULL
313 static struct pinctrl_gpio_range
*
314 pinctrl_match_gpio_range(struct pinctrl_dev
*pctldev
, unsigned gpio
)
316 struct pinctrl_gpio_range
*range
= NULL
;
318 mutex_lock(&pctldev
->mutex
);
319 /* Loop over the ranges */
320 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
321 /* Check if we're in the valid range */
322 if (gpio
>= range
->base
&&
323 gpio
< range
->base
+ range
->npins
) {
324 mutex_unlock(&pctldev
->mutex
);
328 mutex_unlock(&pctldev
->mutex
);
333 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
334 * the same GPIO chip are in range
335 * @gpio: gpio pin to check taken from the global GPIO pin space
337 * This function is complement of pinctrl_match_gpio_range(). If the return
338 * value of pinctrl_match_gpio_range() is NULL, this function could be used
339 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
340 * of the same GPIO chip don't have back-end pinctrl interface.
341 * If the return value is true, it means that pinctrl device is ready & the
342 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
343 * is false, it means that pinctrl device may not be ready.
345 #ifdef CONFIG_GPIOLIB
346 static bool pinctrl_ready_for_gpio_range(unsigned gpio
)
348 struct pinctrl_dev
*pctldev
;
349 struct pinctrl_gpio_range
*range
= NULL
;
350 struct gpio_chip
*chip
= gpio_to_chip(gpio
);
352 mutex_lock(&pinctrldev_list_mutex
);
354 /* Loop over the pin controllers */
355 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
356 /* Loop over the ranges */
357 mutex_lock(&pctldev
->mutex
);
358 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
359 /* Check if any gpio range overlapped with gpio chip */
360 if (range
->base
+ range
->npins
- 1 < chip
->base
||
361 range
->base
> chip
->base
+ chip
->ngpio
- 1)
363 mutex_unlock(&pctldev
->mutex
);
364 mutex_unlock(&pinctrldev_list_mutex
);
367 mutex_unlock(&pctldev
->mutex
);
370 mutex_unlock(&pinctrldev_list_mutex
);
375 static bool pinctrl_ready_for_gpio_range(unsigned gpio
) { return true; }
379 * pinctrl_get_device_gpio_range() - find device for GPIO range
380 * @gpio: the pin to locate the pin controller for
381 * @outdev: the pin control device if found
382 * @outrange: the GPIO range if found
384 * Find the pin controller handling a certain GPIO pin from the pinspace of
385 * the GPIO subsystem, return the device and the matching GPIO range. Returns
386 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
387 * may still have not been registered.
389 static int pinctrl_get_device_gpio_range(unsigned gpio
,
390 struct pinctrl_dev
**outdev
,
391 struct pinctrl_gpio_range
**outrange
)
393 struct pinctrl_dev
*pctldev
= NULL
;
395 mutex_lock(&pinctrldev_list_mutex
);
397 /* Loop over the pin controllers */
398 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
399 struct pinctrl_gpio_range
*range
;
401 range
= pinctrl_match_gpio_range(pctldev
, gpio
);
405 mutex_unlock(&pinctrldev_list_mutex
);
410 mutex_unlock(&pinctrldev_list_mutex
);
412 return -EPROBE_DEFER
;
416 * pinctrl_add_gpio_range() - register a GPIO range for a controller
417 * @pctldev: pin controller device to add the range to
418 * @range: the GPIO range to add
420 * This adds a range of GPIOs to be handled by a certain pin controller. Call
421 * this to register handled ranges after registering your pin controller.
423 void pinctrl_add_gpio_range(struct pinctrl_dev
*pctldev
,
424 struct pinctrl_gpio_range
*range
)
426 mutex_lock(&pctldev
->mutex
);
427 list_add_tail(&range
->node
, &pctldev
->gpio_ranges
);
428 mutex_unlock(&pctldev
->mutex
);
430 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range
);
432 void pinctrl_add_gpio_ranges(struct pinctrl_dev
*pctldev
,
433 struct pinctrl_gpio_range
*ranges
,
438 for (i
= 0; i
< nranges
; i
++)
439 pinctrl_add_gpio_range(pctldev
, &ranges
[i
]);
441 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges
);
443 struct pinctrl_dev
*pinctrl_find_and_add_gpio_range(const char *devname
,
444 struct pinctrl_gpio_range
*range
)
446 struct pinctrl_dev
*pctldev
;
448 pctldev
= get_pinctrl_dev_from_devname(devname
);
451 * If we can't find this device, let's assume that is because
452 * it has not probed yet, so the driver trying to register this
453 * range need to defer probing.
456 return ERR_PTR(-EPROBE_DEFER
);
458 pinctrl_add_gpio_range(pctldev
, range
);
462 EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range
);
464 int pinctrl_get_group_pins(struct pinctrl_dev
*pctldev
, const char *pin_group
,
465 const unsigned **pins
, unsigned *num_pins
)
467 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
470 if (!pctlops
->get_group_pins
)
473 gs
= pinctrl_get_group_selector(pctldev
, pin_group
);
477 return pctlops
->get_group_pins(pctldev
, gs
, pins
, num_pins
);
479 EXPORT_SYMBOL_GPL(pinctrl_get_group_pins
);
482 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
483 * @pctldev: the pin controller device to look in
484 * @pin: a controller-local number to find the range for
486 struct pinctrl_gpio_range
*
487 pinctrl_find_gpio_range_from_pin(struct pinctrl_dev
*pctldev
,
490 struct pinctrl_gpio_range
*range
;
492 mutex_lock(&pctldev
->mutex
);
493 /* Loop over the ranges */
494 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
495 /* Check if we're in the valid range */
498 for (a
= 0; a
< range
->npins
; a
++) {
499 if (range
->pins
[a
] == pin
)
502 } else if (pin
>= range
->pin_base
&&
503 pin
< range
->pin_base
+ range
->npins
)
508 mutex_unlock(&pctldev
->mutex
);
511 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin
);
514 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
515 * @pctldev: pin controller device to remove the range from
516 * @range: the GPIO range to remove
518 void pinctrl_remove_gpio_range(struct pinctrl_dev
*pctldev
,
519 struct pinctrl_gpio_range
*range
)
521 mutex_lock(&pctldev
->mutex
);
522 list_del(&range
->node
);
523 mutex_unlock(&pctldev
->mutex
);
525 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range
);
528 * pinctrl_get_group_selector() - returns the group selector for a group
529 * @pctldev: the pin controller handling the group
530 * @pin_group: the pin group to look up
532 int pinctrl_get_group_selector(struct pinctrl_dev
*pctldev
,
533 const char *pin_group
)
535 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
536 unsigned ngroups
= pctlops
->get_groups_count(pctldev
);
537 unsigned group_selector
= 0;
539 while (group_selector
< ngroups
) {
540 const char *gname
= pctlops
->get_group_name(pctldev
,
542 if (!strcmp(gname
, pin_group
)) {
543 dev_dbg(pctldev
->dev
,
544 "found group selector %u for %s\n",
547 return group_selector
;
553 dev_err(pctldev
->dev
, "does not have pin group %s\n",
560 * pinctrl_request_gpio() - request a single pin to be used as GPIO
561 * @gpio: the GPIO pin number from the GPIO subsystem number space
563 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
564 * as part of their gpio_request() semantics, platforms and individual drivers
565 * shall *NOT* request GPIO pins to be muxed in.
567 int pinctrl_request_gpio(unsigned gpio
)
569 struct pinctrl_dev
*pctldev
;
570 struct pinctrl_gpio_range
*range
;
574 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
576 if (pinctrl_ready_for_gpio_range(gpio
))
581 mutex_lock(&pctldev
->mutex
);
583 /* Convert to the pin controllers number space */
584 pin
= gpio_to_pin(range
, gpio
);
586 ret
= pinmux_request_gpio(pctldev
, range
, pin
, gpio
);
588 mutex_unlock(&pctldev
->mutex
);
592 EXPORT_SYMBOL_GPL(pinctrl_request_gpio
);
595 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
596 * @gpio: the GPIO pin number from the GPIO subsystem number space
598 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
599 * as part of their gpio_free() semantics, platforms and individual drivers
600 * shall *NOT* request GPIO pins to be muxed out.
602 void pinctrl_free_gpio(unsigned gpio
)
604 struct pinctrl_dev
*pctldev
;
605 struct pinctrl_gpio_range
*range
;
609 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
613 mutex_lock(&pctldev
->mutex
);
615 /* Convert to the pin controllers number space */
616 pin
= gpio_to_pin(range
, gpio
);
618 pinmux_free_gpio(pctldev
, pin
, range
);
620 mutex_unlock(&pctldev
->mutex
);
622 EXPORT_SYMBOL_GPL(pinctrl_free_gpio
);
624 static int pinctrl_gpio_direction(unsigned gpio
, bool input
)
626 struct pinctrl_dev
*pctldev
;
627 struct pinctrl_gpio_range
*range
;
631 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
636 mutex_lock(&pctldev
->mutex
);
638 /* Convert to the pin controllers number space */
639 pin
= gpio_to_pin(range
, gpio
);
640 ret
= pinmux_gpio_direction(pctldev
, range
, pin
, input
);
642 mutex_unlock(&pctldev
->mutex
);
648 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
649 * @gpio: the GPIO pin number from the GPIO subsystem number space
651 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
652 * as part of their gpio_direction_input() semantics, platforms and individual
653 * drivers shall *NOT* touch pin control GPIO calls.
655 int pinctrl_gpio_direction_input(unsigned gpio
)
657 return pinctrl_gpio_direction(gpio
, true);
659 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input
);
662 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
663 * @gpio: the GPIO pin number from the GPIO subsystem number space
665 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
666 * as part of their gpio_direction_output() semantics, platforms and individual
667 * drivers shall *NOT* touch pin control GPIO calls.
669 int pinctrl_gpio_direction_output(unsigned gpio
)
671 return pinctrl_gpio_direction(gpio
, false);
673 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output
);
675 static struct pinctrl_state
*find_state(struct pinctrl
*p
,
678 struct pinctrl_state
*state
;
680 list_for_each_entry(state
, &p
->states
, node
)
681 if (!strcmp(state
->name
, name
))
687 static struct pinctrl_state
*create_state(struct pinctrl
*p
,
690 struct pinctrl_state
*state
;
692 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
695 "failed to alloc struct pinctrl_state\n");
696 return ERR_PTR(-ENOMEM
);
700 INIT_LIST_HEAD(&state
->settings
);
702 list_add_tail(&state
->node
, &p
->states
);
707 static int add_setting(struct pinctrl
*p
, struct pinctrl_map
const *map
)
709 struct pinctrl_state
*state
;
710 struct pinctrl_setting
*setting
;
713 state
= find_state(p
, map
->name
);
715 state
= create_state(p
, map
->name
);
717 return PTR_ERR(state
);
719 if (map
->type
== PIN_MAP_TYPE_DUMMY_STATE
)
722 setting
= kzalloc(sizeof(*setting
), GFP_KERNEL
);
723 if (setting
== NULL
) {
725 "failed to alloc struct pinctrl_setting\n");
729 setting
->type
= map
->type
;
731 setting
->pctldev
= get_pinctrl_dev_from_devname(map
->ctrl_dev_name
);
732 if (setting
->pctldev
== NULL
) {
734 /* Do not defer probing of hogs (circular loop) */
735 if (!strcmp(map
->ctrl_dev_name
, map
->dev_name
))
738 * OK let us guess that the driver is not there yet, and
739 * let's defer obtaining this pinctrl handle to later...
741 dev_info(p
->dev
, "unknown pinctrl device %s in map entry, deferring probe",
743 return -EPROBE_DEFER
;
746 setting
->dev_name
= map
->dev_name
;
749 case PIN_MAP_TYPE_MUX_GROUP
:
750 ret
= pinmux_map_to_setting(map
, setting
);
752 case PIN_MAP_TYPE_CONFIGS_PIN
:
753 case PIN_MAP_TYPE_CONFIGS_GROUP
:
754 ret
= pinconf_map_to_setting(map
, setting
);
765 list_add_tail(&setting
->node
, &state
->settings
);
770 static struct pinctrl
*find_pinctrl(struct device
*dev
)
774 mutex_lock(&pinctrl_list_mutex
);
775 list_for_each_entry(p
, &pinctrl_list
, node
)
777 mutex_unlock(&pinctrl_list_mutex
);
781 mutex_unlock(&pinctrl_list_mutex
);
785 static void pinctrl_free(struct pinctrl
*p
, bool inlist
);
787 static struct pinctrl
*create_pinctrl(struct device
*dev
)
791 struct pinctrl_maps
*maps_node
;
793 struct pinctrl_map
const *map
;
797 * create the state cookie holder struct pinctrl for each
798 * mapping, this is what consumers will get when requesting
799 * a pin control handle with pinctrl_get()
801 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
803 dev_err(dev
, "failed to alloc struct pinctrl\n");
804 return ERR_PTR(-ENOMEM
);
807 INIT_LIST_HEAD(&p
->states
);
808 INIT_LIST_HEAD(&p
->dt_maps
);
810 ret
= pinctrl_dt_to_map(p
);
816 devname
= dev_name(dev
);
818 mutex_lock(&pinctrl_maps_mutex
);
819 /* Iterate over the pin control maps to locate the right ones */
820 for_each_maps(maps_node
, i
, map
) {
821 /* Map must be for this device */
822 if (strcmp(map
->dev_name
, devname
))
825 ret
= add_setting(p
, map
);
827 * At this point the adding of a setting may:
829 * - Defer, if the pinctrl device is not yet available
830 * - Fail, if the pinctrl device is not yet available,
831 * AND the setting is a hog. We cannot defer that, since
832 * the hog will kick in immediately after the device
835 * If the error returned was not -EPROBE_DEFER then we
836 * accumulate the errors to see if we end up with
837 * an -EPROBE_DEFER later, as that is the worst case.
839 if (ret
== -EPROBE_DEFER
) {
840 pinctrl_free(p
, false);
841 mutex_unlock(&pinctrl_maps_mutex
);
845 mutex_unlock(&pinctrl_maps_mutex
);
848 /* If some other error than deferral occured, return here */
849 pinctrl_free(p
, false);
853 kref_init(&p
->users
);
855 /* Add the pinctrl handle to the global list */
856 mutex_lock(&pinctrl_list_mutex
);
857 list_add_tail(&p
->node
, &pinctrl_list
);
858 mutex_unlock(&pinctrl_list_mutex
);
864 * pinctrl_get() - retrieves the pinctrl handle for a device
865 * @dev: the device to obtain the handle for
867 struct pinctrl
*pinctrl_get(struct device
*dev
)
872 return ERR_PTR(-EINVAL
);
875 * See if somebody else (such as the device core) has already
876 * obtained a handle to the pinctrl for this device. In that case,
877 * return another pointer to it.
879 p
= find_pinctrl(dev
);
881 dev_dbg(dev
, "obtain a copy of previously claimed pinctrl\n");
886 return create_pinctrl(dev
);
888 EXPORT_SYMBOL_GPL(pinctrl_get
);
890 static void pinctrl_free_setting(bool disable_setting
,
891 struct pinctrl_setting
*setting
)
893 switch (setting
->type
) {
894 case PIN_MAP_TYPE_MUX_GROUP
:
896 pinmux_disable_setting(setting
);
897 pinmux_free_setting(setting
);
899 case PIN_MAP_TYPE_CONFIGS_PIN
:
900 case PIN_MAP_TYPE_CONFIGS_GROUP
:
901 pinconf_free_setting(setting
);
908 static void pinctrl_free(struct pinctrl
*p
, bool inlist
)
910 struct pinctrl_state
*state
, *n1
;
911 struct pinctrl_setting
*setting
, *n2
;
913 mutex_lock(&pinctrl_list_mutex
);
914 list_for_each_entry_safe(state
, n1
, &p
->states
, node
) {
915 list_for_each_entry_safe(setting
, n2
, &state
->settings
, node
) {
916 pinctrl_free_setting(state
== p
->state
, setting
);
917 list_del(&setting
->node
);
920 list_del(&state
->node
);
924 pinctrl_dt_free_maps(p
);
929 mutex_unlock(&pinctrl_list_mutex
);
933 * pinctrl_release() - release the pinctrl handle
934 * @kref: the kref in the pinctrl being released
936 static void pinctrl_release(struct kref
*kref
)
938 struct pinctrl
*p
= container_of(kref
, struct pinctrl
, users
);
940 pinctrl_free(p
, true);
944 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
945 * @p: the pinctrl handle to release
947 void pinctrl_put(struct pinctrl
*p
)
949 kref_put(&p
->users
, pinctrl_release
);
951 EXPORT_SYMBOL_GPL(pinctrl_put
);
954 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
955 * @p: the pinctrl handle to retrieve the state from
956 * @name: the state name to retrieve
958 struct pinctrl_state
*pinctrl_lookup_state(struct pinctrl
*p
,
961 struct pinctrl_state
*state
;
963 state
= find_state(p
, name
);
965 if (pinctrl_dummy_state
) {
966 /* create dummy state */
967 dev_dbg(p
->dev
, "using pinctrl dummy state (%s)\n",
969 state
= create_state(p
, name
);
971 state
= ERR_PTR(-ENODEV
);
976 EXPORT_SYMBOL_GPL(pinctrl_lookup_state
);
979 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
980 * @p: the pinctrl handle for the device that requests configuration
981 * @state: the state handle to select/activate/program
983 int pinctrl_select_state(struct pinctrl
*p
, struct pinctrl_state
*state
)
985 struct pinctrl_setting
*setting
, *setting2
;
986 struct pinctrl_state
*old_state
= p
->state
;
989 if (p
->state
== state
)
994 * For each pinmux setting in the old state, forget SW's record
995 * of mux owner for that pingroup. Any pingroups which are
996 * still owned by the new state will be re-acquired by the call
997 * to pinmux_enable_setting() in the loop below.
999 list_for_each_entry(setting
, &p
->state
->settings
, node
) {
1000 if (setting
->type
!= PIN_MAP_TYPE_MUX_GROUP
)
1002 pinmux_disable_setting(setting
);
1008 /* Apply all the settings for the new state */
1009 list_for_each_entry(setting
, &state
->settings
, node
) {
1010 switch (setting
->type
) {
1011 case PIN_MAP_TYPE_MUX_GROUP
:
1012 ret
= pinmux_enable_setting(setting
);
1014 case PIN_MAP_TYPE_CONFIGS_PIN
:
1015 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1016 ret
= pinconf_apply_setting(setting
);
1024 goto unapply_new_state
;
1033 dev_err(p
->dev
, "Error applying setting, reverse things back\n");
1035 list_for_each_entry(setting2
, &state
->settings
, node
) {
1036 if (&setting2
->node
== &setting
->node
)
1039 * All we can do here is pinmux_disable_setting.
1040 * That means that some pins are muxed differently now
1041 * than they were before applying the setting (We can't
1042 * "unmux a pin"!), but it's not a big deal since the pins
1043 * are free to be muxed by another apply_setting.
1045 if (setting2
->type
== PIN_MAP_TYPE_MUX_GROUP
)
1046 pinmux_disable_setting(setting2
);
1049 /* There's no infinite recursive loop here because p->state is NULL */
1051 pinctrl_select_state(p
, old_state
);
1055 EXPORT_SYMBOL_GPL(pinctrl_select_state
);
1057 static void devm_pinctrl_release(struct device
*dev
, void *res
)
1059 pinctrl_put(*(struct pinctrl
**)res
);
1063 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1064 * @dev: the device to obtain the handle for
1066 * If there is a need to explicitly destroy the returned struct pinctrl,
1067 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1069 struct pinctrl
*devm_pinctrl_get(struct device
*dev
)
1071 struct pinctrl
**ptr
, *p
;
1073 ptr
= devres_alloc(devm_pinctrl_release
, sizeof(*ptr
), GFP_KERNEL
);
1075 return ERR_PTR(-ENOMEM
);
1077 p
= pinctrl_get(dev
);
1080 devres_add(dev
, ptr
);
1087 EXPORT_SYMBOL_GPL(devm_pinctrl_get
);
1089 static int devm_pinctrl_match(struct device
*dev
, void *res
, void *data
)
1091 struct pinctrl
**p
= res
;
1097 * devm_pinctrl_put() - Resource managed pinctrl_put()
1098 * @p: the pinctrl handle to release
1100 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1101 * this function will not need to be called and the resource management
1102 * code will ensure that the resource is freed.
1104 void devm_pinctrl_put(struct pinctrl
*p
)
1106 WARN_ON(devres_release(p
->dev
, devm_pinctrl_release
,
1107 devm_pinctrl_match
, p
));
1109 EXPORT_SYMBOL_GPL(devm_pinctrl_put
);
1111 int pinctrl_register_map(struct pinctrl_map
const *maps
, unsigned num_maps
,
1115 struct pinctrl_maps
*maps_node
;
1117 pr_debug("add %u pinctrl maps\n", num_maps
);
1119 /* First sanity check the new mapping */
1120 for (i
= 0; i
< num_maps
; i
++) {
1121 if (!maps
[i
].dev_name
) {
1122 pr_err("failed to register map %s (%d): no device given\n",
1127 if (!maps
[i
].name
) {
1128 pr_err("failed to register map %d: no map name given\n",
1133 if (maps
[i
].type
!= PIN_MAP_TYPE_DUMMY_STATE
&&
1134 !maps
[i
].ctrl_dev_name
) {
1135 pr_err("failed to register map %s (%d): no pin control device given\n",
1140 switch (maps
[i
].type
) {
1141 case PIN_MAP_TYPE_DUMMY_STATE
:
1143 case PIN_MAP_TYPE_MUX_GROUP
:
1144 ret
= pinmux_validate_map(&maps
[i
], i
);
1148 case PIN_MAP_TYPE_CONFIGS_PIN
:
1149 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1150 ret
= pinconf_validate_map(&maps
[i
], i
);
1155 pr_err("failed to register map %s (%d): invalid type given\n",
1161 maps_node
= kzalloc(sizeof(*maps_node
), GFP_KERNEL
);
1163 pr_err("failed to alloc struct pinctrl_maps\n");
1167 maps_node
->num_maps
= num_maps
;
1169 maps_node
->maps
= kmemdup(maps
, sizeof(*maps
) * num_maps
,
1171 if (!maps_node
->maps
) {
1172 pr_err("failed to duplicate mapping table\n");
1177 maps_node
->maps
= maps
;
1180 mutex_lock(&pinctrl_maps_mutex
);
1181 list_add_tail(&maps_node
->node
, &pinctrl_maps
);
1182 mutex_unlock(&pinctrl_maps_mutex
);
1188 * pinctrl_register_mappings() - register a set of pin controller mappings
1189 * @maps: the pincontrol mappings table to register. This should probably be
1190 * marked with __initdata so it can be discarded after boot. This
1191 * function will perform a shallow copy for the mapping entries.
1192 * @num_maps: the number of maps in the mapping table
1194 int pinctrl_register_mappings(struct pinctrl_map
const *maps
,
1197 return pinctrl_register_map(maps
, num_maps
, true);
1200 void pinctrl_unregister_map(struct pinctrl_map
const *map
)
1202 struct pinctrl_maps
*maps_node
;
1204 mutex_lock(&pinctrl_maps_mutex
);
1205 list_for_each_entry(maps_node
, &pinctrl_maps
, node
) {
1206 if (maps_node
->maps
== map
) {
1207 list_del(&maps_node
->node
);
1209 mutex_unlock(&pinctrl_maps_mutex
);
1213 mutex_unlock(&pinctrl_maps_mutex
);
1217 * pinctrl_force_sleep() - turn a given controller device into sleep state
1218 * @pctldev: pin controller device
1220 int pinctrl_force_sleep(struct pinctrl_dev
*pctldev
)
1222 if (!IS_ERR(pctldev
->p
) && !IS_ERR(pctldev
->hog_sleep
))
1223 return pinctrl_select_state(pctldev
->p
, pctldev
->hog_sleep
);
1226 EXPORT_SYMBOL_GPL(pinctrl_force_sleep
);
1229 * pinctrl_force_default() - turn a given controller device into default state
1230 * @pctldev: pin controller device
1232 int pinctrl_force_default(struct pinctrl_dev
*pctldev
)
1234 if (!IS_ERR(pctldev
->p
) && !IS_ERR(pctldev
->hog_default
))
1235 return pinctrl_select_state(pctldev
->p
, pctldev
->hog_default
);
1238 EXPORT_SYMBOL_GPL(pinctrl_force_default
);
1243 * pinctrl_pm_select_state() - select pinctrl state for PM
1244 * @dev: device to select default state for
1245 * @state: state to set
1247 static int pinctrl_pm_select_state(struct device
*dev
,
1248 struct pinctrl_state
*state
)
1250 struct dev_pin_info
*pins
= dev
->pins
;
1254 return 0; /* No such state */
1255 ret
= pinctrl_select_state(pins
->p
, state
);
1257 dev_err(dev
, "failed to activate pinctrl state %s\n",
1263 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1264 * @dev: device to select default state for
1266 int pinctrl_pm_select_default_state(struct device
*dev
)
1271 return pinctrl_pm_select_state(dev
, dev
->pins
->default_state
);
1273 EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state
);
1276 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1277 * @dev: device to select sleep state for
1279 int pinctrl_pm_select_sleep_state(struct device
*dev
)
1284 return pinctrl_pm_select_state(dev
, dev
->pins
->sleep_state
);
1286 EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state
);
1289 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1290 * @dev: device to select idle state for
1292 int pinctrl_pm_select_idle_state(struct device
*dev
)
1297 return pinctrl_pm_select_state(dev
, dev
->pins
->idle_state
);
1299 EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state
);
1302 #ifdef CONFIG_DEBUG_FS
1304 static int pinctrl_pins_show(struct seq_file
*s
, void *what
)
1306 struct pinctrl_dev
*pctldev
= s
->private;
1307 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1310 seq_printf(s
, "registered pins: %d\n", pctldev
->desc
->npins
);
1312 mutex_lock(&pctldev
->mutex
);
1314 /* The pin number can be retrived from the pin controller descriptor */
1315 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
1316 struct pin_desc
*desc
;
1318 pin
= pctldev
->desc
->pins
[i
].number
;
1319 desc
= pin_desc_get(pctldev
, pin
);
1320 /* Pin space may be sparse */
1324 seq_printf(s
, "pin %d (%s) ", pin
,
1325 desc
->name
? desc
->name
: "unnamed");
1327 /* Driver-specific info per pin */
1328 if (ops
->pin_dbg_show
)
1329 ops
->pin_dbg_show(pctldev
, s
, pin
);
1334 mutex_unlock(&pctldev
->mutex
);
1339 static int pinctrl_groups_show(struct seq_file
*s
, void *what
)
1341 struct pinctrl_dev
*pctldev
= s
->private;
1342 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1343 unsigned ngroups
, selector
= 0;
1345 mutex_lock(&pctldev
->mutex
);
1347 ngroups
= ops
->get_groups_count(pctldev
);
1349 seq_puts(s
, "registered pin groups:\n");
1350 while (selector
< ngroups
) {
1351 const unsigned *pins
= NULL
;
1352 unsigned num_pins
= 0;
1353 const char *gname
= ops
->get_group_name(pctldev
, selector
);
1358 if (ops
->get_group_pins
)
1359 ret
= ops
->get_group_pins(pctldev
, selector
,
1362 seq_printf(s
, "%s [ERROR GETTING PINS]\n",
1365 seq_printf(s
, "group: %s\n", gname
);
1366 for (i
= 0; i
< num_pins
; i
++) {
1367 pname
= pin_get_name(pctldev
, pins
[i
]);
1368 if (WARN_ON(!pname
)) {
1369 mutex_unlock(&pctldev
->mutex
);
1372 seq_printf(s
, "pin %d (%s)\n", pins
[i
], pname
);
1379 mutex_unlock(&pctldev
->mutex
);
1384 static int pinctrl_gpioranges_show(struct seq_file
*s
, void *what
)
1386 struct pinctrl_dev
*pctldev
= s
->private;
1387 struct pinctrl_gpio_range
*range
= NULL
;
1389 seq_puts(s
, "GPIO ranges handled:\n");
1391 mutex_lock(&pctldev
->mutex
);
1393 /* Loop over the ranges */
1394 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
1397 seq_printf(s
, "%u: %s GPIOS [%u - %u] PINS {",
1398 range
->id
, range
->name
,
1399 range
->base
, (range
->base
+ range
->npins
- 1));
1400 for (a
= 0; a
< range
->npins
- 1; a
++)
1401 seq_printf(s
, "%u, ", range
->pins
[a
]);
1402 seq_printf(s
, "%u}\n", range
->pins
[a
]);
1405 seq_printf(s
, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1406 range
->id
, range
->name
,
1407 range
->base
, (range
->base
+ range
->npins
- 1),
1409 (range
->pin_base
+ range
->npins
- 1));
1412 mutex_unlock(&pctldev
->mutex
);
1417 static int pinctrl_devices_show(struct seq_file
*s
, void *what
)
1419 struct pinctrl_dev
*pctldev
;
1421 seq_puts(s
, "name [pinmux] [pinconf]\n");
1423 mutex_lock(&pinctrldev_list_mutex
);
1425 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
1426 seq_printf(s
, "%s ", pctldev
->desc
->name
);
1427 if (pctldev
->desc
->pmxops
)
1428 seq_puts(s
, "yes ");
1431 if (pctldev
->desc
->confops
)
1438 mutex_unlock(&pinctrldev_list_mutex
);
1443 static inline const char *map_type(enum pinctrl_map_type type
)
1445 static const char * const names
[] = {
1453 if (type
>= ARRAY_SIZE(names
))
1459 static int pinctrl_maps_show(struct seq_file
*s
, void *what
)
1461 struct pinctrl_maps
*maps_node
;
1463 struct pinctrl_map
const *map
;
1465 seq_puts(s
, "Pinctrl maps:\n");
1467 mutex_lock(&pinctrl_maps_mutex
);
1468 for_each_maps(maps_node
, i
, map
) {
1469 seq_printf(s
, "device %s\nstate %s\ntype %s (%d)\n",
1470 map
->dev_name
, map
->name
, map_type(map
->type
),
1473 if (map
->type
!= PIN_MAP_TYPE_DUMMY_STATE
)
1474 seq_printf(s
, "controlling device %s\n",
1475 map
->ctrl_dev_name
);
1477 switch (map
->type
) {
1478 case PIN_MAP_TYPE_MUX_GROUP
:
1479 pinmux_show_map(s
, map
);
1481 case PIN_MAP_TYPE_CONFIGS_PIN
:
1482 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1483 pinconf_show_map(s
, map
);
1489 seq_printf(s
, "\n");
1491 mutex_unlock(&pinctrl_maps_mutex
);
1496 static int pinctrl_show(struct seq_file
*s
, void *what
)
1499 struct pinctrl_state
*state
;
1500 struct pinctrl_setting
*setting
;
1502 seq_puts(s
, "Requested pin control handlers their pinmux maps:\n");
1504 mutex_lock(&pinctrl_list_mutex
);
1506 list_for_each_entry(p
, &pinctrl_list
, node
) {
1507 seq_printf(s
, "device: %s current state: %s\n",
1509 p
->state
? p
->state
->name
: "none");
1511 list_for_each_entry(state
, &p
->states
, node
) {
1512 seq_printf(s
, " state: %s\n", state
->name
);
1514 list_for_each_entry(setting
, &state
->settings
, node
) {
1515 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
1517 seq_printf(s
, " type: %s controller %s ",
1518 map_type(setting
->type
),
1519 pinctrl_dev_get_name(pctldev
));
1521 switch (setting
->type
) {
1522 case PIN_MAP_TYPE_MUX_GROUP
:
1523 pinmux_show_setting(s
, setting
);
1525 case PIN_MAP_TYPE_CONFIGS_PIN
:
1526 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1527 pinconf_show_setting(s
, setting
);
1536 mutex_unlock(&pinctrl_list_mutex
);
1541 static int pinctrl_pins_open(struct inode
*inode
, struct file
*file
)
1543 return single_open(file
, pinctrl_pins_show
, inode
->i_private
);
1546 static int pinctrl_groups_open(struct inode
*inode
, struct file
*file
)
1548 return single_open(file
, pinctrl_groups_show
, inode
->i_private
);
1551 static int pinctrl_gpioranges_open(struct inode
*inode
, struct file
*file
)
1553 return single_open(file
, pinctrl_gpioranges_show
, inode
->i_private
);
1556 static int pinctrl_devices_open(struct inode
*inode
, struct file
*file
)
1558 return single_open(file
, pinctrl_devices_show
, NULL
);
1561 static int pinctrl_maps_open(struct inode
*inode
, struct file
*file
)
1563 return single_open(file
, pinctrl_maps_show
, NULL
);
1566 static int pinctrl_open(struct inode
*inode
, struct file
*file
)
1568 return single_open(file
, pinctrl_show
, NULL
);
1571 static const struct file_operations pinctrl_pins_ops
= {
1572 .open
= pinctrl_pins_open
,
1574 .llseek
= seq_lseek
,
1575 .release
= single_release
,
1578 static const struct file_operations pinctrl_groups_ops
= {
1579 .open
= pinctrl_groups_open
,
1581 .llseek
= seq_lseek
,
1582 .release
= single_release
,
1585 static const struct file_operations pinctrl_gpioranges_ops
= {
1586 .open
= pinctrl_gpioranges_open
,
1588 .llseek
= seq_lseek
,
1589 .release
= single_release
,
1592 static const struct file_operations pinctrl_devices_ops
= {
1593 .open
= pinctrl_devices_open
,
1595 .llseek
= seq_lseek
,
1596 .release
= single_release
,
1599 static const struct file_operations pinctrl_maps_ops
= {
1600 .open
= pinctrl_maps_open
,
1602 .llseek
= seq_lseek
,
1603 .release
= single_release
,
1606 static const struct file_operations pinctrl_ops
= {
1607 .open
= pinctrl_open
,
1609 .llseek
= seq_lseek
,
1610 .release
= single_release
,
1613 static struct dentry
*debugfs_root
;
1615 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1617 struct dentry
*device_root
;
1619 device_root
= debugfs_create_dir(dev_name(pctldev
->dev
),
1621 pctldev
->device_root
= device_root
;
1623 if (IS_ERR(device_root
) || !device_root
) {
1624 pr_warn("failed to create debugfs directory for %s\n",
1625 dev_name(pctldev
->dev
));
1628 debugfs_create_file("pins", S_IFREG
| S_IRUGO
,
1629 device_root
, pctldev
, &pinctrl_pins_ops
);
1630 debugfs_create_file("pingroups", S_IFREG
| S_IRUGO
,
1631 device_root
, pctldev
, &pinctrl_groups_ops
);
1632 debugfs_create_file("gpio-ranges", S_IFREG
| S_IRUGO
,
1633 device_root
, pctldev
, &pinctrl_gpioranges_ops
);
1634 if (pctldev
->desc
->pmxops
)
1635 pinmux_init_device_debugfs(device_root
, pctldev
);
1636 if (pctldev
->desc
->confops
)
1637 pinconf_init_device_debugfs(device_root
, pctldev
);
1640 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1642 debugfs_remove_recursive(pctldev
->device_root
);
1645 static void pinctrl_init_debugfs(void)
1647 debugfs_root
= debugfs_create_dir("pinctrl", NULL
);
1648 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
1649 pr_warn("failed to create debugfs directory\n");
1650 debugfs_root
= NULL
;
1654 debugfs_create_file("pinctrl-devices", S_IFREG
| S_IRUGO
,
1655 debugfs_root
, NULL
, &pinctrl_devices_ops
);
1656 debugfs_create_file("pinctrl-maps", S_IFREG
| S_IRUGO
,
1657 debugfs_root
, NULL
, &pinctrl_maps_ops
);
1658 debugfs_create_file("pinctrl-handles", S_IFREG
| S_IRUGO
,
1659 debugfs_root
, NULL
, &pinctrl_ops
);
1662 #else /* CONFIG_DEBUG_FS */
1664 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1668 static void pinctrl_init_debugfs(void)
1672 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1678 static int pinctrl_check_ops(struct pinctrl_dev
*pctldev
)
1680 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1683 !ops
->get_groups_count
||
1684 !ops
->get_group_name
)
1687 if (ops
->dt_node_to_map
&& !ops
->dt_free_map
)
1694 * pinctrl_register() - register a pin controller device
1695 * @pctldesc: descriptor for this pin controller
1696 * @dev: parent device for this pin controller
1697 * @driver_data: private pin controller data for this pin controller
1699 struct pinctrl_dev
*pinctrl_register(struct pinctrl_desc
*pctldesc
,
1700 struct device
*dev
, void *driver_data
)
1702 struct pinctrl_dev
*pctldev
;
1706 return ERR_PTR(-EINVAL
);
1707 if (!pctldesc
->name
)
1708 return ERR_PTR(-EINVAL
);
1710 pctldev
= kzalloc(sizeof(*pctldev
), GFP_KERNEL
);
1711 if (pctldev
== NULL
) {
1712 dev_err(dev
, "failed to alloc struct pinctrl_dev\n");
1713 return ERR_PTR(-ENOMEM
);
1716 /* Initialize pin control device struct */
1717 pctldev
->owner
= pctldesc
->owner
;
1718 pctldev
->desc
= pctldesc
;
1719 pctldev
->driver_data
= driver_data
;
1720 INIT_RADIX_TREE(&pctldev
->pin_desc_tree
, GFP_KERNEL
);
1721 INIT_LIST_HEAD(&pctldev
->gpio_ranges
);
1723 mutex_init(&pctldev
->mutex
);
1725 /* check core ops for sanity */
1726 ret
= pinctrl_check_ops(pctldev
);
1728 dev_err(dev
, "pinctrl ops lacks necessary functions\n");
1732 /* If we're implementing pinmuxing, check the ops for sanity */
1733 if (pctldesc
->pmxops
) {
1734 ret
= pinmux_check_ops(pctldev
);
1739 /* If we're implementing pinconfig, check the ops for sanity */
1740 if (pctldesc
->confops
) {
1741 ret
= pinconf_check_ops(pctldev
);
1746 /* Register all the pins */
1747 dev_dbg(dev
, "try to register %d pins ...\n", pctldesc
->npins
);
1748 ret
= pinctrl_register_pins(pctldev
, pctldesc
->pins
, pctldesc
->npins
);
1750 dev_err(dev
, "error during pin registration\n");
1751 pinctrl_free_pindescs(pctldev
, pctldesc
->pins
,
1756 mutex_lock(&pinctrldev_list_mutex
);
1757 list_add_tail(&pctldev
->node
, &pinctrldev_list
);
1758 mutex_unlock(&pinctrldev_list_mutex
);
1760 pctldev
->p
= pinctrl_get(pctldev
->dev
);
1762 if (!IS_ERR(pctldev
->p
)) {
1763 pctldev
->hog_default
=
1764 pinctrl_lookup_state(pctldev
->p
, PINCTRL_STATE_DEFAULT
);
1765 if (IS_ERR(pctldev
->hog_default
)) {
1766 dev_dbg(dev
, "failed to lookup the default state\n");
1768 if (pinctrl_select_state(pctldev
->p
,
1769 pctldev
->hog_default
))
1771 "failed to select default state\n");
1774 pctldev
->hog_sleep
=
1775 pinctrl_lookup_state(pctldev
->p
,
1776 PINCTRL_STATE_SLEEP
);
1777 if (IS_ERR(pctldev
->hog_sleep
))
1778 dev_dbg(dev
, "failed to lookup the sleep state\n");
1781 pinctrl_init_device_debugfs(pctldev
);
1786 mutex_destroy(&pctldev
->mutex
);
1788 return ERR_PTR(ret
);
1790 EXPORT_SYMBOL_GPL(pinctrl_register
);
1793 * pinctrl_unregister() - unregister pinmux
1794 * @pctldev: pin controller to unregister
1796 * Called by pinmux drivers to unregister a pinmux.
1798 void pinctrl_unregister(struct pinctrl_dev
*pctldev
)
1800 struct pinctrl_gpio_range
*range
, *n
;
1801 if (pctldev
== NULL
)
1804 mutex_lock(&pctldev
->mutex
);
1805 pinctrl_remove_device_debugfs(pctldev
);
1806 mutex_unlock(&pctldev
->mutex
);
1808 if (!IS_ERR(pctldev
->p
))
1809 pinctrl_put(pctldev
->p
);
1811 mutex_lock(&pinctrldev_list_mutex
);
1812 mutex_lock(&pctldev
->mutex
);
1813 /* TODO: check that no pinmuxes are still active? */
1814 list_del(&pctldev
->node
);
1815 /* Destroy descriptor tree */
1816 pinctrl_free_pindescs(pctldev
, pctldev
->desc
->pins
,
1817 pctldev
->desc
->npins
);
1818 /* remove gpio ranges map */
1819 list_for_each_entry_safe(range
, n
, &pctldev
->gpio_ranges
, node
)
1820 list_del(&range
->node
);
1822 mutex_unlock(&pctldev
->mutex
);
1823 mutex_destroy(&pctldev
->mutex
);
1825 mutex_unlock(&pinctrldev_list_mutex
);
1827 EXPORT_SYMBOL_GPL(pinctrl_unregister
);
1829 static int __init
pinctrl_init(void)
1831 pr_info("initialized pinctrl subsystem\n");
1832 pinctrl_init_debugfs();
1836 /* init early since many drivers really need to initialized pinmux early */
1837 core_initcall(pinctrl_init
);