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/radix-tree.h>
22 #include <linux/err.h>
23 #include <linux/list.h>
24 #include <linux/mutex.h>
25 #include <linux/spinlock.h>
26 #include <linux/sysfs.h>
27 #include <linux/debugfs.h>
28 #include <linux/seq_file.h>
29 #include <linux/pinctrl/pinctrl.h>
30 #include <linux/pinctrl/machine.h>
36 * struct pinctrl_maps - a list item containing part of the mapping table
37 * @node: mapping table list node
38 * @maps: array of mapping table entries
39 * @num_maps: the number of entries in @maps
42 struct list_head node
;
43 struct pinctrl_map
const *maps
;
48 * struct pinctrl_hog - a list item to stash control hogs
49 * @node: pin control hog list node
50 * @map: map entry responsible for this hogging
51 * @pmx: the pin control hogged by this item
54 struct list_head node
;
55 struct pinctrl_map
const *map
;
59 /* Global list of pin control devices */
60 static DEFINE_MUTEX(pinctrldev_list_mutex
);
61 static LIST_HEAD(pinctrldev_list
);
63 /* List of pin controller handles */
64 static DEFINE_MUTEX(pinctrl_list_mutex
);
65 static LIST_HEAD(pinctrl_list
);
67 /* Global pinctrl maps */
68 static DEFINE_MUTEX(pinctrl_maps_mutex
);
69 static LIST_HEAD(pinctrl_maps
);
71 #define for_each_maps(_maps_node_, _i_, _map_) \
72 list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
73 for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
74 _i_ < _maps_node_->num_maps; \
75 i++, _map_ = &_maps_node_->maps[_i_])
77 const char *pinctrl_dev_get_name(struct pinctrl_dev
*pctldev
)
79 /* We're not allowed to register devices without name */
80 return pctldev
->desc
->name
;
82 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name
);
84 void *pinctrl_dev_get_drvdata(struct pinctrl_dev
*pctldev
)
86 return pctldev
->driver_data
;
88 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata
);
91 * get_pinctrl_dev_from_devname() - look up pin controller device
92 * @devname: the name of a device instance, as returned by dev_name()
94 * Looks up a pin control device matching a certain device name or pure device
95 * pointer, the pure device pointer will take precedence.
97 struct pinctrl_dev
*get_pinctrl_dev_from_devname(const char *devname
)
99 struct pinctrl_dev
*pctldev
= NULL
;
105 mutex_lock(&pinctrldev_list_mutex
);
106 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
107 if (!strcmp(dev_name(pctldev
->dev
), devname
)) {
108 /* Matched on device name */
113 mutex_unlock(&pinctrldev_list_mutex
);
115 return found
? pctldev
: NULL
;
118 struct pin_desc
*pin_desc_get(struct pinctrl_dev
*pctldev
, unsigned int pin
)
120 struct pin_desc
*pindesc
;
123 spin_lock_irqsave(&pctldev
->pin_desc_tree_lock
, flags
);
124 pindesc
= radix_tree_lookup(&pctldev
->pin_desc_tree
, pin
);
125 spin_unlock_irqrestore(&pctldev
->pin_desc_tree_lock
, flags
);
131 * pin_get_from_name() - look up a pin number from a name
132 * @pctldev: the pin control device to lookup the pin on
133 * @name: the name of the pin to look up
135 int pin_get_from_name(struct pinctrl_dev
*pctldev
, const char *name
)
139 /* The pin number can be retrived from the pin controller descriptor */
140 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
141 struct pin_desc
*desc
;
143 pin
= pctldev
->desc
->pins
[i
].number
;
144 desc
= pin_desc_get(pctldev
, pin
);
145 /* Pin space may be sparse */
148 if (desc
->name
&& !strcmp(name
, desc
->name
))
156 * pin_is_valid() - check if pin exists on controller
157 * @pctldev: the pin control device to check the pin on
158 * @pin: pin to check, use the local pin controller index number
160 * This tells us whether a certain pin exist on a certain pin controller or
161 * not. Pin lists may be sparse, so some pins may not exist.
163 bool pin_is_valid(struct pinctrl_dev
*pctldev
, int pin
)
165 struct pin_desc
*pindesc
;
170 pindesc
= pin_desc_get(pctldev
, pin
);
176 EXPORT_SYMBOL_GPL(pin_is_valid
);
178 /* Deletes a range of pin descriptors */
179 static void pinctrl_free_pindescs(struct pinctrl_dev
*pctldev
,
180 const struct pinctrl_pin_desc
*pins
,
185 spin_lock(&pctldev
->pin_desc_tree_lock
);
186 for (i
= 0; i
< num_pins
; i
++) {
187 struct pin_desc
*pindesc
;
189 pindesc
= radix_tree_lookup(&pctldev
->pin_desc_tree
,
191 if (pindesc
!= NULL
) {
192 radix_tree_delete(&pctldev
->pin_desc_tree
,
194 if (pindesc
->dynamic_name
)
195 kfree(pindesc
->name
);
199 spin_unlock(&pctldev
->pin_desc_tree_lock
);
202 static int pinctrl_register_one_pin(struct pinctrl_dev
*pctldev
,
203 unsigned number
, const char *name
)
205 struct pin_desc
*pindesc
;
207 pindesc
= pin_desc_get(pctldev
, number
);
208 if (pindesc
!= NULL
) {
209 pr_err("pin %d already registered on %s\n", number
,
210 pctldev
->desc
->name
);
214 pindesc
= kzalloc(sizeof(*pindesc
), GFP_KERNEL
);
215 if (pindesc
== NULL
) {
216 dev_err(pctldev
->dev
, "failed to alloc struct pin_desc\n");
220 spin_lock_init(&pindesc
->lock
);
223 pindesc
->pctldev
= pctldev
;
225 /* Copy basic pin info */
227 pindesc
->name
= name
;
229 pindesc
->name
= kasprintf(GFP_KERNEL
, "PIN%u", number
);
230 if (pindesc
->name
== NULL
)
232 pindesc
->dynamic_name
= true;
235 spin_lock(&pctldev
->pin_desc_tree_lock
);
236 radix_tree_insert(&pctldev
->pin_desc_tree
, number
, pindesc
);
237 spin_unlock(&pctldev
->pin_desc_tree_lock
);
238 pr_debug("registered pin %d (%s) on %s\n",
239 number
, pindesc
->name
, pctldev
->desc
->name
);
243 static int pinctrl_register_pins(struct pinctrl_dev
*pctldev
,
244 struct pinctrl_pin_desc
const *pins
,
250 for (i
= 0; i
< num_descs
; i
++) {
251 ret
= pinctrl_register_one_pin(pctldev
,
252 pins
[i
].number
, pins
[i
].name
);
261 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
262 * @pctldev: pin controller device to check
263 * @gpio: gpio pin to check taken from the global GPIO pin space
265 * Tries to match a GPIO pin number to the ranges handled by a certain pin
266 * controller, return the range or NULL
268 static struct pinctrl_gpio_range
*
269 pinctrl_match_gpio_range(struct pinctrl_dev
*pctldev
, unsigned gpio
)
271 struct pinctrl_gpio_range
*range
= NULL
;
273 /* Loop over the ranges */
274 mutex_lock(&pctldev
->gpio_ranges_lock
);
275 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
276 /* Check if we're in the valid range */
277 if (gpio
>= range
->base
&&
278 gpio
< range
->base
+ range
->npins
) {
279 mutex_unlock(&pctldev
->gpio_ranges_lock
);
283 mutex_unlock(&pctldev
->gpio_ranges_lock
);
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 * negative if the GPIO range could not be found in any device.
298 static int pinctrl_get_device_gpio_range(unsigned gpio
,
299 struct pinctrl_dev
**outdev
,
300 struct pinctrl_gpio_range
**outrange
)
302 struct pinctrl_dev
*pctldev
= NULL
;
304 /* Loop over the pin controllers */
305 mutex_lock(&pinctrldev_list_mutex
);
306 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
307 struct pinctrl_gpio_range
*range
;
309 range
= pinctrl_match_gpio_range(pctldev
, gpio
);
313 mutex_unlock(&pinctrldev_list_mutex
);
317 mutex_unlock(&pinctrldev_list_mutex
);
323 * pinctrl_add_gpio_range() - register a GPIO range for a controller
324 * @pctldev: pin controller device to add the range to
325 * @range: the GPIO range to add
327 * This adds a range of GPIOs to be handled by a certain pin controller. Call
328 * this to register handled ranges after registering your pin controller.
330 void pinctrl_add_gpio_range(struct pinctrl_dev
*pctldev
,
331 struct pinctrl_gpio_range
*range
)
333 mutex_lock(&pctldev
->gpio_ranges_lock
);
334 list_add_tail(&range
->node
, &pctldev
->gpio_ranges
);
335 mutex_unlock(&pctldev
->gpio_ranges_lock
);
337 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range
);
340 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
341 * @pctldev: pin controller device to remove the range from
342 * @range: the GPIO range to remove
344 void pinctrl_remove_gpio_range(struct pinctrl_dev
*pctldev
,
345 struct pinctrl_gpio_range
*range
)
347 mutex_lock(&pctldev
->gpio_ranges_lock
);
348 list_del(&range
->node
);
349 mutex_unlock(&pctldev
->gpio_ranges_lock
);
351 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range
);
354 * pinctrl_get_group_selector() - returns the group selector for a group
355 * @pctldev: the pin controller handling the group
356 * @pin_group: the pin group to look up
358 int pinctrl_get_group_selector(struct pinctrl_dev
*pctldev
,
359 const char *pin_group
)
361 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
362 unsigned group_selector
= 0;
364 while (pctlops
->list_groups(pctldev
, group_selector
) >= 0) {
365 const char *gname
= pctlops
->get_group_name(pctldev
,
367 if (!strcmp(gname
, pin_group
)) {
368 dev_dbg(pctldev
->dev
,
369 "found group selector %u for %s\n",
372 return group_selector
;
378 dev_err(pctldev
->dev
, "does not have pin group %s\n",
385 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
386 * @gpio: the GPIO pin number from the GPIO subsystem number space
388 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
389 * as part of their gpio_request() semantics, platforms and individual drivers
390 * shall *NOT* request GPIO pins to be muxed in.
392 int pinctrl_request_gpio(unsigned gpio
)
394 struct pinctrl_dev
*pctldev
;
395 struct pinctrl_gpio_range
*range
;
399 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
403 /* Convert to the pin controllers number space */
404 pin
= gpio
- range
->base
+ range
->pin_base
;
406 return pinmux_request_gpio(pctldev
, range
, pin
, gpio
);
408 EXPORT_SYMBOL_GPL(pinctrl_request_gpio
);
411 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
412 * @gpio: the GPIO pin number from the GPIO subsystem number space
414 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
415 * as part of their gpio_free() semantics, platforms and individual drivers
416 * shall *NOT* request GPIO pins to be muxed out.
418 void pinctrl_free_gpio(unsigned gpio
)
420 struct pinctrl_dev
*pctldev
;
421 struct pinctrl_gpio_range
*range
;
425 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
429 /* Convert to the pin controllers number space */
430 pin
= gpio
- range
->base
+ range
->pin_base
;
432 return pinmux_free_gpio(pctldev
, pin
, range
);
434 EXPORT_SYMBOL_GPL(pinctrl_free_gpio
);
436 static int pinctrl_gpio_direction(unsigned gpio
, bool input
)
438 struct pinctrl_dev
*pctldev
;
439 struct pinctrl_gpio_range
*range
;
443 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
447 /* Convert to the pin controllers number space */
448 pin
= gpio
- range
->base
+ range
->pin_base
;
450 return pinmux_gpio_direction(pctldev
, range
, pin
, input
);
454 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
455 * @gpio: the GPIO pin number from the GPIO subsystem number space
457 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
458 * as part of their gpio_direction_input() semantics, platforms and individual
459 * drivers shall *NOT* touch pin control GPIO calls.
461 int pinctrl_gpio_direction_input(unsigned gpio
)
463 return pinctrl_gpio_direction(gpio
, true);
465 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input
);
468 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
469 * @gpio: the GPIO pin number from the GPIO subsystem number space
471 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
472 * as part of their gpio_direction_output() semantics, platforms and individual
473 * drivers shall *NOT* touch pin control GPIO calls.
475 int pinctrl_gpio_direction_output(unsigned gpio
)
477 return pinctrl_gpio_direction(gpio
, false);
479 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output
);
481 static struct pinctrl
*pinctrl_get_locked(struct device
*dev
, const char *name
)
483 struct pinctrl_dev
*pctldev
= NULL
;
486 unsigned num_maps
= 0;
488 struct pinctrl_maps
*maps_node
;
490 struct pinctrl_map
const *map
;
492 /* We must have a dev name */
494 return ERR_PTR(-EINVAL
);
496 devname
= dev_name(dev
);
498 dev_dbg(dev
, "pinctrl_get() for device %s state %s\n", devname
, name
);
501 * create the state cookie holder struct pinctrl for each
502 * mapping, this is what consumers will get when requesting
503 * a pin control handle with pinctrl_get()
505 p
= kzalloc(sizeof(struct pinctrl
), GFP_KERNEL
);
507 dev_err(dev
, "failed to alloc struct pinctrl\n");
508 return ERR_PTR(-ENOMEM
);
510 mutex_init(&p
->mutex
);
511 pinmux_init_pinctrl_handle(p
);
513 /* Iterate over the pin control maps to locate the right ones */
514 for_each_maps(maps_node
, i
, map
) {
516 * First, try to find the pctldev given in the map
518 pctldev
= get_pinctrl_dev_from_devname(map
->ctrl_dev_name
);
520 dev_err(dev
, "unknown pinctrl device %s in map entry",
524 /* Eventually, this should trigger deferred probe */
525 return ERR_PTR(-ENODEV
);
528 dev_dbg(dev
, "in map, found pctldev %s to handle function %s",
529 dev_name(pctldev
->dev
), map
->function
);
531 /* Map must be for this device */
532 if (strcmp(map
->dev_name
, devname
))
536 * If we're looking for a specific named map, this must match,
537 * else we loop and look for the next.
540 if (map
->name
== NULL
)
542 if (strcmp(map
->name
, name
))
546 ret
= pinmux_apply_muxmap(pctldev
, p
, dev
, devname
, map
);
555 * This may be perfectly legitimate. An IP block may get re-used
556 * across SoCs. Not all of those SoCs may need pinmux settings for the
557 * IP block, e.g. if one SoC dedicates pins to that function but
558 * another doesn't. The driver won't know this, and will always
559 * attempt to set up the pinmux. The mapping table defines whether any
560 * HW programming is actually needed.
563 dev_info(dev
, "zero maps found for mapping %s\n", name
);
565 dev_dbg(dev
, "found %u maps for device %s state %s\n",
566 num_maps
, devname
, name
? name
: "(undefined)");
568 /* Add the pinmux to the global list */
569 mutex_lock(&pinctrl_list_mutex
);
570 list_add_tail(&p
->node
, &pinctrl_list
);
571 mutex_unlock(&pinctrl_list_mutex
);
577 * pinctrl_get() - retrieves the pin controller handle for a certain device
578 * @dev: the device to get the pin controller handle for
579 * @name: an optional specific control mapping name or NULL, the name is only
580 * needed if you want to have more than one mapping per device, or if you
581 * need an anonymous pin control (not tied to any specific device)
583 struct pinctrl
*pinctrl_get(struct device
*dev
, const char *name
)
587 mutex_lock(&pinctrl_maps_mutex
);
588 p
= pinctrl_get_locked(dev
, name
);
589 mutex_unlock(&pinctrl_maps_mutex
);
593 EXPORT_SYMBOL_GPL(pinctrl_get
);
596 * pinctrl_put() - release a previously claimed pin control handle
597 * @p: a pin control handle previously claimed by pinctrl_get()
599 void pinctrl_put(struct pinctrl
*p
)
604 mutex_lock(&p
->mutex
);
606 pr_warn("releasing pin control handle with active users!\n");
607 /* Free the groups and all acquired pins */
609 mutex_unlock(&p
->mutex
);
611 /* Remove from list */
612 mutex_lock(&pinctrl_list_mutex
);
614 mutex_unlock(&pinctrl_list_mutex
);
618 EXPORT_SYMBOL_GPL(pinctrl_put
);
621 * pinctrl_enable() - enable a certain pin controller setting
622 * @p: the pin control handle to enable, previously claimed by pinctrl_get()
624 int pinctrl_enable(struct pinctrl
*p
)
630 mutex_lock(&p
->mutex
);
631 if (p
->usecount
++ == 0) {
632 ret
= pinmux_enable(p
);
636 mutex_unlock(&p
->mutex
);
639 EXPORT_SYMBOL_GPL(pinctrl_enable
);
642 * pinctrl_disable() - disable a certain pin control setting
643 * @p: the pin control handle to disable, previously claimed by pinctrl_get()
645 void pinctrl_disable(struct pinctrl
*p
)
650 mutex_lock(&p
->mutex
);
651 if (--p
->usecount
== 0) {
654 mutex_unlock(&p
->mutex
);
656 EXPORT_SYMBOL_GPL(pinctrl_disable
);
659 * pinctrl_register_mappings() - register a set of pin controller mappings
660 * @maps: the pincontrol mappings table to register. This should probably be
661 * marked with __initdata so it can be discarded after boot. This
662 * function will perform a shallow copy for the mapping entries.
663 * @num_maps: the number of maps in the mapping table
665 int pinctrl_register_mappings(struct pinctrl_map
const *maps
,
669 struct pinctrl_maps
*maps_node
;
671 pr_debug("add %d pinmux maps\n", num_maps
);
673 /* First sanity check the new mapping */
674 for (i
= 0; i
< num_maps
; i
++) {
676 pr_err("failed to register map %d: no map name given\n",
681 if (!maps
[i
].ctrl_dev_name
) {
682 pr_err("failed to register map %s (%d): no pin control device given\n",
687 if (!maps
[i
].function
) {
688 pr_err("failed to register map %s (%d): no function ID given\n",
693 if (!maps
[i
].dev_name
) {
694 pr_err("failed to register map %s (%d): no device given\n",
700 maps_node
= kzalloc(sizeof(*maps_node
), GFP_KERNEL
);
702 pr_err("failed to alloc struct pinctrl_maps\n");
706 maps_node
->num_maps
= num_maps
;
707 maps_node
->maps
= kmemdup(maps
, sizeof(*maps
) * num_maps
, GFP_KERNEL
);
708 if (!maps_node
->maps
) {
709 pr_err("failed to duplicate mapping table\n");
714 mutex_lock(&pinctrl_maps_mutex
);
715 list_add_tail(&maps_node
->node
, &pinctrl_maps
);
716 mutex_unlock(&pinctrl_maps_mutex
);
721 /* Hog a single map entry and add to the hoglist */
722 static int pinctrl_hog_map(struct pinctrl_dev
*pctldev
,
723 struct pinctrl_map
const *map
)
725 struct pinctrl_hog
*hog
;
729 hog
= kzalloc(sizeof(struct pinctrl_hog
), GFP_KERNEL
);
731 dev_err(pctldev
->dev
, "failed to alloc struct pinctrl_hog\n");
735 p
= pinctrl_get_locked(pctldev
->dev
, map
->name
);
738 dev_err(pctldev
->dev
,
739 "could not get the %s pin control mapping for hogging\n",
744 ret
= pinctrl_enable(p
);
748 dev_err(pctldev
->dev
,
749 "could not enable the %s pin control mapping for hogging\n",
757 dev_info(pctldev
->dev
, "hogged map %s, function %s\n", map
->name
,
759 mutex_lock(&pctldev
->pinctrl_hogs_lock
);
760 list_add_tail(&hog
->node
, &pctldev
->pinctrl_hogs
);
761 mutex_unlock(&pctldev
->pinctrl_hogs_lock
);
767 * pinctrl_hog_maps() - hog specific map entries on controller device
768 * @pctldev: the pin control device to hog entries on
770 * When the pin controllers are registered, there may be some specific pinmux
771 * map entries that need to be hogged, i.e. get+enabled until the system shuts
774 static int pinctrl_hog_maps(struct pinctrl_dev
*pctldev
)
776 struct device
*dev
= pctldev
->dev
;
777 const char *devname
= dev_name(dev
);
779 struct pinctrl_maps
*maps_node
;
781 struct pinctrl_map
const *map
;
783 INIT_LIST_HEAD(&pctldev
->pinctrl_hogs
);
784 mutex_init(&pctldev
->pinctrl_hogs_lock
);
786 mutex_lock(&pinctrl_maps_mutex
);
787 for_each_maps(maps_node
, i
, map
) {
788 if (!strcmp(map
->ctrl_dev_name
, devname
) &&
789 !strcmp(map
->dev_name
, devname
)) {
790 /* OK time to hog! */
791 ret
= pinctrl_hog_map(pctldev
, map
);
793 mutex_unlock(&pinctrl_maps_mutex
);
798 mutex_unlock(&pinctrl_maps_mutex
);
804 * pinctrl_unhog_maps() - unhog specific map entries on controller device
805 * @pctldev: the pin control device to unhog entries on
807 static void pinctrl_unhog_maps(struct pinctrl_dev
*pctldev
)
809 struct list_head
*node
, *tmp
;
811 mutex_lock(&pctldev
->pinctrl_hogs_lock
);
812 list_for_each_safe(node
, tmp
, &pctldev
->pinctrl_hogs
) {
813 struct pinctrl_hog
*hog
=
814 list_entry(node
, struct pinctrl_hog
, node
);
815 pinctrl_disable(hog
->p
);
820 mutex_unlock(&pctldev
->pinctrl_hogs_lock
);
823 #ifdef CONFIG_DEBUG_FS
825 static int pinctrl_pins_show(struct seq_file
*s
, void *what
)
827 struct pinctrl_dev
*pctldev
= s
->private;
828 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
831 seq_printf(s
, "registered pins: %d\n", pctldev
->desc
->npins
);
833 /* The pin number can be retrived from the pin controller descriptor */
834 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
835 struct pin_desc
*desc
;
837 pin
= pctldev
->desc
->pins
[i
].number
;
838 desc
= pin_desc_get(pctldev
, pin
);
839 /* Pin space may be sparse */
843 seq_printf(s
, "pin %d (%s) ", pin
,
844 desc
->name
? desc
->name
: "unnamed");
846 /* Driver-specific info per pin */
847 if (ops
->pin_dbg_show
)
848 ops
->pin_dbg_show(pctldev
, s
, pin
);
856 static int pinctrl_groups_show(struct seq_file
*s
, void *what
)
858 struct pinctrl_dev
*pctldev
= s
->private;
859 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
860 unsigned selector
= 0;
866 seq_puts(s
, "registered pin groups:\n");
867 while (ops
->list_groups(pctldev
, selector
) >= 0) {
868 const unsigned *pins
;
870 const char *gname
= ops
->get_group_name(pctldev
, selector
);
874 ret
= ops
->get_group_pins(pctldev
, selector
,
877 seq_printf(s
, "%s [ERROR GETTING PINS]\n",
880 seq_printf(s
, "group: %s, pins = [ ", gname
);
881 for (i
= 0; i
< num_pins
; i
++)
882 seq_printf(s
, "%d ", pins
[i
]);
892 static int pinctrl_gpioranges_show(struct seq_file
*s
, void *what
)
894 struct pinctrl_dev
*pctldev
= s
->private;
895 struct pinctrl_gpio_range
*range
= NULL
;
897 seq_puts(s
, "GPIO ranges handled:\n");
899 /* Loop over the ranges */
900 mutex_lock(&pctldev
->gpio_ranges_lock
);
901 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
902 seq_printf(s
, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
903 range
->id
, range
->name
,
904 range
->base
, (range
->base
+ range
->npins
- 1),
906 (range
->pin_base
+ range
->npins
- 1));
908 mutex_unlock(&pctldev
->gpio_ranges_lock
);
913 static int pinctrl_maps_show(struct seq_file
*s
, void *what
)
915 struct pinctrl_maps
*maps_node
;
917 struct pinctrl_map
const *map
;
919 seq_puts(s
, "Pinctrl maps:\n");
921 mutex_lock(&pinctrl_maps_mutex
);
922 for_each_maps(maps_node
, i
, map
) {
923 seq_printf(s
, "%s:\n", map
->name
);
924 seq_printf(s
, " device: %s\n", map
->dev_name
);
925 seq_printf(s
, " controlling device %s\n", map
->ctrl_dev_name
);
926 seq_printf(s
, " function: %s\n", map
->function
);
927 seq_printf(s
, " group: %s\n", map
->group
? map
->group
:
930 mutex_unlock(&pinctrl_maps_mutex
);
935 static int pinmux_hogs_show(struct seq_file
*s
, void *what
)
937 struct pinctrl_dev
*pctldev
= s
->private;
938 struct pinctrl_hog
*hog
;
940 seq_puts(s
, "Pin control map hogs held by device\n");
942 list_for_each_entry(hog
, &pctldev
->pinctrl_hogs
, node
)
943 seq_printf(s
, "%s\n", hog
->map
->name
);
948 static int pinctrl_devices_show(struct seq_file
*s
, void *what
)
950 struct pinctrl_dev
*pctldev
;
952 seq_puts(s
, "name [pinmux] [pinconf]\n");
953 mutex_lock(&pinctrldev_list_mutex
);
954 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
955 seq_printf(s
, "%s ", pctldev
->desc
->name
);
956 if (pctldev
->desc
->pmxops
)
960 if (pctldev
->desc
->confops
)
966 mutex_unlock(&pinctrldev_list_mutex
);
971 static int pinctrl_show(struct seq_file
*s
, void *what
)
975 seq_puts(s
, "Requested pin control handlers their pinmux maps:\n");
976 list_for_each_entry(p
, &pinctrl_list
, node
) {
977 struct pinctrl_dev
*pctldev
= p
->pctldev
;
980 seq_puts(s
, "NO PIN CONTROLLER DEVICE\n");
984 seq_printf(s
, "device: %s",
985 pinctrl_dev_get_name(p
->pctldev
));
987 pinmux_dbg_show(s
, p
);
989 seq_printf(s
, " users: %u map-> %s\n",
991 p
->dev
? dev_name(p
->dev
) : "(system)");
997 static int pinctrl_pins_open(struct inode
*inode
, struct file
*file
)
999 return single_open(file
, pinctrl_pins_show
, inode
->i_private
);
1002 static int pinctrl_groups_open(struct inode
*inode
, struct file
*file
)
1004 return single_open(file
, pinctrl_groups_show
, inode
->i_private
);
1007 static int pinctrl_gpioranges_open(struct inode
*inode
, struct file
*file
)
1009 return single_open(file
, pinctrl_gpioranges_show
, inode
->i_private
);
1012 static int pinctrl_maps_open(struct inode
*inode
, struct file
*file
)
1014 return single_open(file
, pinctrl_maps_show
, inode
->i_private
);
1017 static int pinmux_hogs_open(struct inode
*inode
, struct file
*file
)
1019 return single_open(file
, pinmux_hogs_show
, inode
->i_private
);
1022 static int pinctrl_devices_open(struct inode
*inode
, struct file
*file
)
1024 return single_open(file
, pinctrl_devices_show
, NULL
);
1027 static int pinctrl_open(struct inode
*inode
, struct file
*file
)
1029 return single_open(file
, pinctrl_show
, NULL
);
1032 static const struct file_operations pinctrl_pins_ops
= {
1033 .open
= pinctrl_pins_open
,
1035 .llseek
= seq_lseek
,
1036 .release
= single_release
,
1039 static const struct file_operations pinctrl_groups_ops
= {
1040 .open
= pinctrl_groups_open
,
1042 .llseek
= seq_lseek
,
1043 .release
= single_release
,
1046 static const struct file_operations pinctrl_gpioranges_ops
= {
1047 .open
= pinctrl_gpioranges_open
,
1049 .llseek
= seq_lseek
,
1050 .release
= single_release
,
1053 static const struct file_operations pinctrl_maps_ops
= {
1054 .open
= pinctrl_maps_open
,
1056 .llseek
= seq_lseek
,
1057 .release
= single_release
,
1060 static const struct file_operations pinmux_hogs_ops
= {
1061 .open
= pinmux_hogs_open
,
1063 .llseek
= seq_lseek
,
1064 .release
= single_release
,
1067 static const struct file_operations pinctrl_devices_ops
= {
1068 .open
= pinctrl_devices_open
,
1070 .llseek
= seq_lseek
,
1071 .release
= single_release
,
1074 static const struct file_operations pinctrl_ops
= {
1075 .open
= pinctrl_open
,
1077 .llseek
= seq_lseek
,
1078 .release
= single_release
,
1081 static struct dentry
*debugfs_root
;
1083 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1085 struct dentry
*device_root
;
1087 device_root
= debugfs_create_dir(dev_name(pctldev
->dev
),
1089 pctldev
->device_root
= device_root
;
1091 if (IS_ERR(device_root
) || !device_root
) {
1092 pr_warn("failed to create debugfs directory for %s\n",
1093 dev_name(pctldev
->dev
));
1096 debugfs_create_file("pins", S_IFREG
| S_IRUGO
,
1097 device_root
, pctldev
, &pinctrl_pins_ops
);
1098 debugfs_create_file("pingroups", S_IFREG
| S_IRUGO
,
1099 device_root
, pctldev
, &pinctrl_groups_ops
);
1100 debugfs_create_file("gpio-ranges", S_IFREG
| S_IRUGO
,
1101 device_root
, pctldev
, &pinctrl_gpioranges_ops
);
1102 debugfs_create_file("pinctrl-maps", S_IFREG
| S_IRUGO
,
1103 device_root
, pctldev
, &pinctrl_maps_ops
);
1104 debugfs_create_file("pinmux-hogs", S_IFREG
| S_IRUGO
,
1105 device_root
, pctldev
, &pinmux_hogs_ops
);
1106 pinmux_init_device_debugfs(device_root
, pctldev
);
1107 pinconf_init_device_debugfs(device_root
, pctldev
);
1110 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1112 debugfs_remove_recursive(pctldev
->device_root
);
1115 static void pinctrl_init_debugfs(void)
1117 debugfs_root
= debugfs_create_dir("pinctrl", NULL
);
1118 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
1119 pr_warn("failed to create debugfs directory\n");
1120 debugfs_root
= NULL
;
1124 debugfs_create_file("pinctrl-devices", S_IFREG
| S_IRUGO
,
1125 debugfs_root
, NULL
, &pinctrl_devices_ops
);
1126 debugfs_create_file("pinctrl-handles", S_IFREG
| S_IRUGO
,
1127 debugfs_root
, NULL
, &pinctrl_ops
);
1130 #else /* CONFIG_DEBUG_FS */
1132 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1136 static void pinctrl_init_debugfs(void)
1140 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1147 * pinctrl_register() - register a pin controller device
1148 * @pctldesc: descriptor for this pin controller
1149 * @dev: parent device for this pin controller
1150 * @driver_data: private pin controller data for this pin controller
1152 struct pinctrl_dev
*pinctrl_register(struct pinctrl_desc
*pctldesc
,
1153 struct device
*dev
, void *driver_data
)
1155 struct pinctrl_dev
*pctldev
;
1158 if (pctldesc
== NULL
)
1160 if (pctldesc
->name
== NULL
)
1163 pctldev
= kzalloc(sizeof(struct pinctrl_dev
), GFP_KERNEL
);
1164 if (pctldev
== NULL
) {
1165 dev_err(dev
, "failed to alloc struct pinctrl_dev\n");
1169 /* Initialize pin control device struct */
1170 pctldev
->owner
= pctldesc
->owner
;
1171 pctldev
->desc
= pctldesc
;
1172 pctldev
->driver_data
= driver_data
;
1173 INIT_RADIX_TREE(&pctldev
->pin_desc_tree
, GFP_KERNEL
);
1174 spin_lock_init(&pctldev
->pin_desc_tree_lock
);
1175 INIT_LIST_HEAD(&pctldev
->gpio_ranges
);
1176 mutex_init(&pctldev
->gpio_ranges_lock
);
1179 /* If we're implementing pinmuxing, check the ops for sanity */
1180 if (pctldesc
->pmxops
) {
1181 ret
= pinmux_check_ops(pctldev
);
1183 pr_err("%s pinmux ops lacks necessary functions\n",
1189 /* If we're implementing pinconfig, check the ops for sanity */
1190 if (pctldesc
->confops
) {
1191 ret
= pinconf_check_ops(pctldev
);
1193 pr_err("%s pin config ops lacks necessary functions\n",
1199 /* Register all the pins */
1200 pr_debug("try to register %d pins on %s...\n",
1201 pctldesc
->npins
, pctldesc
->name
);
1202 ret
= pinctrl_register_pins(pctldev
, pctldesc
->pins
, pctldesc
->npins
);
1204 pr_err("error during pin registration\n");
1205 pinctrl_free_pindescs(pctldev
, pctldesc
->pins
,
1210 pinctrl_init_device_debugfs(pctldev
);
1211 mutex_lock(&pinctrldev_list_mutex
);
1212 list_add_tail(&pctldev
->node
, &pinctrldev_list
);
1213 mutex_unlock(&pinctrldev_list_mutex
);
1214 pinctrl_hog_maps(pctldev
);
1221 EXPORT_SYMBOL_GPL(pinctrl_register
);
1224 * pinctrl_unregister() - unregister pinmux
1225 * @pctldev: pin controller to unregister
1227 * Called by pinmux drivers to unregister a pinmux.
1229 void pinctrl_unregister(struct pinctrl_dev
*pctldev
)
1231 if (pctldev
== NULL
)
1234 pinctrl_remove_device_debugfs(pctldev
);
1235 pinctrl_unhog_maps(pctldev
);
1236 /* TODO: check that no pinmuxes are still active? */
1237 mutex_lock(&pinctrldev_list_mutex
);
1238 list_del(&pctldev
->node
);
1239 mutex_unlock(&pinctrldev_list_mutex
);
1240 /* Destroy descriptor tree */
1241 pinctrl_free_pindescs(pctldev
, pctldev
->desc
->pins
,
1242 pctldev
->desc
->npins
);
1245 EXPORT_SYMBOL_GPL(pinctrl_unregister
);
1247 static int __init
pinctrl_init(void)
1249 pr_info("initialized pinctrl subsystem\n");
1250 pinctrl_init_debugfs();
1254 /* init early since many drivers really need to initialized pinmux early */
1255 core_initcall(pinctrl_init
);