1 // SPDX-License-Identifier: GPL-2.0-only
3 * Core driver for the pin muxing portions of the pin control subsystem
5 * Copyright (C) 2011-2012 ST-Ericsson SA
6 * Written on behalf of Linaro for ST-Ericsson
7 * Based on bits of regulator core, gpio core and clk core
9 * Author: Linus Walleij <linus.walleij@linaro.org>
11 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
13 #define pr_fmt(fmt) "pinmux core: " fmt
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/device.h>
19 #include <linux/slab.h>
20 #include <linux/radix-tree.h>
21 #include <linux/err.h>
22 #include <linux/list.h>
23 #include <linux/string.h>
24 #include <linux/debugfs.h>
25 #include <linux/seq_file.h>
26 #include <linux/pinctrl/machine.h>
27 #include <linux/pinctrl/pinmux.h>
31 int pinmux_check_ops(struct pinctrl_dev
*pctldev
)
33 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
35 unsigned selector
= 0;
37 /* Check that we implement required operations */
39 !ops
->get_functions_count
||
40 !ops
->get_function_name
||
41 !ops
->get_function_groups
||
43 dev_err(pctldev
->dev
, "pinmux ops lacks necessary functions\n");
46 /* Check that all functions registered have names */
47 nfuncs
= ops
->get_functions_count(pctldev
);
48 while (selector
< nfuncs
) {
49 const char *fname
= ops
->get_function_name(pctldev
,
52 dev_err(pctldev
->dev
, "pinmux ops has no name for function%u\n",
62 int pinmux_validate_map(const struct pinctrl_map
*map
, int i
)
64 if (!map
->data
.mux
.function
) {
65 pr_err("failed to register map %s (%d): no function given\n",
74 * pinmux_can_be_used_for_gpio() - check if a specific pin
75 * is either muxed to a different function or used as gpio.
77 * @pin: the pin number in the global pin space
79 * Controllers not defined as strict will always return true,
80 * menaning that the gpio can be used.
82 bool pinmux_can_be_used_for_gpio(struct pinctrl_dev
*pctldev
, unsigned pin
)
84 struct pin_desc
*desc
= pin_desc_get(pctldev
, pin
);
85 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
87 /* Can't inspect pin, assume it can be used */
91 if (ops
->strict
&& desc
->mux_usecount
)
94 return !(ops
->strict
&& !!desc
->gpio_owner
);
98 * pin_request() - request a single pin to be muxed in, typically for GPIO
99 * @pin: the pin number in the global pin space
100 * @owner: a representation of the owner of this pin; typically the device
101 * name that controls its mux function, or the requested GPIO name
102 * @gpio_range: the range matching the GPIO pin if this is a request for a
105 static int pin_request(struct pinctrl_dev
*pctldev
,
106 int pin
, const char *owner
,
107 struct pinctrl_gpio_range
*gpio_range
)
109 struct pin_desc
*desc
;
110 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
111 int status
= -EINVAL
;
113 desc
= pin_desc_get(pctldev
, pin
);
115 dev_err(pctldev
->dev
,
116 "pin %d is not registered so it cannot be requested\n",
121 dev_dbg(pctldev
->dev
, "request pin %d (%s) for %s\n",
122 pin
, desc
->name
, owner
);
124 if ((!gpio_range
|| ops
->strict
) &&
125 desc
->mux_usecount
&& strcmp(desc
->mux_owner
, owner
)) {
126 dev_err(pctldev
->dev
,
127 "pin %s already requested by %s; cannot claim for %s\n",
128 desc
->name
, desc
->mux_owner
, owner
);
132 if ((gpio_range
|| ops
->strict
) && desc
->gpio_owner
) {
133 dev_err(pctldev
->dev
,
134 "pin %s already requested by %s; cannot claim for %s\n",
135 desc
->name
, desc
->gpio_owner
, owner
);
140 desc
->gpio_owner
= owner
;
142 desc
->mux_usecount
++;
143 if (desc
->mux_usecount
> 1)
146 desc
->mux_owner
= owner
;
149 /* Let each pin increase references to this module */
150 if (!try_module_get(pctldev
->owner
)) {
151 dev_err(pctldev
->dev
,
152 "could not increase module refcount for pin %d\n",
159 * If there is no kind of request function for the pin we just assume
160 * we got it by default and proceed.
162 if (gpio_range
&& ops
->gpio_request_enable
)
163 /* This requests and enables a single GPIO pin */
164 status
= ops
->gpio_request_enable(pctldev
, gpio_range
, pin
);
165 else if (ops
->request
)
166 status
= ops
->request(pctldev
, pin
);
171 dev_err(pctldev
->dev
, "request() failed for pin %d\n", pin
);
172 module_put(pctldev
->owner
);
178 desc
->gpio_owner
= NULL
;
180 desc
->mux_usecount
--;
181 if (!desc
->mux_usecount
)
182 desc
->mux_owner
= NULL
;
187 dev_err(pctldev
->dev
, "pin-%d (%s) status %d\n",
194 * pin_free() - release a single muxed in pin so something else can be muxed
195 * @pctldev: pin controller device handling this pin
196 * @pin: the pin to free
197 * @gpio_range: the range matching the GPIO pin if this is a request for a
200 * This function returns a pointer to the previous owner. This is used
201 * for callers that dynamically allocate an owner name so it can be freed
202 * once the pin is free. This is done for GPIO request functions.
204 static const char *pin_free(struct pinctrl_dev
*pctldev
, int pin
,
205 struct pinctrl_gpio_range
*gpio_range
)
207 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
208 struct pin_desc
*desc
;
211 desc
= pin_desc_get(pctldev
, pin
);
213 dev_err(pctldev
->dev
,
214 "pin is not registered so it cannot be freed\n");
220 * A pin should not be freed more times than allocated.
222 if (WARN_ON(!desc
->mux_usecount
))
224 desc
->mux_usecount
--;
225 if (desc
->mux_usecount
)
230 * If there is no kind of request function for the pin we just assume
231 * we got it by default and proceed.
233 if (gpio_range
&& ops
->gpio_disable_free
)
234 ops
->gpio_disable_free(pctldev
, gpio_range
, pin
);
236 ops
->free(pctldev
, pin
);
239 owner
= desc
->gpio_owner
;
240 desc
->gpio_owner
= NULL
;
242 owner
= desc
->mux_owner
;
243 desc
->mux_owner
= NULL
;
244 desc
->mux_setting
= NULL
;
247 module_put(pctldev
->owner
);
253 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
254 * @pctldev: pin controller device affected
255 * @pin: the pin to mux in for GPIO
256 * @range: the applicable GPIO range
258 int pinmux_request_gpio(struct pinctrl_dev
*pctldev
,
259 struct pinctrl_gpio_range
*range
,
260 unsigned pin
, unsigned gpio
)
265 /* Conjure some name stating what chip and pin this is taken by */
266 owner
= kasprintf(GFP_KERNEL
, "%s:%d", range
->name
, gpio
);
270 ret
= pin_request(pctldev
, pin
, owner
, range
);
278 * pinmux_free_gpio() - release a pin from GPIO muxing
279 * @pctldev: the pin controller device for the pin
280 * @pin: the affected currently GPIO-muxed in pin
281 * @range: applicable GPIO range
283 void pinmux_free_gpio(struct pinctrl_dev
*pctldev
, unsigned pin
,
284 struct pinctrl_gpio_range
*range
)
288 owner
= pin_free(pctldev
, pin
, range
);
293 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
294 * @pctldev: the pin controller handling this pin
295 * @range: applicable GPIO range
296 * @pin: the affected GPIO pin in this controller
297 * @input: true if we set the pin as input, false for output
299 int pinmux_gpio_direction(struct pinctrl_dev
*pctldev
,
300 struct pinctrl_gpio_range
*range
,
301 unsigned pin
, bool input
)
303 const struct pinmux_ops
*ops
;
306 ops
= pctldev
->desc
->pmxops
;
308 if (ops
->gpio_set_direction
)
309 ret
= ops
->gpio_set_direction(pctldev
, range
, pin
, input
);
316 static int pinmux_func_name_to_selector(struct pinctrl_dev
*pctldev
,
317 const char *function
)
319 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
320 unsigned nfuncs
= ops
->get_functions_count(pctldev
);
321 unsigned selector
= 0;
323 /* See if this pctldev has this function */
324 while (selector
< nfuncs
) {
325 const char *fname
= ops
->get_function_name(pctldev
, selector
);
327 if (!strcmp(function
, fname
))
336 int pinmux_map_to_setting(const struct pinctrl_map
*map
,
337 struct pinctrl_setting
*setting
)
339 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
340 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
341 char const * const *groups
;
347 dev_err(pctldev
->dev
, "does not support mux function\n");
351 ret
= pinmux_func_name_to_selector(pctldev
, map
->data
.mux
.function
);
353 dev_err(pctldev
->dev
, "invalid function %s in map table\n",
354 map
->data
.mux
.function
);
357 setting
->data
.mux
.func
= ret
;
359 ret
= pmxops
->get_function_groups(pctldev
, setting
->data
.mux
.func
,
360 &groups
, &num_groups
);
362 dev_err(pctldev
->dev
, "can't query groups for function %s\n",
363 map
->data
.mux
.function
);
367 dev_err(pctldev
->dev
,
368 "function %s can't be selected on any group\n",
369 map
->data
.mux
.function
);
372 if (map
->data
.mux
.group
) {
373 group
= map
->data
.mux
.group
;
374 ret
= match_string(groups
, num_groups
, group
);
376 dev_err(pctldev
->dev
,
377 "invalid group \"%s\" for function \"%s\"\n",
378 group
, map
->data
.mux
.function
);
385 ret
= pinctrl_get_group_selector(pctldev
, group
);
387 dev_err(pctldev
->dev
, "invalid group %s in map table\n",
388 map
->data
.mux
.group
);
391 setting
->data
.mux
.group
= ret
;
396 void pinmux_free_setting(const struct pinctrl_setting
*setting
)
398 /* This function is currently unused */
401 int pinmux_enable_setting(const struct pinctrl_setting
*setting
)
403 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
404 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
405 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
407 const unsigned *pins
= NULL
;
408 unsigned num_pins
= 0;
410 struct pin_desc
*desc
;
412 if (pctlops
->get_group_pins
)
413 ret
= pctlops
->get_group_pins(pctldev
, setting
->data
.mux
.group
,
419 /* errors only affect debug data, so just warn */
420 gname
= pctlops
->get_group_name(pctldev
,
421 setting
->data
.mux
.group
);
422 dev_warn(pctldev
->dev
,
423 "could not get pins for group %s\n",
428 /* Try to allocate all pins in this group, one by one */
429 for (i
= 0; i
< num_pins
; i
++) {
430 ret
= pin_request(pctldev
, pins
[i
], setting
->dev_name
, NULL
);
435 desc
= pin_desc_get(pctldev
, pins
[i
]);
436 pname
= desc
? desc
->name
: "non-existing";
437 gname
= pctlops
->get_group_name(pctldev
,
438 setting
->data
.mux
.group
);
439 dev_err(pctldev
->dev
,
440 "could not request pin %d (%s) from group %s "
442 pins
[i
], pname
, gname
,
443 pinctrl_dev_get_name(pctldev
));
444 goto err_pin_request
;
448 /* Now that we have acquired the pins, encode the mux setting */
449 for (i
= 0; i
< num_pins
; i
++) {
450 desc
= pin_desc_get(pctldev
, pins
[i
]);
452 dev_warn(pctldev
->dev
,
453 "could not get pin desc for pin %d\n",
457 desc
->mux_setting
= &(setting
->data
.mux
);
460 ret
= ops
->set_mux(pctldev
, setting
->data
.mux
.func
,
461 setting
->data
.mux
.group
);
469 for (i
= 0; i
< num_pins
; i
++) {
470 desc
= pin_desc_get(pctldev
, pins
[i
]);
472 desc
->mux_setting
= NULL
;
475 /* On error release all taken pins */
477 pin_free(pctldev
, pins
[i
], NULL
);
482 void pinmux_disable_setting(const struct pinctrl_setting
*setting
)
484 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
485 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
487 const unsigned *pins
= NULL
;
488 unsigned num_pins
= 0;
490 struct pin_desc
*desc
;
492 if (pctlops
->get_group_pins
)
493 ret
= pctlops
->get_group_pins(pctldev
, setting
->data
.mux
.group
,
498 /* errors only affect debug data, so just warn */
499 gname
= pctlops
->get_group_name(pctldev
,
500 setting
->data
.mux
.group
);
501 dev_warn(pctldev
->dev
,
502 "could not get pins for group %s\n",
507 /* Flag the descs that no setting is active */
508 for (i
= 0; i
< num_pins
; i
++) {
509 desc
= pin_desc_get(pctldev
, pins
[i
]);
511 dev_warn(pctldev
->dev
,
512 "could not get pin desc for pin %d\n",
516 if (desc
->mux_setting
== &(setting
->data
.mux
)) {
517 pin_free(pctldev
, pins
[i
], NULL
);
521 gname
= pctlops
->get_group_name(pctldev
,
522 setting
->data
.mux
.group
);
523 dev_warn(pctldev
->dev
,
524 "not freeing pin %d (%s) as part of "
525 "deactivating group %s - it is already "
526 "used for some other setting",
527 pins
[i
], desc
->name
, gname
);
532 #ifdef CONFIG_DEBUG_FS
534 /* Called from pincontrol core */
535 static int pinmux_functions_show(struct seq_file
*s
, void *what
)
537 struct pinctrl_dev
*pctldev
= s
->private;
538 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
540 unsigned func_selector
= 0;
545 mutex_lock(&pctldev
->mutex
);
546 nfuncs
= pmxops
->get_functions_count(pctldev
);
547 while (func_selector
< nfuncs
) {
548 const char *func
= pmxops
->get_function_name(pctldev
,
550 const char * const *groups
;
555 ret
= pmxops
->get_function_groups(pctldev
, func_selector
,
556 &groups
, &num_groups
);
558 seq_printf(s
, "function %s: COULD NOT GET GROUPS\n",
564 seq_printf(s
, "function: %s, groups = [ ", func
);
565 for (i
= 0; i
< num_groups
; i
++)
566 seq_printf(s
, "%s ", groups
[i
]);
572 mutex_unlock(&pctldev
->mutex
);
577 static int pinmux_pins_show(struct seq_file
*s
, void *what
)
579 struct pinctrl_dev
*pctldev
= s
->private;
580 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
581 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
587 seq_puts(s
, "Pinmux settings per pin\n");
590 "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
593 "Format: pin (name): mux_owner gpio_owner hog?\n");
595 mutex_lock(&pctldev
->mutex
);
597 /* The pin number can be retrived from the pin controller descriptor */
598 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
599 struct pin_desc
*desc
;
602 pin
= pctldev
->desc
->pins
[i
].number
;
603 desc
= pin_desc_get(pctldev
, pin
);
604 /* Skip if we cannot search the pin */
608 if (desc
->mux_owner
&&
609 !strcmp(desc
->mux_owner
, pinctrl_dev_get_name(pctldev
)))
612 if (pmxops
->strict
) {
614 seq_printf(s
, "pin %d (%s): device %s%s",
615 pin
, desc
->name
, desc
->mux_owner
,
616 is_hog
? " (HOG)" : "");
617 else if (desc
->gpio_owner
)
618 seq_printf(s
, "pin %d (%s): GPIO %s",
619 pin
, desc
->name
, desc
->gpio_owner
);
621 seq_printf(s
, "pin %d (%s): UNCLAIMED",
624 /* For non-strict controllers */
625 seq_printf(s
, "pin %d (%s): %s %s%s", pin
, desc
->name
,
626 desc
->mux_owner
? desc
->mux_owner
628 desc
->gpio_owner
? desc
->gpio_owner
629 : "(GPIO UNCLAIMED)",
630 is_hog
? " (HOG)" : "");
633 /* If mux: print function+group claiming the pin */
634 if (desc
->mux_setting
)
635 seq_printf(s
, " function %s group %s\n",
636 pmxops
->get_function_name(pctldev
,
637 desc
->mux_setting
->func
),
638 pctlops
->get_group_name(pctldev
,
639 desc
->mux_setting
->group
));
644 mutex_unlock(&pctldev
->mutex
);
649 void pinmux_show_map(struct seq_file
*s
, const struct pinctrl_map
*map
)
651 seq_printf(s
, "group %s\nfunction %s\n",
652 map
->data
.mux
.group
? map
->data
.mux
.group
: "(default)",
653 map
->data
.mux
.function
);
656 void pinmux_show_setting(struct seq_file
*s
,
657 const struct pinctrl_setting
*setting
)
659 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
660 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
661 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
663 seq_printf(s
, "group: %s (%u) function: %s (%u)\n",
664 pctlops
->get_group_name(pctldev
, setting
->data
.mux
.group
),
665 setting
->data
.mux
.group
,
666 pmxops
->get_function_name(pctldev
, setting
->data
.mux
.func
),
667 setting
->data
.mux
.func
);
670 DEFINE_SHOW_ATTRIBUTE(pinmux_functions
);
671 DEFINE_SHOW_ATTRIBUTE(pinmux_pins
);
673 void pinmux_init_device_debugfs(struct dentry
*devroot
,
674 struct pinctrl_dev
*pctldev
)
676 debugfs_create_file("pinmux-functions", S_IFREG
| S_IRUGO
,
677 devroot
, pctldev
, &pinmux_functions_fops
);
678 debugfs_create_file("pinmux-pins", S_IFREG
| S_IRUGO
,
679 devroot
, pctldev
, &pinmux_pins_fops
);
682 #endif /* CONFIG_DEBUG_FS */
684 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
687 * pinmux_generic_get_function_count() - returns number of functions
688 * @pctldev: pin controller device
690 int pinmux_generic_get_function_count(struct pinctrl_dev
*pctldev
)
692 return pctldev
->num_functions
;
694 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count
);
697 * pinmux_generic_get_function_name() - returns the function name
698 * @pctldev: pin controller device
699 * @selector: function number
702 pinmux_generic_get_function_name(struct pinctrl_dev
*pctldev
,
703 unsigned int selector
)
705 struct function_desc
*function
;
707 function
= radix_tree_lookup(&pctldev
->pin_function_tree
,
712 return function
->name
;
714 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name
);
717 * pinmux_generic_get_function_groups() - gets the function groups
718 * @pctldev: pin controller device
719 * @selector: function number
720 * @groups: array of pin groups
721 * @num_groups: number of pin groups
723 int pinmux_generic_get_function_groups(struct pinctrl_dev
*pctldev
,
724 unsigned int selector
,
725 const char * const **groups
,
726 unsigned * const num_groups
)
728 struct function_desc
*function
;
730 function
= radix_tree_lookup(&pctldev
->pin_function_tree
,
733 dev_err(pctldev
->dev
, "%s could not find function%i\n",
737 *groups
= function
->group_names
;
738 *num_groups
= function
->num_group_names
;
742 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups
);
745 * pinmux_generic_get_function() - returns a function based on the number
746 * @pctldev: pin controller device
747 * @group_selector: function number
749 struct function_desc
*pinmux_generic_get_function(struct pinctrl_dev
*pctldev
,
750 unsigned int selector
)
752 struct function_desc
*function
;
754 function
= radix_tree_lookup(&pctldev
->pin_function_tree
,
761 EXPORT_SYMBOL_GPL(pinmux_generic_get_function
);
764 * pinmux_generic_add_function() - adds a function group
765 * @pctldev: pin controller device
766 * @name: name of the function
767 * @groups: array of pin groups
768 * @num_groups: number of pin groups
769 * @data: pin controller driver specific data
771 int pinmux_generic_add_function(struct pinctrl_dev
*pctldev
,
774 const unsigned int num_groups
,
777 struct function_desc
*function
;
783 selector
= pinmux_func_name_to_selector(pctldev
, name
);
787 selector
= pctldev
->num_functions
;
789 function
= devm_kzalloc(pctldev
->dev
, sizeof(*function
), GFP_KERNEL
);
793 function
->name
= name
;
794 function
->group_names
= groups
;
795 function
->num_group_names
= num_groups
;
796 function
->data
= data
;
798 radix_tree_insert(&pctldev
->pin_function_tree
, selector
, function
);
800 pctldev
->num_functions
++;
804 EXPORT_SYMBOL_GPL(pinmux_generic_add_function
);
807 * pinmux_generic_remove_function() - removes a numbered function
808 * @pctldev: pin controller device
809 * @selector: function number
811 * Note that the caller must take care of locking.
813 int pinmux_generic_remove_function(struct pinctrl_dev
*pctldev
,
814 unsigned int selector
)
816 struct function_desc
*function
;
818 function
= radix_tree_lookup(&pctldev
->pin_function_tree
,
823 radix_tree_delete(&pctldev
->pin_function_tree
, selector
);
824 devm_kfree(pctldev
->dev
, function
);
826 pctldev
->num_functions
--;
830 EXPORT_SYMBOL_GPL(pinmux_generic_remove_function
);
833 * pinmux_generic_free_functions() - removes all functions
834 * @pctldev: pin controller device
836 * Note that the caller must take care of locking. The pinctrl
837 * functions are allocated with devm_kzalloc() so no need to free
840 void pinmux_generic_free_functions(struct pinctrl_dev
*pctldev
)
842 struct radix_tree_iter iter
;
845 radix_tree_for_each_slot(slot
, &pctldev
->pin_function_tree
, &iter
, 0)
846 radix_tree_delete(&pctldev
->pin_function_tree
, iter
.index
);
848 pctldev
->num_functions
= 0;
851 #endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */