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
);
218 spin_lock_init(&pindesc
->lock
);
221 pindesc
->pctldev
= pctldev
;
223 /* Copy basic pin info */
225 pindesc
->name
= name
;
227 pindesc
->name
= kasprintf(GFP_KERNEL
, "PIN%u", number
);
228 if (pindesc
->name
== NULL
)
230 pindesc
->dynamic_name
= true;
233 spin_lock(&pctldev
->pin_desc_tree_lock
);
234 radix_tree_insert(&pctldev
->pin_desc_tree
, number
, pindesc
);
235 spin_unlock(&pctldev
->pin_desc_tree_lock
);
236 pr_debug("registered pin %d (%s) on %s\n",
237 number
, pindesc
->name
, pctldev
->desc
->name
);
241 static int pinctrl_register_pins(struct pinctrl_dev
*pctldev
,
242 struct pinctrl_pin_desc
const *pins
,
248 for (i
= 0; i
< num_descs
; i
++) {
249 ret
= pinctrl_register_one_pin(pctldev
,
250 pins
[i
].number
, pins
[i
].name
);
259 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
260 * @pctldev: pin controller device to check
261 * @gpio: gpio pin to check taken from the global GPIO pin space
263 * Tries to match a GPIO pin number to the ranges handled by a certain pin
264 * controller, return the range or NULL
266 static struct pinctrl_gpio_range
*
267 pinctrl_match_gpio_range(struct pinctrl_dev
*pctldev
, unsigned gpio
)
269 struct pinctrl_gpio_range
*range
= NULL
;
271 /* Loop over the ranges */
272 mutex_lock(&pctldev
->gpio_ranges_lock
);
273 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
274 /* Check if we're in the valid range */
275 if (gpio
>= range
->base
&&
276 gpio
< range
->base
+ range
->npins
) {
277 mutex_unlock(&pctldev
->gpio_ranges_lock
);
281 mutex_unlock(&pctldev
->gpio_ranges_lock
);
287 * pinctrl_get_device_gpio_range() - find device for GPIO range
288 * @gpio: the pin to locate the pin controller for
289 * @outdev: the pin control device if found
290 * @outrange: the GPIO range if found
292 * Find the pin controller handling a certain GPIO pin from the pinspace of
293 * the GPIO subsystem, return the device and the matching GPIO range. Returns
294 * negative if the GPIO range could not be found in any device.
296 static int pinctrl_get_device_gpio_range(unsigned gpio
,
297 struct pinctrl_dev
**outdev
,
298 struct pinctrl_gpio_range
**outrange
)
300 struct pinctrl_dev
*pctldev
= NULL
;
302 /* Loop over the pin controllers */
303 mutex_lock(&pinctrldev_list_mutex
);
304 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
305 struct pinctrl_gpio_range
*range
;
307 range
= pinctrl_match_gpio_range(pctldev
, gpio
);
311 mutex_unlock(&pinctrldev_list_mutex
);
315 mutex_unlock(&pinctrldev_list_mutex
);
321 * pinctrl_add_gpio_range() - register a GPIO range for a controller
322 * @pctldev: pin controller device to add the range to
323 * @range: the GPIO range to add
325 * This adds a range of GPIOs to be handled by a certain pin controller. Call
326 * this to register handled ranges after registering your pin controller.
328 void pinctrl_add_gpio_range(struct pinctrl_dev
*pctldev
,
329 struct pinctrl_gpio_range
*range
)
331 mutex_lock(&pctldev
->gpio_ranges_lock
);
332 list_add_tail(&range
->node
, &pctldev
->gpio_ranges
);
333 mutex_unlock(&pctldev
->gpio_ranges_lock
);
335 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range
);
338 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
339 * @pctldev: pin controller device to remove the range from
340 * @range: the GPIO range to remove
342 void pinctrl_remove_gpio_range(struct pinctrl_dev
*pctldev
,
343 struct pinctrl_gpio_range
*range
)
345 mutex_lock(&pctldev
->gpio_ranges_lock
);
346 list_del(&range
->node
);
347 mutex_unlock(&pctldev
->gpio_ranges_lock
);
349 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range
);
352 * pinctrl_get_group_selector() - returns the group selector for a group
353 * @pctldev: the pin controller handling the group
354 * @pin_group: the pin group to look up
356 int pinctrl_get_group_selector(struct pinctrl_dev
*pctldev
,
357 const char *pin_group
)
359 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
360 unsigned group_selector
= 0;
362 while (pctlops
->list_groups(pctldev
, group_selector
) >= 0) {
363 const char *gname
= pctlops
->get_group_name(pctldev
,
365 if (!strcmp(gname
, pin_group
)) {
366 dev_dbg(pctldev
->dev
,
367 "found group selector %u for %s\n",
370 return group_selector
;
376 dev_err(pctldev
->dev
, "does not have pin group %s\n",
383 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
384 * @gpio: the GPIO pin number from the GPIO subsystem number space
386 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
387 * as part of their gpio_request() semantics, platforms and individual drivers
388 * shall *NOT* request GPIO pins to be muxed in.
390 int pinctrl_request_gpio(unsigned gpio
)
392 struct pinctrl_dev
*pctldev
;
393 struct pinctrl_gpio_range
*range
;
397 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
401 /* Convert to the pin controllers number space */
402 pin
= gpio
- range
->base
+ range
->pin_base
;
404 return pinmux_request_gpio(pctldev
, range
, pin
, gpio
);
406 EXPORT_SYMBOL_GPL(pinctrl_request_gpio
);
409 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
410 * @gpio: the GPIO pin number from the GPIO subsystem number space
412 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
413 * as part of their gpio_free() semantics, platforms and individual drivers
414 * shall *NOT* request GPIO pins to be muxed out.
416 void pinctrl_free_gpio(unsigned gpio
)
418 struct pinctrl_dev
*pctldev
;
419 struct pinctrl_gpio_range
*range
;
423 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
427 /* Convert to the pin controllers number space */
428 pin
= gpio
- range
->base
+ range
->pin_base
;
430 return pinmux_free_gpio(pctldev
, pin
, range
);
432 EXPORT_SYMBOL_GPL(pinctrl_free_gpio
);
434 static int pinctrl_gpio_direction(unsigned gpio
, bool input
)
436 struct pinctrl_dev
*pctldev
;
437 struct pinctrl_gpio_range
*range
;
441 ret
= pinctrl_get_device_gpio_range(gpio
, &pctldev
, &range
);
445 /* Convert to the pin controllers number space */
446 pin
= gpio
- range
->base
+ range
->pin_base
;
448 return pinmux_gpio_direction(pctldev
, range
, pin
, input
);
452 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
453 * @gpio: the GPIO pin number from the GPIO subsystem number space
455 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
456 * as part of their gpio_direction_input() semantics, platforms and individual
457 * drivers shall *NOT* touch pin control GPIO calls.
459 int pinctrl_gpio_direction_input(unsigned gpio
)
461 return pinctrl_gpio_direction(gpio
, true);
463 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input
);
466 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
467 * @gpio: the GPIO pin number from the GPIO subsystem number space
469 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
470 * as part of their gpio_direction_output() semantics, platforms and individual
471 * drivers shall *NOT* touch pin control GPIO calls.
473 int pinctrl_gpio_direction_output(unsigned gpio
)
475 return pinctrl_gpio_direction(gpio
, false);
477 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output
);
479 static struct pinctrl
*pinctrl_get_locked(struct device
*dev
, const char *name
)
481 struct pinctrl_dev
*pctldev
= NULL
;
484 unsigned num_maps
= 0;
486 struct pinctrl_maps
*maps_node
;
488 struct pinctrl_map
const *map
;
490 /* We must have a dev name */
492 return ERR_PTR(-EINVAL
);
494 devname
= dev_name(dev
);
496 pr_debug("get pin control handle device %s state %s\n", devname
, name
);
499 * create the state cookie holder struct pinctrl for each
500 * mapping, this is what consumers will get when requesting
501 * a pin control handle with pinctrl_get()
503 p
= kzalloc(sizeof(struct pinctrl
), GFP_KERNEL
);
505 return ERR_PTR(-ENOMEM
);
506 mutex_init(&p
->mutex
);
507 pinmux_init_pinctrl_handle(p
);
509 /* Iterate over the pin control maps to locate the right ones */
510 for_each_maps(maps_node
, i
, map
) {
512 * First, try to find the pctldev given in the map
514 pctldev
= get_pinctrl_dev_from_devname(map
->ctrl_dev_name
);
516 dev_err(dev
, "unknown pinctrl device %s in map entry",
520 /* Eventually, this should trigger deferred probe */
521 return ERR_PTR(-ENODEV
);
524 pr_debug("in map, found pctldev %s to handle function %s",
525 dev_name(pctldev
->dev
), map
->function
);
527 /* Map must be for this device */
528 if (strcmp(map
->dev_name
, devname
))
532 * If we're looking for a specific named map, this must match,
533 * else we loop and look for the next.
536 if (map
->name
== NULL
)
538 if (strcmp(map
->name
, name
))
542 ret
= pinmux_apply_muxmap(pctldev
, p
, dev
, devname
, map
);
551 * This may be perfectly legitimate. An IP block may get re-used
552 * across SoCs. Not all of those SoCs may need pinmux settings for the
553 * IP block, e.g. if one SoC dedicates pins to that function but
554 * another doesn't. The driver won't know this, and will always
555 * attempt to set up the pinmux. The mapping table defines whether any
556 * HW programming is actually needed.
559 dev_info(dev
, "zero maps found for mapping %s\n", name
);
561 pr_debug("found %u mux maps for device %s, UD %s\n",
562 num_maps
, devname
, name
? name
: "(undefined)");
564 /* Add the pinmux to the global list */
565 mutex_lock(&pinctrl_list_mutex
);
566 list_add_tail(&p
->node
, &pinctrl_list
);
567 mutex_unlock(&pinctrl_list_mutex
);
573 * pinctrl_get() - retrieves the pin controller handle for a certain device
574 * @dev: the device to get the pin controller handle for
575 * @name: an optional specific control mapping name or NULL, the name is only
576 * needed if you want to have more than one mapping per device, or if you
577 * need an anonymous pin control (not tied to any specific device)
579 struct pinctrl
*pinctrl_get(struct device
*dev
, const char *name
)
583 mutex_lock(&pinctrl_maps_mutex
);
584 p
= pinctrl_get_locked(dev
, name
);
585 mutex_unlock(&pinctrl_maps_mutex
);
589 EXPORT_SYMBOL_GPL(pinctrl_get
);
592 * pinctrl_put() - release a previously claimed pin control handle
593 * @p: a pin control handle previously claimed by pinctrl_get()
595 void pinctrl_put(struct pinctrl
*p
)
600 mutex_lock(&p
->mutex
);
602 pr_warn("releasing pin control handle with active users!\n");
603 /* Free the groups and all acquired pins */
605 mutex_unlock(&p
->mutex
);
607 /* Remove from list */
608 mutex_lock(&pinctrl_list_mutex
);
610 mutex_unlock(&pinctrl_list_mutex
);
614 EXPORT_SYMBOL_GPL(pinctrl_put
);
617 * pinctrl_enable() - enable a certain pin controller setting
618 * @p: the pin control handle to enable, previously claimed by pinctrl_get()
620 int pinctrl_enable(struct pinctrl
*p
)
626 mutex_lock(&p
->mutex
);
627 if (p
->usecount
++ == 0) {
628 ret
= pinmux_enable(p
);
632 mutex_unlock(&p
->mutex
);
635 EXPORT_SYMBOL_GPL(pinctrl_enable
);
638 * pinctrl_disable() - disable a certain pin control setting
639 * @p: the pin control handle to disable, previously claimed by pinctrl_get()
641 void pinctrl_disable(struct pinctrl
*p
)
646 mutex_lock(&p
->mutex
);
647 if (--p
->usecount
== 0) {
650 mutex_unlock(&p
->mutex
);
652 EXPORT_SYMBOL_GPL(pinctrl_disable
);
655 * pinctrl_register_mappings() - register a set of pin controller mappings
656 * @maps: the pincontrol mappings table to register. This should probably be
657 * marked with __initdata so it can be discarded after boot. This
658 * function will perform a shallow copy for the mapping entries.
659 * @num_maps: the number of maps in the mapping table
661 int pinctrl_register_mappings(struct pinctrl_map
const *maps
,
665 struct pinctrl_maps
*maps_node
;
667 pr_debug("add %d pinmux maps\n", num_maps
);
669 /* First sanity check the new mapping */
670 for (i
= 0; i
< num_maps
; i
++) {
672 pr_err("failed to register map %d: no map name given\n",
677 if (!maps
[i
].ctrl_dev_name
) {
678 pr_err("failed to register map %s (%d): no pin control device given\n",
683 if (!maps
[i
].function
) {
684 pr_err("failed to register map %s (%d): no function ID given\n",
689 if (!maps
[i
].dev_name
) {
690 pr_err("failed to register map %s (%d): no device given\n",
696 maps_node
= kzalloc(sizeof(*maps_node
), GFP_KERNEL
);
698 pr_err("failed to alloc struct pinctrl_maps\n");
702 maps_node
->num_maps
= num_maps
;
703 maps_node
->maps
= kmemdup(maps
, sizeof(*maps
) * num_maps
, GFP_KERNEL
);
704 if (!maps_node
->maps
) {
709 mutex_lock(&pinctrl_maps_mutex
);
710 list_add_tail(&maps_node
->node
, &pinctrl_maps
);
711 mutex_unlock(&pinctrl_maps_mutex
);
716 /* Hog a single map entry and add to the hoglist */
717 static int pinctrl_hog_map(struct pinctrl_dev
*pctldev
,
718 struct pinctrl_map
const *map
)
720 struct pinctrl_hog
*hog
;
724 hog
= kzalloc(sizeof(struct pinctrl_hog
), GFP_KERNEL
);
728 p
= pinctrl_get_locked(pctldev
->dev
, map
->name
);
731 dev_err(pctldev
->dev
,
732 "could not get the %s pin control mapping for hogging\n",
737 ret
= pinctrl_enable(p
);
741 dev_err(pctldev
->dev
,
742 "could not enable the %s pin control mapping for hogging\n",
750 dev_info(pctldev
->dev
, "hogged map %s, function %s\n", map
->name
,
752 mutex_lock(&pctldev
->pinctrl_hogs_lock
);
753 list_add_tail(&hog
->node
, &pctldev
->pinctrl_hogs
);
754 mutex_unlock(&pctldev
->pinctrl_hogs_lock
);
760 * pinctrl_hog_maps() - hog specific map entries on controller device
761 * @pctldev: the pin control device to hog entries on
763 * When the pin controllers are registered, there may be some specific pinmux
764 * map entries that need to be hogged, i.e. get+enabled until the system shuts
767 static int pinctrl_hog_maps(struct pinctrl_dev
*pctldev
)
769 struct device
*dev
= pctldev
->dev
;
770 const char *devname
= dev_name(dev
);
772 struct pinctrl_maps
*maps_node
;
774 struct pinctrl_map
const *map
;
776 INIT_LIST_HEAD(&pctldev
->pinctrl_hogs
);
777 mutex_init(&pctldev
->pinctrl_hogs_lock
);
779 mutex_lock(&pinctrl_maps_mutex
);
780 for_each_maps(maps_node
, i
, map
) {
781 if (!strcmp(map
->ctrl_dev_name
, devname
) &&
782 !strcmp(map
->dev_name
, devname
)) {
783 /* OK time to hog! */
784 ret
= pinctrl_hog_map(pctldev
, map
);
786 mutex_unlock(&pinctrl_maps_mutex
);
791 mutex_unlock(&pinctrl_maps_mutex
);
797 * pinctrl_unhog_maps() - unhog specific map entries on controller device
798 * @pctldev: the pin control device to unhog entries on
800 static void pinctrl_unhog_maps(struct pinctrl_dev
*pctldev
)
802 struct list_head
*node
, *tmp
;
804 mutex_lock(&pctldev
->pinctrl_hogs_lock
);
805 list_for_each_safe(node
, tmp
, &pctldev
->pinctrl_hogs
) {
806 struct pinctrl_hog
*hog
=
807 list_entry(node
, struct pinctrl_hog
, node
);
808 pinctrl_disable(hog
->p
);
813 mutex_unlock(&pctldev
->pinctrl_hogs_lock
);
816 #ifdef CONFIG_DEBUG_FS
818 static int pinctrl_pins_show(struct seq_file
*s
, void *what
)
820 struct pinctrl_dev
*pctldev
= s
->private;
821 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
824 seq_printf(s
, "registered pins: %d\n", pctldev
->desc
->npins
);
826 /* The pin number can be retrived from the pin controller descriptor */
827 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
828 struct pin_desc
*desc
;
830 pin
= pctldev
->desc
->pins
[i
].number
;
831 desc
= pin_desc_get(pctldev
, pin
);
832 /* Pin space may be sparse */
836 seq_printf(s
, "pin %d (%s) ", pin
,
837 desc
->name
? desc
->name
: "unnamed");
839 /* Driver-specific info per pin */
840 if (ops
->pin_dbg_show
)
841 ops
->pin_dbg_show(pctldev
, s
, pin
);
849 static int pinctrl_groups_show(struct seq_file
*s
, void *what
)
851 struct pinctrl_dev
*pctldev
= s
->private;
852 const struct pinctrl_ops
*ops
= pctldev
->desc
->pctlops
;
853 unsigned selector
= 0;
859 seq_puts(s
, "registered pin groups:\n");
860 while (ops
->list_groups(pctldev
, selector
) >= 0) {
861 const unsigned *pins
;
863 const char *gname
= ops
->get_group_name(pctldev
, selector
);
867 ret
= ops
->get_group_pins(pctldev
, selector
,
870 seq_printf(s
, "%s [ERROR GETTING PINS]\n",
873 seq_printf(s
, "group: %s, pins = [ ", gname
);
874 for (i
= 0; i
< num_pins
; i
++)
875 seq_printf(s
, "%d ", pins
[i
]);
885 static int pinctrl_gpioranges_show(struct seq_file
*s
, void *what
)
887 struct pinctrl_dev
*pctldev
= s
->private;
888 struct pinctrl_gpio_range
*range
= NULL
;
890 seq_puts(s
, "GPIO ranges handled:\n");
892 /* Loop over the ranges */
893 mutex_lock(&pctldev
->gpio_ranges_lock
);
894 list_for_each_entry(range
, &pctldev
->gpio_ranges
, node
) {
895 seq_printf(s
, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
896 range
->id
, range
->name
,
897 range
->base
, (range
->base
+ range
->npins
- 1),
899 (range
->pin_base
+ range
->npins
- 1));
901 mutex_unlock(&pctldev
->gpio_ranges_lock
);
906 static int pinctrl_maps_show(struct seq_file
*s
, void *what
)
908 struct pinctrl_maps
*maps_node
;
910 struct pinctrl_map
const *map
;
912 seq_puts(s
, "Pinctrl maps:\n");
914 mutex_lock(&pinctrl_maps_mutex
);
915 for_each_maps(maps_node
, i
, map
) {
916 seq_printf(s
, "%s:\n", map
->name
);
917 seq_printf(s
, " device: %s\n", map
->dev_name
);
918 seq_printf(s
, " controlling device %s\n", map
->ctrl_dev_name
);
919 seq_printf(s
, " function: %s\n", map
->function
);
920 seq_printf(s
, " group: %s\n", map
->group
? map
->group
:
923 mutex_unlock(&pinctrl_maps_mutex
);
928 static int pinmux_hogs_show(struct seq_file
*s
, void *what
)
930 struct pinctrl_dev
*pctldev
= s
->private;
931 struct pinctrl_hog
*hog
;
933 seq_puts(s
, "Pin control map hogs held by device\n");
935 list_for_each_entry(hog
, &pctldev
->pinctrl_hogs
, node
)
936 seq_printf(s
, "%s\n", hog
->map
->name
);
941 static int pinctrl_devices_show(struct seq_file
*s
, void *what
)
943 struct pinctrl_dev
*pctldev
;
945 seq_puts(s
, "name [pinmux] [pinconf]\n");
946 mutex_lock(&pinctrldev_list_mutex
);
947 list_for_each_entry(pctldev
, &pinctrldev_list
, node
) {
948 seq_printf(s
, "%s ", pctldev
->desc
->name
);
949 if (pctldev
->desc
->pmxops
)
953 if (pctldev
->desc
->confops
)
959 mutex_unlock(&pinctrldev_list_mutex
);
964 static int pinctrl_show(struct seq_file
*s
, void *what
)
968 seq_puts(s
, "Requested pin control handlers their pinmux maps:\n");
969 list_for_each_entry(p
, &pinctrl_list
, node
) {
970 struct pinctrl_dev
*pctldev
= p
->pctldev
;
973 seq_puts(s
, "NO PIN CONTROLLER DEVICE\n");
977 seq_printf(s
, "device: %s",
978 pinctrl_dev_get_name(p
->pctldev
));
980 pinmux_dbg_show(s
, p
);
982 seq_printf(s
, " users: %u map-> %s\n",
984 p
->dev
? dev_name(p
->dev
) : "(system)");
990 static int pinctrl_pins_open(struct inode
*inode
, struct file
*file
)
992 return single_open(file
, pinctrl_pins_show
, inode
->i_private
);
995 static int pinctrl_groups_open(struct inode
*inode
, struct file
*file
)
997 return single_open(file
, pinctrl_groups_show
, inode
->i_private
);
1000 static int pinctrl_gpioranges_open(struct inode
*inode
, struct file
*file
)
1002 return single_open(file
, pinctrl_gpioranges_show
, inode
->i_private
);
1005 static int pinctrl_maps_open(struct inode
*inode
, struct file
*file
)
1007 return single_open(file
, pinctrl_maps_show
, inode
->i_private
);
1010 static int pinmux_hogs_open(struct inode
*inode
, struct file
*file
)
1012 return single_open(file
, pinmux_hogs_show
, inode
->i_private
);
1015 static int pinctrl_devices_open(struct inode
*inode
, struct file
*file
)
1017 return single_open(file
, pinctrl_devices_show
, NULL
);
1020 static int pinctrl_open(struct inode
*inode
, struct file
*file
)
1022 return single_open(file
, pinctrl_show
, NULL
);
1025 static const struct file_operations pinctrl_pins_ops
= {
1026 .open
= pinctrl_pins_open
,
1028 .llseek
= seq_lseek
,
1029 .release
= single_release
,
1032 static const struct file_operations pinctrl_groups_ops
= {
1033 .open
= pinctrl_groups_open
,
1035 .llseek
= seq_lseek
,
1036 .release
= single_release
,
1039 static const struct file_operations pinctrl_gpioranges_ops
= {
1040 .open
= pinctrl_gpioranges_open
,
1042 .llseek
= seq_lseek
,
1043 .release
= single_release
,
1046 static const struct file_operations pinctrl_maps_ops
= {
1047 .open
= pinctrl_maps_open
,
1049 .llseek
= seq_lseek
,
1050 .release
= single_release
,
1053 static const struct file_operations pinmux_hogs_ops
= {
1054 .open
= pinmux_hogs_open
,
1056 .llseek
= seq_lseek
,
1057 .release
= single_release
,
1060 static const struct file_operations pinctrl_devices_ops
= {
1061 .open
= pinctrl_devices_open
,
1063 .llseek
= seq_lseek
,
1064 .release
= single_release
,
1067 static const struct file_operations pinctrl_ops
= {
1068 .open
= pinctrl_open
,
1070 .llseek
= seq_lseek
,
1071 .release
= single_release
,
1074 static struct dentry
*debugfs_root
;
1076 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1078 struct dentry
*device_root
;
1080 device_root
= debugfs_create_dir(dev_name(pctldev
->dev
),
1082 pctldev
->device_root
= device_root
;
1084 if (IS_ERR(device_root
) || !device_root
) {
1085 pr_warn("failed to create debugfs directory for %s\n",
1086 dev_name(pctldev
->dev
));
1089 debugfs_create_file("pins", S_IFREG
| S_IRUGO
,
1090 device_root
, pctldev
, &pinctrl_pins_ops
);
1091 debugfs_create_file("pingroups", S_IFREG
| S_IRUGO
,
1092 device_root
, pctldev
, &pinctrl_groups_ops
);
1093 debugfs_create_file("gpio-ranges", S_IFREG
| S_IRUGO
,
1094 device_root
, pctldev
, &pinctrl_gpioranges_ops
);
1095 debugfs_create_file("pinctrl-maps", S_IFREG
| S_IRUGO
,
1096 device_root
, pctldev
, &pinctrl_maps_ops
);
1097 debugfs_create_file("pinmux-hogs", S_IFREG
| S_IRUGO
,
1098 device_root
, pctldev
, &pinmux_hogs_ops
);
1099 pinmux_init_device_debugfs(device_root
, pctldev
);
1100 pinconf_init_device_debugfs(device_root
, pctldev
);
1103 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1105 debugfs_remove_recursive(pctldev
->device_root
);
1108 static void pinctrl_init_debugfs(void)
1110 debugfs_root
= debugfs_create_dir("pinctrl", NULL
);
1111 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
1112 pr_warn("failed to create debugfs directory\n");
1113 debugfs_root
= NULL
;
1117 debugfs_create_file("pinctrl-devices", S_IFREG
| S_IRUGO
,
1118 debugfs_root
, NULL
, &pinctrl_devices_ops
);
1119 debugfs_create_file("pinctrl-handles", S_IFREG
| S_IRUGO
,
1120 debugfs_root
, NULL
, &pinctrl_ops
);
1123 #else /* CONFIG_DEBUG_FS */
1125 static void pinctrl_init_device_debugfs(struct pinctrl_dev
*pctldev
)
1129 static void pinctrl_init_debugfs(void)
1133 static void pinctrl_remove_device_debugfs(struct pinctrl_dev
*pctldev
)
1140 * pinctrl_register() - register a pin controller device
1141 * @pctldesc: descriptor for this pin controller
1142 * @dev: parent device for this pin controller
1143 * @driver_data: private pin controller data for this pin controller
1145 struct pinctrl_dev
*pinctrl_register(struct pinctrl_desc
*pctldesc
,
1146 struct device
*dev
, void *driver_data
)
1148 struct pinctrl_dev
*pctldev
;
1151 if (pctldesc
== NULL
)
1153 if (pctldesc
->name
== NULL
)
1156 pctldev
= kzalloc(sizeof(struct pinctrl_dev
), GFP_KERNEL
);
1157 if (pctldev
== NULL
)
1160 /* Initialize pin control device struct */
1161 pctldev
->owner
= pctldesc
->owner
;
1162 pctldev
->desc
= pctldesc
;
1163 pctldev
->driver_data
= driver_data
;
1164 INIT_RADIX_TREE(&pctldev
->pin_desc_tree
, GFP_KERNEL
);
1165 spin_lock_init(&pctldev
->pin_desc_tree_lock
);
1166 INIT_LIST_HEAD(&pctldev
->gpio_ranges
);
1167 mutex_init(&pctldev
->gpio_ranges_lock
);
1170 /* If we're implementing pinmuxing, check the ops for sanity */
1171 if (pctldesc
->pmxops
) {
1172 ret
= pinmux_check_ops(pctldev
);
1174 pr_err("%s pinmux ops lacks necessary functions\n",
1180 /* If we're implementing pinconfig, check the ops for sanity */
1181 if (pctldesc
->confops
) {
1182 ret
= pinconf_check_ops(pctldev
);
1184 pr_err("%s pin config ops lacks necessary functions\n",
1190 /* Register all the pins */
1191 pr_debug("try to register %d pins on %s...\n",
1192 pctldesc
->npins
, pctldesc
->name
);
1193 ret
= pinctrl_register_pins(pctldev
, pctldesc
->pins
, pctldesc
->npins
);
1195 pr_err("error during pin registration\n");
1196 pinctrl_free_pindescs(pctldev
, pctldesc
->pins
,
1201 pinctrl_init_device_debugfs(pctldev
);
1202 mutex_lock(&pinctrldev_list_mutex
);
1203 list_add_tail(&pctldev
->node
, &pinctrldev_list
);
1204 mutex_unlock(&pinctrldev_list_mutex
);
1205 pinctrl_hog_maps(pctldev
);
1212 EXPORT_SYMBOL_GPL(pinctrl_register
);
1215 * pinctrl_unregister() - unregister pinmux
1216 * @pctldev: pin controller to unregister
1218 * Called by pinmux drivers to unregister a pinmux.
1220 void pinctrl_unregister(struct pinctrl_dev
*pctldev
)
1222 if (pctldev
== NULL
)
1225 pinctrl_remove_device_debugfs(pctldev
);
1226 pinctrl_unhog_maps(pctldev
);
1227 /* TODO: check that no pinmuxes are still active? */
1228 mutex_lock(&pinctrldev_list_mutex
);
1229 list_del(&pctldev
->node
);
1230 mutex_unlock(&pinctrldev_list_mutex
);
1231 /* Destroy descriptor tree */
1232 pinctrl_free_pindescs(pctldev
, pctldev
->desc
->pins
,
1233 pctldev
->desc
->npins
);
1236 EXPORT_SYMBOL_GPL(pinctrl_unregister
);
1238 static int __init
pinctrl_init(void)
1240 pr_info("initialized pinctrl subsystem\n");
1241 pinctrl_init_debugfs();
1245 /* init early since many drivers really need to initialized pinmux early */
1246 core_initcall(pinctrl_init
);