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 pr_err("pin %d already registered on %s\n", number
,
235 pctldev
->desc
->name
);
239 pindesc
= kzalloc(sizeof(*pindesc
), GFP_KERNEL
);
240 if (pindesc
== NULL
) {
241 dev_err(pctldev
->dev
, "failed to alloc struct pin_desc\n");
246 pindesc
->pctldev
= pctldev
;
248 /* Copy basic pin info */
250 pindesc
->name
= name
;
252 pindesc
->name
= kasprintf(GFP_KERNEL
, "PIN%u", number
);
253 if (pindesc
->name
== NULL
) {
257 pindesc
->dynamic_name
= true;
260 radix_tree_insert(&pctldev
->pin_desc_tree
, number
, pindesc
);
261 pr_debug("registered pin %d (%s) on %s\n",
262 number
, pindesc
->name
, pctldev
->desc
->name
);
266 static int pinctrl_register_pins(struct pinctrl_dev
*pctldev
,
267 struct pinctrl_pin_desc
const *pins
,
273 for (i
= 0; i
< num_descs
; i
++) {
274 ret
= pinctrl_register_one_pin(pctldev
,
275 pins
[i
].number
, pins
[i
].name
);
284 * gpio_to_pin() - GPIO range GPIO number to pin number translation
285 * @range: GPIO range used for the translation
286 * @gpio: gpio pin to translate to a pin number
288 * Finds the pin number for a given GPIO using the specified GPIO range
289 * as a base for translation. The distinction between linear GPIO ranges
290 * and pin list based GPIO ranges is managed correctly by this function.
292 * This function assumes the gpio is part of the specified GPIO range, use
293 * only after making sure this is the case (e.g. by calling it on the
294 * result of successful pinctrl_get_device_gpio_range calls)!
296 static inline int gpio_to_pin(struct pinctrl_gpio_range
*range
,
299 unsigned int offset
= gpio
- range
->base
;
301 return range
->pins
[offset
];
303 return range
->pin_base
+ offset
;
307 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
308 * @pctldev: pin controller device to check
309 * @gpio: gpio pin to check taken from the global GPIO pin space
311 * Tries to match a GPIO pin number to the ranges handled by a certain pin
312 * controller, return the range or NULL
314 static struct pinctrl_gpio_range
*
315 pinctrl_match_gpio_range(struct pinctrl_dev
*pctldev
, unsigned gpio
)
317 struct pinctrl_gpio_range
*range
= NULL
;
319 mutex_lock(&pctldev
->mutex
);
320 /* Loop over the ranges */
321 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
322 /* Check if we're in the valid range */
323 if (gpio
>= range
->base
&&
324 gpio
< range
->base
+ range
->npins
) {
325 mutex_unlock(&pctldev
->mutex
);
329 mutex_unlock(&pctldev
->mutex
);
334 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
335 * the same GPIO chip are in range
336 * @gpio: gpio pin to check taken from the global GPIO pin space
338 * This function is complement of pinctrl_match_gpio_range(). If the return
339 * value of pinctrl_match_gpio_range() is NULL, this function could be used
340 * to check whether pinctrl device is ready or not. Maybe some GPIO pins
341 * of the same GPIO chip don't have back-end pinctrl interface.
342 * If the return value is true, it means that pinctrl device is ready & the
343 * certain GPIO pin doesn't have back-end pinctrl device. If the return value
344 * is false, it means that pinctrl device may not be ready.
346 #ifdef CONFIG_GPIOLIB
347 static bool pinctrl_ready_for_gpio_range(unsigned gpio
)
349 struct pinctrl_dev
*pctldev
;
350 struct pinctrl_gpio_range
*range
= NULL
;
351 struct gpio_chip
*chip
= gpio_to_chip(gpio
);
353 mutex_lock(&pinctrldev_list_mutex
);
355 /* Loop over the pin controllers */
356 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
357 /* Loop over the ranges */
358 mutex_lock(&pctldev
->mutex
);
359 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
360 /* Check if any gpio range overlapped with gpio chip */
361 if (range
->base
+ range
->npins
- 1 < chip
->base
||
362 range
->base
> chip
->base
+ chip
->ngpio
- 1)
364 mutex_unlock(&pctldev
->mutex
);
365 mutex_unlock(&pinctrldev_list_mutex
);
368 mutex_unlock(&pctldev
->mutex
);
371 mutex_unlock(&pinctrldev_list_mutex
);
376 static bool pinctrl_ready_for_gpio_range(unsigned gpio
) { return true; }
380 * pinctrl_get_device_gpio_range() - find device for GPIO range
381 * @gpio: the pin to locate the pin controller for
382 * @outdev: the pin control device if found
383 * @outrange: the GPIO range if found
385 * Find the pin controller handling a certain GPIO pin from the pinspace of
386 * the GPIO subsystem, return the device and the matching GPIO range. Returns
387 * -EPROBE_DEFER if the GPIO range could not be found in any device since it
388 * may still have not been registered.
390 static int pinctrl_get_device_gpio_range(unsigned gpio
,
391 struct pinctrl_dev
**outdev
,
392 struct pinctrl_gpio_range
**outrange
)
394 struct pinctrl_dev
*pctldev
= NULL
;
396 mutex_lock(&pinctrldev_list_mutex
);
398 /* Loop over the pin controllers */
399 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
400 struct pinctrl_gpio_range
*range
;
402 range
= pinctrl_match_gpio_range(pctldev
, gpio
);
406 mutex_unlock(&pinctrldev_list_mutex
);
411 mutex_unlock(&pinctrldev_list_mutex
);
413 return -EPROBE_DEFER
;
417 * pinctrl_add_gpio_range() - register a GPIO range for a controller
418 * @pctldev: pin controller device to add the range to
419 * @range: the GPIO range to add
421 * This adds a range of GPIOs to be handled by a certain pin controller. Call
422 * this to register handled ranges after registering your pin controller.
424 void pinctrl_add_gpio_range(struct pinctrl_dev
*pctldev
,
425 struct pinctrl_gpio_range
*range
)
427 mutex_lock(&pctldev
->mutex
);
428 list_add_tail(&range
->node
, &pctldev
->gpio_ranges
);
429 mutex_unlock(&pctldev
->mutex
);
431 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range
);
433 void pinctrl_add_gpio_ranges(struct pinctrl_dev
*pctldev
,
434 struct pinctrl_gpio_range
*ranges
,
439 for (i
= 0; i
< nranges
; i
++)
440 pinctrl_add_gpio_range(pctldev
, &ranges
[i
]);
442 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges
);
444 struct pinctrl_dev
*pinctrl_find_and_add_gpio_range(const char *devname
,
445 struct pinctrl_gpio_range
*range
)
447 struct pinctrl_dev
*pctldev
;
449 pctldev
= get_pinctrl_dev_from_devname(devname
);
452 * If we can't find this device, let's assume that is because
453 * it has not probed yet, so the driver trying to register this
454 * range need to defer probing.
457 return ERR_PTR(-EPROBE_DEFER
);
459 pinctrl_add_gpio_range(pctldev
, range
);
463 EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range
);
465 int pinctrl_get_group_pins(struct pinctrl_dev
*pctldev
, const char *pin_group
,
466 const unsigned **pins
, unsigned *num_pins
)
468 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
471 if (!pctlops
->get_group_pins
)
474 gs
= pinctrl_get_group_selector(pctldev
, pin_group
);
478 return pctlops
->get_group_pins(pctldev
, gs
, pins
, num_pins
);
480 EXPORT_SYMBOL_GPL(pinctrl_get_group_pins
);
483 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
484 * @pctldev: the pin controller device to look in
485 * @pin: a controller-local number to find the range for
487 struct pinctrl_gpio_range
*
488 pinctrl_find_gpio_range_from_pin(struct pinctrl_dev
*pctldev
,
491 struct pinctrl_gpio_range
*range
;
493 mutex_lock(&pctldev
->mutex
);
494 /* Loop over the ranges */
495 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
496 /* Check if we're in the valid range */
499 for (a
= 0; a
< range
->npins
; a
++) {
500 if (range
->pins
[a
] == pin
)
503 } else if (pin
>= range
->pin_base
&&
504 pin
< range
->pin_base
+ range
->npins
)
509 mutex_unlock(&pctldev
->mutex
);
512 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin
);
515 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
516 * @pctldev: pin controller device to remove the range from
517 * @range: the GPIO range to remove
519 void pinctrl_remove_gpio_range(struct pinctrl_dev
*pctldev
,
520 struct pinctrl_gpio_range
*range
)
522 mutex_lock(&pctldev
->mutex
);
523 list_del(&range
->node
);
524 mutex_unlock(&pctldev
->mutex
);
526 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range
);
529 * pinctrl_get_group_selector() - returns the group selector for a group
530 * @pctldev: the pin controller handling the group
531 * @pin_group: the pin group to look up
533 int pinctrl_get_group_selector(struct pinctrl_dev
*pctldev
,
534 const char *pin_group
)
536 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
537 unsigned ngroups
= pctlops
->get_groups_count(pctldev
);
538 unsigned group_selector
= 0;
540 while (group_selector
< ngroups
) {
541 const char *gname
= pctlops
->get_group_name(pctldev
,
543 if (!strcmp(gname
, pin_group
)) {
544 dev_dbg(pctldev
->dev
,
545 "found group selector %u for %s\n",
548 return group_selector
;
554 dev_err(pctldev
->dev
, "does not have pin group %s\n",
561 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
562 * @gpio: the GPIO pin number from the GPIO subsystem number space
564 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
565 * as part of their gpio_request() semantics, platforms and individual drivers
566 * shall *NOT* request GPIO pins to be muxed in.
568 int pinctrl_request_gpio(unsigned gpio
)
570 struct pinctrl_dev
*pctldev
;
571 struct pinctrl_gpio_range
*range
;
575 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
577 if (pinctrl_ready_for_gpio_range(gpio
))
582 mutex_lock(&pctldev
->mutex
);
584 /* Convert to the pin controllers number space */
585 pin
= gpio_to_pin(range
, gpio
);
587 ret
= pinmux_request_gpio(pctldev
, range
, pin
, gpio
);
589 mutex_unlock(&pctldev
->mutex
);
593 EXPORT_SYMBOL_GPL(pinctrl_request_gpio
);
596 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
597 * @gpio: the GPIO pin number from the GPIO subsystem number space
599 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
600 * as part of their gpio_free() semantics, platforms and individual drivers
601 * shall *NOT* request GPIO pins to be muxed out.
603 void pinctrl_free_gpio(unsigned gpio
)
605 struct pinctrl_dev
*pctldev
;
606 struct pinctrl_gpio_range
*range
;
610 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
614 mutex_lock(&pctldev
->mutex
);
616 /* Convert to the pin controllers number space */
617 pin
= gpio_to_pin(range
, gpio
);
619 pinmux_free_gpio(pctldev
, pin
, range
);
621 mutex_unlock(&pctldev
->mutex
);
623 EXPORT_SYMBOL_GPL(pinctrl_free_gpio
);
625 static int pinctrl_gpio_direction(unsigned gpio
, bool input
)
627 struct pinctrl_dev
*pctldev
;
628 struct pinctrl_gpio_range
*range
;
632 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
637 mutex_lock(&pctldev
->mutex
);
639 /* Convert to the pin controllers number space */
640 pin
= gpio_to_pin(range
, gpio
);
641 ret
= pinmux_gpio_direction(pctldev
, range
, pin
, input
);
643 mutex_unlock(&pctldev
->mutex
);
649 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
650 * @gpio: the GPIO pin number from the GPIO subsystem number space
652 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
653 * as part of their gpio_direction_input() semantics, platforms and individual
654 * drivers shall *NOT* touch pin control GPIO calls.
656 int pinctrl_gpio_direction_input(unsigned gpio
)
658 return pinctrl_gpio_direction(gpio
, true);
660 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input
);
663 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
664 * @gpio: the GPIO pin number from the GPIO subsystem number space
666 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
667 * as part of their gpio_direction_output() semantics, platforms and individual
668 * drivers shall *NOT* touch pin control GPIO calls.
670 int pinctrl_gpio_direction_output(unsigned gpio
)
672 return pinctrl_gpio_direction(gpio
, false);
674 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output
);
676 static struct pinctrl_state
*find_state(struct pinctrl
*p
,
679 struct pinctrl_state
*state
;
681 list_for_each_entry(state
, &p
->states
, node
)
682 if (!strcmp(state
->name
, name
))
688 static struct pinctrl_state
*create_state(struct pinctrl
*p
,
691 struct pinctrl_state
*state
;
693 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
696 "failed to alloc struct pinctrl_state\n");
697 return ERR_PTR(-ENOMEM
);
701 INIT_LIST_HEAD(&state
->settings
);
703 list_add_tail(&state
->node
, &p
->states
);
708 static int add_setting(struct pinctrl
*p
, struct pinctrl_map
const *map
)
710 struct pinctrl_state
*state
;
711 struct pinctrl_setting
*setting
;
714 state
= find_state(p
, map
->name
);
716 state
= create_state(p
, map
->name
);
718 return PTR_ERR(state
);
720 if (map
->type
== PIN_MAP_TYPE_DUMMY_STATE
)
723 setting
= kzalloc(sizeof(*setting
), GFP_KERNEL
);
724 if (setting
== NULL
) {
726 "failed to alloc struct pinctrl_setting\n");
730 setting
->type
= map
->type
;
732 setting
->pctldev
= get_pinctrl_dev_from_devname(map
->ctrl_dev_name
);
733 if (setting
->pctldev
== NULL
) {
735 /* Do not defer probing of hogs (circular loop) */
736 if (!strcmp(map
->ctrl_dev_name
, map
->dev_name
))
739 * OK let us guess that the driver is not there yet, and
740 * let's defer obtaining this pinctrl handle to later...
742 dev_info(p
->dev
, "unknown pinctrl device %s in map entry, deferring probe",
744 return -EPROBE_DEFER
;
747 setting
->dev_name
= map
->dev_name
;
750 case PIN_MAP_TYPE_MUX_GROUP
:
751 ret
= pinmux_map_to_setting(map
, setting
);
753 case PIN_MAP_TYPE_CONFIGS_PIN
:
754 case PIN_MAP_TYPE_CONFIGS_GROUP
:
755 ret
= pinconf_map_to_setting(map
, setting
);
766 list_add_tail(&setting
->node
, &state
->settings
);
771 static struct pinctrl
*find_pinctrl(struct device
*dev
)
775 mutex_lock(&pinctrl_list_mutex
);
776 list_for_each_entry(p
, &pinctrl_list
, node
)
778 mutex_unlock(&pinctrl_list_mutex
);
782 mutex_unlock(&pinctrl_list_mutex
);
786 static void pinctrl_free(struct pinctrl
*p
, bool inlist
);
788 static struct pinctrl
*create_pinctrl(struct device
*dev
)
792 struct pinctrl_maps
*maps_node
;
794 struct pinctrl_map
const *map
;
798 * create the state cookie holder struct pinctrl for each
799 * mapping, this is what consumers will get when requesting
800 * a pin control handle with pinctrl_get()
802 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
804 dev_err(dev
, "failed to alloc struct pinctrl\n");
805 return ERR_PTR(-ENOMEM
);
808 INIT_LIST_HEAD(&p
->states
);
809 INIT_LIST_HEAD(&p
->dt_maps
);
811 ret
= pinctrl_dt_to_map(p
);
817 devname
= dev_name(dev
);
819 mutex_lock(&pinctrl_maps_mutex
);
820 /* Iterate over the pin control maps to locate the right ones */
821 for_each_maps(maps_node
, i
, map
) {
822 /* Map must be for this device */
823 if (strcmp(map
->dev_name
, devname
))
826 ret
= add_setting(p
, map
);
828 * At this point the adding of a setting may:
830 * - Defer, if the pinctrl device is not yet available
831 * - Fail, if the pinctrl device is not yet available,
832 * AND the setting is a hog. We cannot defer that, since
833 * the hog will kick in immediately after the device
836 * If the error returned was not -EPROBE_DEFER then we
837 * accumulate the errors to see if we end up with
838 * an -EPROBE_DEFER later, as that is the worst case.
840 if (ret
== -EPROBE_DEFER
) {
841 pinctrl_free(p
, false);
842 mutex_unlock(&pinctrl_maps_mutex
);
846 mutex_unlock(&pinctrl_maps_mutex
);
849 /* If some other error than deferral occured, return here */
850 pinctrl_free(p
, false);
854 kref_init(&p
->users
);
856 /* Add the pinctrl handle to the global list */
857 mutex_lock(&pinctrl_list_mutex
);
858 list_add_tail(&p
->node
, &pinctrl_list
);
859 mutex_unlock(&pinctrl_list_mutex
);
865 * pinctrl_get() - retrieves the pinctrl handle for a device
866 * @dev: the device to obtain the handle for
868 struct pinctrl
*pinctrl_get(struct device
*dev
)
873 return ERR_PTR(-EINVAL
);
876 * See if somebody else (such as the device core) has already
877 * obtained a handle to the pinctrl for this device. In that case,
878 * return another pointer to it.
880 p
= find_pinctrl(dev
);
882 dev_dbg(dev
, "obtain a copy of previously claimed pinctrl\n");
887 return create_pinctrl(dev
);
889 EXPORT_SYMBOL_GPL(pinctrl_get
);
891 static void pinctrl_free_setting(bool disable_setting
,
892 struct pinctrl_setting
*setting
)
894 switch (setting
->type
) {
895 case PIN_MAP_TYPE_MUX_GROUP
:
897 pinmux_disable_setting(setting
);
898 pinmux_free_setting(setting
);
900 case PIN_MAP_TYPE_CONFIGS_PIN
:
901 case PIN_MAP_TYPE_CONFIGS_GROUP
:
902 pinconf_free_setting(setting
);
909 static void pinctrl_free(struct pinctrl
*p
, bool inlist
)
911 struct pinctrl_state
*state
, *n1
;
912 struct pinctrl_setting
*setting
, *n2
;
914 mutex_lock(&pinctrl_list_mutex
);
915 list_for_each_entry_safe(state
, n1
, &p
->states
, node
) {
916 list_for_each_entry_safe(setting
, n2
, &state
->settings
, node
) {
917 pinctrl_free_setting(state
== p
->state
, setting
);
918 list_del(&setting
->node
);
921 list_del(&state
->node
);
925 pinctrl_dt_free_maps(p
);
930 mutex_unlock(&pinctrl_list_mutex
);
934 * pinctrl_release() - release the pinctrl handle
935 * @kref: the kref in the pinctrl being released
937 static void pinctrl_release(struct kref
*kref
)
939 struct pinctrl
*p
= container_of(kref
, struct pinctrl
, users
);
941 pinctrl_free(p
, true);
945 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
946 * @p: the pinctrl handle to release
948 void pinctrl_put(struct pinctrl
*p
)
950 kref_put(&p
->users
, pinctrl_release
);
952 EXPORT_SYMBOL_GPL(pinctrl_put
);
955 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
956 * @p: the pinctrl handle to retrieve the state from
957 * @name: the state name to retrieve
959 struct pinctrl_state
*pinctrl_lookup_state(struct pinctrl
*p
,
962 struct pinctrl_state
*state
;
964 state
= find_state(p
, name
);
966 if (pinctrl_dummy_state
) {
967 /* create dummy state */
968 dev_dbg(p
->dev
, "using pinctrl dummy state (%s)\n",
970 state
= create_state(p
, name
);
972 state
= ERR_PTR(-ENODEV
);
977 EXPORT_SYMBOL_GPL(pinctrl_lookup_state
);
980 * pinctrl_select_state() - select/activate/program a pinctrl state to HW
981 * @p: the pinctrl handle for the device that requests configuration
982 * @state: the state handle to select/activate/program
984 int pinctrl_select_state(struct pinctrl
*p
, struct pinctrl_state
*state
)
986 struct pinctrl_setting
*setting
, *setting2
;
987 struct pinctrl_state
*old_state
= p
->state
;
990 if (p
->state
== state
)
995 * For each pinmux setting in the old state, forget SW's record
996 * of mux owner for that pingroup. Any pingroups which are
997 * still owned by the new state will be re-acquired by the call
998 * to pinmux_enable_setting() in the loop below.
1000 list_for_each_entry(setting
, &p
->state
->settings
, node
) {
1001 if (setting
->type
!= PIN_MAP_TYPE_MUX_GROUP
)
1003 pinmux_disable_setting(setting
);
1009 /* Apply all the settings for the new state */
1010 list_for_each_entry(setting
, &state
->settings
, node
) {
1011 switch (setting
->type
) {
1012 case PIN_MAP_TYPE_MUX_GROUP
:
1013 ret
= pinmux_enable_setting(setting
);
1015 case PIN_MAP_TYPE_CONFIGS_PIN
:
1016 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1017 ret
= pinconf_apply_setting(setting
);
1025 goto unapply_new_state
;
1034 dev_err(p
->dev
, "Error applying setting, reverse things back\n");
1036 list_for_each_entry(setting2
, &state
->settings
, node
) {
1037 if (&setting2
->node
== &setting
->node
)
1040 * All we can do here is pinmux_disable_setting.
1041 * That means that some pins are muxed differently now
1042 * than they were before applying the setting (We can't
1043 * "unmux a pin"!), but it's not a big deal since the pins
1044 * are free to be muxed by another apply_setting.
1046 if (setting2
->type
== PIN_MAP_TYPE_MUX_GROUP
)
1047 pinmux_disable_setting(setting2
);
1050 /* There's no infinite recursive loop here because p->state is NULL */
1052 pinctrl_select_state(p
, old_state
);
1056 EXPORT_SYMBOL_GPL(pinctrl_select_state
);
1058 static void devm_pinctrl_release(struct device
*dev
, void *res
)
1060 pinctrl_put(*(struct pinctrl
**)res
);
1064 * struct devm_pinctrl_get() - Resource managed pinctrl_get()
1065 * @dev: the device to obtain the handle for
1067 * If there is a need to explicitly destroy the returned struct pinctrl,
1068 * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
1070 struct pinctrl
*devm_pinctrl_get(struct device
*dev
)
1072 struct pinctrl
**ptr
, *p
;
1074 ptr
= devres_alloc(devm_pinctrl_release
, sizeof(*ptr
), GFP_KERNEL
);
1076 return ERR_PTR(-ENOMEM
);
1078 p
= pinctrl_get(dev
);
1081 devres_add(dev
, ptr
);
1088 EXPORT_SYMBOL_GPL(devm_pinctrl_get
);
1090 static int devm_pinctrl_match(struct device
*dev
, void *res
, void *data
)
1092 struct pinctrl
**p
= res
;
1098 * devm_pinctrl_put() - Resource managed pinctrl_put()
1099 * @p: the pinctrl handle to release
1101 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
1102 * this function will not need to be called and the resource management
1103 * code will ensure that the resource is freed.
1105 void devm_pinctrl_put(struct pinctrl
*p
)
1107 WARN_ON(devres_release(p
->dev
, devm_pinctrl_release
,
1108 devm_pinctrl_match
, p
));
1110 EXPORT_SYMBOL_GPL(devm_pinctrl_put
);
1112 int pinctrl_register_map(struct pinctrl_map
const *maps
, unsigned num_maps
,
1113 bool dup
, bool locked
)
1116 struct pinctrl_maps
*maps_node
;
1118 pr_debug("add %d pinmux maps\n", num_maps
);
1120 /* First sanity check the new mapping */
1121 for (i
= 0; i
< num_maps
; i
++) {
1122 if (!maps
[i
].dev_name
) {
1123 pr_err("failed to register map %s (%d): no device given\n",
1128 if (!maps
[i
].name
) {
1129 pr_err("failed to register map %d: no map name given\n",
1134 if (maps
[i
].type
!= PIN_MAP_TYPE_DUMMY_STATE
&&
1135 !maps
[i
].ctrl_dev_name
) {
1136 pr_err("failed to register map %s (%d): no pin control device given\n",
1141 switch (maps
[i
].type
) {
1142 case PIN_MAP_TYPE_DUMMY_STATE
:
1144 case PIN_MAP_TYPE_MUX_GROUP
:
1145 ret
= pinmux_validate_map(&maps
[i
], i
);
1149 case PIN_MAP_TYPE_CONFIGS_PIN
:
1150 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1151 ret
= pinconf_validate_map(&maps
[i
], i
);
1156 pr_err("failed to register map %s (%d): invalid type given\n",
1162 maps_node
= kzalloc(sizeof(*maps_node
), GFP_KERNEL
);
1164 pr_err("failed to alloc struct pinctrl_maps\n");
1168 maps_node
->num_maps
= num_maps
;
1170 maps_node
->maps
= kmemdup(maps
, sizeof(*maps
) * num_maps
,
1172 if (!maps_node
->maps
) {
1173 pr_err("failed to duplicate mapping table\n");
1178 maps_node
->maps
= maps
;
1182 mutex_lock(&pinctrl_maps_mutex
);
1183 list_add_tail(&maps_node
->node
, &pinctrl_maps
);
1185 mutex_unlock(&pinctrl_maps_mutex
);
1191 * pinctrl_register_mappings() - register a set of pin controller mappings
1192 * @maps: the pincontrol mappings table to register. This should probably be
1193 * marked with __initdata so it can be discarded after boot. This
1194 * function will perform a shallow copy for the mapping entries.
1195 * @num_maps: the number of maps in the mapping table
1197 int pinctrl_register_mappings(struct pinctrl_map
const *maps
,
1200 return pinctrl_register_map(maps
, num_maps
, true, false);
1203 void pinctrl_unregister_map(struct pinctrl_map
const *map
)
1205 struct pinctrl_maps
*maps_node
;
1207 mutex_lock(&pinctrl_maps_mutex
);
1208 list_for_each_entry(maps_node
, &pinctrl_maps
, node
) {
1209 if (maps_node
->maps
== map
) {
1210 list_del(&maps_node
->node
);
1212 mutex_unlock(&pinctrl_maps_mutex
);
1216 mutex_unlock(&pinctrl_maps_mutex
);
1220 * pinctrl_force_sleep() - turn a given controller device into sleep state
1221 * @pctldev: pin controller device
1223 int pinctrl_force_sleep(struct pinctrl_dev
*pctldev
)
1225 if (!IS_ERR(pctldev
->p
) && !IS_ERR(pctldev
->hog_sleep
))
1226 return pinctrl_select_state(pctldev
->p
, pctldev
->hog_sleep
);
1229 EXPORT_SYMBOL_GPL(pinctrl_force_sleep
);
1232 * pinctrl_force_default() - turn a given controller device into default state
1233 * @pctldev: pin controller device
1235 int pinctrl_force_default(struct pinctrl_dev
*pctldev
)
1237 if (!IS_ERR(pctldev
->p
) && !IS_ERR(pctldev
->hog_default
))
1238 return pinctrl_select_state(pctldev
->p
, pctldev
->hog_default
);
1241 EXPORT_SYMBOL_GPL(pinctrl_force_default
);
1246 * pinctrl_pm_select_state() - select pinctrl state for PM
1247 * @dev: device to select default state for
1248 * @state: state to set
1250 static int pinctrl_pm_select_state(struct device
*dev
,
1251 struct pinctrl_state
*state
)
1253 struct dev_pin_info
*pins
= dev
->pins
;
1257 return 0; /* No such state */
1258 ret
= pinctrl_select_state(pins
->p
, state
);
1260 dev_err(dev
, "failed to activate pinctrl state %s\n",
1266 * pinctrl_pm_select_default_state() - select default pinctrl state for PM
1267 * @dev: device to select default state for
1269 int pinctrl_pm_select_default_state(struct device
*dev
)
1274 return pinctrl_pm_select_state(dev
, dev
->pins
->default_state
);
1276 EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state
);
1279 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
1280 * @dev: device to select sleep state for
1282 int pinctrl_pm_select_sleep_state(struct device
*dev
)
1287 return pinctrl_pm_select_state(dev
, dev
->pins
->sleep_state
);
1289 EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state
);
1292 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
1293 * @dev: device to select idle state for
1295 int pinctrl_pm_select_idle_state(struct device
*dev
)
1300 return pinctrl_pm_select_state(dev
, dev
->pins
->idle_state
);
1302 EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state
);
1305 #ifdef CONFIG_DEBUG_FS
1307 static int pinctrl_pins_show(struct seq_file
*s
, void *what
)
1309 struct pinctrl_dev
*pctldev
= s
->private;
1310 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1313 seq_printf(s
, "registered pins: %d\n", pctldev
->desc
->npins
);
1315 mutex_lock(&pctldev
->mutex
);
1317 /* The pin number can be retrived from the pin controller descriptor */
1318 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
1319 struct pin_desc
*desc
;
1321 pin
= pctldev
->desc
->pins
[i
].number
;
1322 desc
= pin_desc_get(pctldev
, pin
);
1323 /* Pin space may be sparse */
1327 seq_printf(s
, "pin %d (%s) ", pin
,
1328 desc
->name
? desc
->name
: "unnamed");
1330 /* Driver-specific info per pin */
1331 if (ops
->pin_dbg_show
)
1332 ops
->pin_dbg_show(pctldev
, s
, pin
);
1337 mutex_unlock(&pctldev
->mutex
);
1342 static int pinctrl_groups_show(struct seq_file
*s
, void *what
)
1344 struct pinctrl_dev
*pctldev
= s
->private;
1345 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1346 unsigned ngroups
, selector
= 0;
1348 mutex_lock(&pctldev
->mutex
);
1350 ngroups
= ops
->get_groups_count(pctldev
);
1352 seq_puts(s
, "registered pin groups:\n");
1353 while (selector
< ngroups
) {
1354 const unsigned *pins
= NULL
;
1355 unsigned num_pins
= 0;
1356 const char *gname
= ops
->get_group_name(pctldev
, selector
);
1361 if (ops
->get_group_pins
)
1362 ret
= ops
->get_group_pins(pctldev
, selector
,
1365 seq_printf(s
, "%s [ERROR GETTING PINS]\n",
1368 seq_printf(s
, "group: %s\n", gname
);
1369 for (i
= 0; i
< num_pins
; i
++) {
1370 pname
= pin_get_name(pctldev
, pins
[i
]);
1371 if (WARN_ON(!pname
)) {
1372 mutex_unlock(&pctldev
->mutex
);
1375 seq_printf(s
, "pin %d (%s)\n", pins
[i
], pname
);
1382 mutex_unlock(&pctldev
->mutex
);
1387 static int pinctrl_gpioranges_show(struct seq_file
*s
, void *what
)
1389 struct pinctrl_dev
*pctldev
= s
->private;
1390 struct pinctrl_gpio_range
*range
= NULL
;
1392 seq_puts(s
, "GPIO ranges handled:\n");
1394 mutex_lock(&pctldev
->mutex
);
1396 /* Loop over the ranges */
1397 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
1400 seq_printf(s
, "%u: %s GPIOS [%u - %u] PINS {",
1401 range
->id
, range
->name
,
1402 range
->base
, (range
->base
+ range
->npins
- 1));
1403 for (a
= 0; a
< range
->npins
- 1; a
++)
1404 seq_printf(s
, "%u, ", range
->pins
[a
]);
1405 seq_printf(s
, "%u}\n", range
->pins
[a
]);
1408 seq_printf(s
, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
1409 range
->id
, range
->name
,
1410 range
->base
, (range
->base
+ range
->npins
- 1),
1412 (range
->pin_base
+ range
->npins
- 1));
1415 mutex_unlock(&pctldev
->mutex
);
1420 static int pinctrl_devices_show(struct seq_file
*s
, void *what
)
1422 struct pinctrl_dev
*pctldev
;
1424 seq_puts(s
, "name [pinmux] [pinconf]\n");
1426 mutex_lock(&pinctrldev_list_mutex
);
1428 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
1429 seq_printf(s
, "%s ", pctldev
->desc
->name
);
1430 if (pctldev
->desc
->pmxops
)
1431 seq_puts(s
, "yes ");
1434 if (pctldev
->desc
->confops
)
1441 mutex_unlock(&pinctrldev_list_mutex
);
1446 static inline const char *map_type(enum pinctrl_map_type type
)
1448 static const char * const names
[] = {
1456 if (type
>= ARRAY_SIZE(names
))
1462 static int pinctrl_maps_show(struct seq_file
*s
, void *what
)
1464 struct pinctrl_maps
*maps_node
;
1466 struct pinctrl_map
const *map
;
1468 seq_puts(s
, "Pinctrl maps:\n");
1470 mutex_lock(&pinctrl_maps_mutex
);
1471 for_each_maps(maps_node
, i
, map
) {
1472 seq_printf(s
, "device %s\nstate %s\ntype %s (%d)\n",
1473 map
->dev_name
, map
->name
, map_type(map
->type
),
1476 if (map
->type
!= PIN_MAP_TYPE_DUMMY_STATE
)
1477 seq_printf(s
, "controlling device %s\n",
1478 map
->ctrl_dev_name
);
1480 switch (map
->type
) {
1481 case PIN_MAP_TYPE_MUX_GROUP
:
1482 pinmux_show_map(s
, map
);
1484 case PIN_MAP_TYPE_CONFIGS_PIN
:
1485 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1486 pinconf_show_map(s
, map
);
1492 seq_printf(s
, "\n");
1494 mutex_unlock(&pinctrl_maps_mutex
);
1499 static int pinctrl_show(struct seq_file
*s
, void *what
)
1502 struct pinctrl_state
*state
;
1503 struct pinctrl_setting
*setting
;
1505 seq_puts(s
, "Requested pin control handlers their pinmux maps:\n");
1507 mutex_lock(&pinctrl_list_mutex
);
1509 list_for_each_entry(p
, &pinctrl_list
, node
) {
1510 seq_printf(s
, "device: %s current state: %s\n",
1512 p
->state
? p
->state
->name
: "none");
1514 list_for_each_entry(state
, &p
->states
, node
) {
1515 seq_printf(s
, " state: %s\n", state
->name
);
1517 list_for_each_entry(setting
, &state
->settings
, node
) {
1518 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
1520 seq_printf(s
, " type: %s controller %s ",
1521 map_type(setting
->type
),
1522 pinctrl_dev_get_name(pctldev
));
1524 switch (setting
->type
) {
1525 case PIN_MAP_TYPE_MUX_GROUP
:
1526 pinmux_show_setting(s
, setting
);
1528 case PIN_MAP_TYPE_CONFIGS_PIN
:
1529 case PIN_MAP_TYPE_CONFIGS_GROUP
:
1530 pinconf_show_setting(s
, setting
);
1539 mutex_unlock(&pinctrl_list_mutex
);
1544 static int pinctrl_pins_open(struct inode
*inode
, struct file
*file
)
1546 return single_open(file
, pinctrl_pins_show
, inode
->i_private
);
1549 static int pinctrl_groups_open(struct inode
*inode
, struct file
*file
)
1551 return single_open(file
, pinctrl_groups_show
, inode
->i_private
);
1554 static int pinctrl_gpioranges_open(struct inode
*inode
, struct file
*file
)
1556 return single_open(file
, pinctrl_gpioranges_show
, inode
->i_private
);
1559 static int pinctrl_devices_open(struct inode
*inode
, struct file
*file
)
1561 return single_open(file
, pinctrl_devices_show
, NULL
);
1564 static int pinctrl_maps_open(struct inode
*inode
, struct file
*file
)
1566 return single_open(file
, pinctrl_maps_show
, NULL
);
1569 static int pinctrl_open(struct inode
*inode
, struct file
*file
)
1571 return single_open(file
, pinctrl_show
, NULL
);
1574 static const struct file_operations pinctrl_pins_ops
= {
1575 .open
= pinctrl_pins_open
,
1577 .llseek
= seq_lseek
,
1578 .release
= single_release
,
1581 static const struct file_operations pinctrl_groups_ops
= {
1582 .open
= pinctrl_groups_open
,
1584 .llseek
= seq_lseek
,
1585 .release
= single_release
,
1588 static const struct file_operations pinctrl_gpioranges_ops
= {
1589 .open
= pinctrl_gpioranges_open
,
1591 .llseek
= seq_lseek
,
1592 .release
= single_release
,
1595 static const struct file_operations pinctrl_devices_ops
= {
1596 .open
= pinctrl_devices_open
,
1598 .llseek
= seq_lseek
,
1599 .release
= single_release
,
1602 static const struct file_operations pinctrl_maps_ops
= {
1603 .open
= pinctrl_maps_open
,
1605 .llseek
= seq_lseek
,
1606 .release
= single_release
,
1609 static const struct file_operations pinctrl_ops
= {
1610 .open
= pinctrl_open
,
1612 .llseek
= seq_lseek
,
1613 .release
= single_release
,
1616 static struct dentry
*debugfs_root
;
1618 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1620 struct dentry
*device_root
;
1622 device_root
= debugfs_create_dir(dev_name(pctldev
->dev
),
1624 pctldev
->device_root
= device_root
;
1626 if (IS_ERR(device_root
) || !device_root
) {
1627 pr_warn("failed to create debugfs directory for %s\n",
1628 dev_name(pctldev
->dev
));
1631 debugfs_create_file("pins", S_IFREG
| S_IRUGO
,
1632 device_root
, pctldev
, &pinctrl_pins_ops
);
1633 debugfs_create_file("pingroups", S_IFREG
| S_IRUGO
,
1634 device_root
, pctldev
, &pinctrl_groups_ops
);
1635 debugfs_create_file("gpio-ranges", S_IFREG
| S_IRUGO
,
1636 device_root
, pctldev
, &pinctrl_gpioranges_ops
);
1637 if (pctldev
->desc
->pmxops
)
1638 pinmux_init_device_debugfs(device_root
, pctldev
);
1639 if (pctldev
->desc
->confops
)
1640 pinconf_init_device_debugfs(device_root
, pctldev
);
1643 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1645 debugfs_remove_recursive(pctldev
->device_root
);
1648 static void pinctrl_init_debugfs(void)
1650 debugfs_root
= debugfs_create_dir("pinctrl", NULL
);
1651 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
1652 pr_warn("failed to create debugfs directory\n");
1653 debugfs_root
= NULL
;
1657 debugfs_create_file("pinctrl-devices", S_IFREG
| S_IRUGO
,
1658 debugfs_root
, NULL
, &pinctrl_devices_ops
);
1659 debugfs_create_file("pinctrl-maps", S_IFREG
| S_IRUGO
,
1660 debugfs_root
, NULL
, &pinctrl_maps_ops
);
1661 debugfs_create_file("pinctrl-handles", S_IFREG
| S_IRUGO
,
1662 debugfs_root
, NULL
, &pinctrl_ops
);
1665 #else /* CONFIG_DEBUG_FS */
1667 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1671 static void pinctrl_init_debugfs(void)
1675 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1681 static int pinctrl_check_ops(struct pinctrl_dev
*pctldev
)
1683 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
1686 !ops
->get_groups_count
||
1687 !ops
->get_group_name
)
1690 if (ops
->dt_node_to_map
&& !ops
->dt_free_map
)
1697 * pinctrl_register() - register a pin controller device
1698 * @pctldesc: descriptor for this pin controller
1699 * @dev: parent device for this pin controller
1700 * @driver_data: private pin controller data for this pin controller
1702 struct pinctrl_dev
*pinctrl_register(struct pinctrl_desc
*pctldesc
,
1703 struct device
*dev
, void *driver_data
)
1705 struct pinctrl_dev
*pctldev
;
1710 if (!pctldesc
->name
)
1713 pctldev
= kzalloc(sizeof(*pctldev
), GFP_KERNEL
);
1714 if (pctldev
== NULL
) {
1715 dev_err(dev
, "failed to alloc struct pinctrl_dev\n");
1719 /* Initialize pin control device struct */
1720 pctldev
->owner
= pctldesc
->owner
;
1721 pctldev
->desc
= pctldesc
;
1722 pctldev
->driver_data
= driver_data
;
1723 INIT_RADIX_TREE(&pctldev
->pin_desc_tree
, GFP_KERNEL
);
1724 INIT_LIST_HEAD(&pctldev
->gpio_ranges
);
1726 mutex_init(&pctldev
->mutex
);
1728 /* check core ops for sanity */
1729 if (pinctrl_check_ops(pctldev
)) {
1730 dev_err(dev
, "pinctrl ops lacks necessary functions\n");
1734 /* If we're implementing pinmuxing, check the ops for sanity */
1735 if (pctldesc
->pmxops
) {
1736 if (pinmux_check_ops(pctldev
))
1740 /* If we're implementing pinconfig, check the ops for sanity */
1741 if (pctldesc
->confops
) {
1742 if (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
);
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
);