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 * pin_request() - request a single pin to be muxed in, typically for GPIO
75 * @pin: the pin number in the global pin space
76 * @owner: a representation of the owner of this pin; typically the device
77 * name that controls its mux function, or the requested GPIO name
78 * @gpio_range: the range matching the GPIO pin if this is a request for a
81 static int pin_request(struct pinctrl_dev
*pctldev
,
82 int pin
, const char *owner
,
83 struct pinctrl_gpio_range
*gpio_range
)
85 struct pin_desc
*desc
;
86 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
89 desc
= pin_desc_get(pctldev
, pin
);
92 "pin %d is not registered so it cannot be requested\n",
97 dev_dbg(pctldev
->dev
, "request pin %d (%s) for %s\n",
98 pin
, desc
->name
, owner
);
100 if ((!gpio_range
|| ops
->strict
) &&
101 desc
->mux_usecount
&& strcmp(desc
->mux_owner
, owner
)) {
102 dev_err(pctldev
->dev
,
103 "pin %s already requested by %s; cannot claim for %s\n",
104 desc
->name
, desc
->mux_owner
, owner
);
108 if ((gpio_range
|| ops
->strict
) && desc
->gpio_owner
) {
109 dev_err(pctldev
->dev
,
110 "pin %s already requested by %s; cannot claim for %s\n",
111 desc
->name
, desc
->gpio_owner
, owner
);
116 desc
->gpio_owner
= owner
;
118 desc
->mux_usecount
++;
119 if (desc
->mux_usecount
> 1)
122 desc
->mux_owner
= owner
;
125 /* Let each pin increase references to this module */
126 if (!try_module_get(pctldev
->owner
)) {
127 dev_err(pctldev
->dev
,
128 "could not increase module refcount for pin %d\n",
135 * If there is no kind of request function for the pin we just assume
136 * we got it by default and proceed.
138 if (gpio_range
&& ops
->gpio_request_enable
)
139 /* This requests and enables a single GPIO pin */
140 status
= ops
->gpio_request_enable(pctldev
, gpio_range
, pin
);
141 else if (ops
->request
)
142 status
= ops
->request(pctldev
, pin
);
147 dev_err(pctldev
->dev
, "request() failed for pin %d\n", pin
);
148 module_put(pctldev
->owner
);
154 desc
->gpio_owner
= NULL
;
156 desc
->mux_usecount
--;
157 if (!desc
->mux_usecount
)
158 desc
->mux_owner
= NULL
;
163 dev_err(pctldev
->dev
, "pin-%d (%s) status %d\n",
170 * pin_free() - release a single muxed in pin so something else can be muxed
171 * @pctldev: pin controller device handling this pin
172 * @pin: the pin to free
173 * @gpio_range: the range matching the GPIO pin if this is a request for a
176 * This function returns a pointer to the previous owner. This is used
177 * for callers that dynamically allocate an owner name so it can be freed
178 * once the pin is free. This is done for GPIO request functions.
180 static const char *pin_free(struct pinctrl_dev
*pctldev
, int pin
,
181 struct pinctrl_gpio_range
*gpio_range
)
183 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
184 struct pin_desc
*desc
;
187 desc
= pin_desc_get(pctldev
, pin
);
189 dev_err(pctldev
->dev
,
190 "pin is not registered so it cannot be freed\n");
196 * A pin should not be freed more times than allocated.
198 if (WARN_ON(!desc
->mux_usecount
))
200 desc
->mux_usecount
--;
201 if (desc
->mux_usecount
)
206 * If there is no kind of request function for the pin we just assume
207 * we got it by default and proceed.
209 if (gpio_range
&& ops
->gpio_disable_free
)
210 ops
->gpio_disable_free(pctldev
, gpio_range
, pin
);
212 ops
->free(pctldev
, pin
);
215 owner
= desc
->gpio_owner
;
216 desc
->gpio_owner
= NULL
;
218 owner
= desc
->mux_owner
;
219 desc
->mux_owner
= NULL
;
220 desc
->mux_setting
= NULL
;
223 module_put(pctldev
->owner
);
229 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
230 * @pctldev: pin controller device affected
231 * @pin: the pin to mux in for GPIO
232 * @range: the applicable GPIO range
234 int pinmux_request_gpio(struct pinctrl_dev
*pctldev
,
235 struct pinctrl_gpio_range
*range
,
236 unsigned pin
, unsigned gpio
)
241 /* Conjure some name stating what chip and pin this is taken by */
242 owner
= kasprintf(GFP_KERNEL
, "%s:%d", range
->name
, gpio
);
246 ret
= pin_request(pctldev
, pin
, owner
, range
);
254 * pinmux_free_gpio() - release a pin from GPIO muxing
255 * @pctldev: the pin controller device for the pin
256 * @pin: the affected currently GPIO-muxed in pin
257 * @range: applicable GPIO range
259 void pinmux_free_gpio(struct pinctrl_dev
*pctldev
, unsigned pin
,
260 struct pinctrl_gpio_range
*range
)
264 owner
= pin_free(pctldev
, pin
, range
);
269 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
270 * @pctldev: the pin controller handling this pin
271 * @range: applicable GPIO range
272 * @pin: the affected GPIO pin in this controller
273 * @input: true if we set the pin as input, false for output
275 int pinmux_gpio_direction(struct pinctrl_dev
*pctldev
,
276 struct pinctrl_gpio_range
*range
,
277 unsigned pin
, bool input
)
279 const struct pinmux_ops
*ops
;
282 ops
= pctldev
->desc
->pmxops
;
284 if (ops
->gpio_set_direction
)
285 ret
= ops
->gpio_set_direction(pctldev
, range
, pin
, input
);
292 static int pinmux_func_name_to_selector(struct pinctrl_dev
*pctldev
,
293 const char *function
)
295 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
296 unsigned nfuncs
= ops
->get_functions_count(pctldev
);
297 unsigned selector
= 0;
299 /* See if this pctldev has this function */
300 while (selector
< nfuncs
) {
301 const char *fname
= ops
->get_function_name(pctldev
, selector
);
303 if (!strcmp(function
, fname
))
312 int pinmux_map_to_setting(const struct pinctrl_map
*map
,
313 struct pinctrl_setting
*setting
)
315 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
316 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
317 char const * const *groups
;
323 dev_err(pctldev
->dev
, "does not support mux function\n");
327 ret
= pinmux_func_name_to_selector(pctldev
, map
->data
.mux
.function
);
329 dev_err(pctldev
->dev
, "invalid function %s in map table\n",
330 map
->data
.mux
.function
);
333 setting
->data
.mux
.func
= ret
;
335 ret
= pmxops
->get_function_groups(pctldev
, setting
->data
.mux
.func
,
336 &groups
, &num_groups
);
338 dev_err(pctldev
->dev
, "can't query groups for function %s\n",
339 map
->data
.mux
.function
);
343 dev_err(pctldev
->dev
,
344 "function %s can't be selected on any group\n",
345 map
->data
.mux
.function
);
348 if (map
->data
.mux
.group
) {
349 group
= map
->data
.mux
.group
;
350 ret
= match_string(groups
, num_groups
, group
);
352 dev_err(pctldev
->dev
,
353 "invalid group \"%s\" for function \"%s\"\n",
354 group
, map
->data
.mux
.function
);
361 ret
= pinctrl_get_group_selector(pctldev
, group
);
363 dev_err(pctldev
->dev
, "invalid group %s in map table\n",
364 map
->data
.mux
.group
);
367 setting
->data
.mux
.group
= ret
;
372 void pinmux_free_setting(const struct pinctrl_setting
*setting
)
374 /* This function is currently unused */
377 int pinmux_enable_setting(const struct pinctrl_setting
*setting
)
379 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
380 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
381 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
383 const unsigned *pins
= NULL
;
384 unsigned num_pins
= 0;
386 struct pin_desc
*desc
;
388 if (pctlops
->get_group_pins
)
389 ret
= pctlops
->get_group_pins(pctldev
, setting
->data
.mux
.group
,
395 /* errors only affect debug data, so just warn */
396 gname
= pctlops
->get_group_name(pctldev
,
397 setting
->data
.mux
.group
);
398 dev_warn(pctldev
->dev
,
399 "could not get pins for group %s\n",
404 /* Try to allocate all pins in this group, one by one */
405 for (i
= 0; i
< num_pins
; i
++) {
406 ret
= pin_request(pctldev
, pins
[i
], setting
->dev_name
, NULL
);
411 desc
= pin_desc_get(pctldev
, pins
[i
]);
412 pname
= desc
? desc
->name
: "non-existing";
413 gname
= pctlops
->get_group_name(pctldev
,
414 setting
->data
.mux
.group
);
415 dev_err(pctldev
->dev
,
416 "could not request pin %d (%s) from group %s "
418 pins
[i
], pname
, gname
,
419 pinctrl_dev_get_name(pctldev
));
420 goto err_pin_request
;
424 /* Now that we have acquired the pins, encode the mux setting */
425 for (i
= 0; i
< num_pins
; i
++) {
426 desc
= pin_desc_get(pctldev
, pins
[i
]);
428 dev_warn(pctldev
->dev
,
429 "could not get pin desc for pin %d\n",
433 desc
->mux_setting
= &(setting
->data
.mux
);
436 ret
= ops
->set_mux(pctldev
, setting
->data
.mux
.func
,
437 setting
->data
.mux
.group
);
445 for (i
= 0; i
< num_pins
; i
++) {
446 desc
= pin_desc_get(pctldev
, pins
[i
]);
448 desc
->mux_setting
= NULL
;
451 /* On error release all taken pins */
453 pin_free(pctldev
, pins
[i
], NULL
);
458 void pinmux_disable_setting(const struct pinctrl_setting
*setting
)
460 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
461 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
463 const unsigned *pins
= NULL
;
464 unsigned num_pins
= 0;
466 struct pin_desc
*desc
;
468 if (pctlops
->get_group_pins
)
469 ret
= pctlops
->get_group_pins(pctldev
, setting
->data
.mux
.group
,
474 /* errors only affect debug data, so just warn */
475 gname
= pctlops
->get_group_name(pctldev
,
476 setting
->data
.mux
.group
);
477 dev_warn(pctldev
->dev
,
478 "could not get pins for group %s\n",
483 /* Flag the descs that no setting is active */
484 for (i
= 0; i
< num_pins
; i
++) {
485 desc
= pin_desc_get(pctldev
, pins
[i
]);
487 dev_warn(pctldev
->dev
,
488 "could not get pin desc for pin %d\n",
492 if (desc
->mux_setting
== &(setting
->data
.mux
)) {
493 pin_free(pctldev
, pins
[i
], NULL
);
497 gname
= pctlops
->get_group_name(pctldev
,
498 setting
->data
.mux
.group
);
499 dev_warn(pctldev
->dev
,
500 "not freeing pin %d (%s) as part of "
501 "deactivating group %s - it is already "
502 "used for some other setting",
503 pins
[i
], desc
->name
, gname
);
508 #ifdef CONFIG_DEBUG_FS
510 /* Called from pincontrol core */
511 static int pinmux_functions_show(struct seq_file
*s
, void *what
)
513 struct pinctrl_dev
*pctldev
= s
->private;
514 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
516 unsigned func_selector
= 0;
521 mutex_lock(&pctldev
->mutex
);
522 nfuncs
= pmxops
->get_functions_count(pctldev
);
523 while (func_selector
< nfuncs
) {
524 const char *func
= pmxops
->get_function_name(pctldev
,
526 const char * const *groups
;
531 ret
= pmxops
->get_function_groups(pctldev
, func_selector
,
532 &groups
, &num_groups
);
534 seq_printf(s
, "function %s: COULD NOT GET GROUPS\n",
540 seq_printf(s
, "function: %s, groups = [ ", func
);
541 for (i
= 0; i
< num_groups
; i
++)
542 seq_printf(s
, "%s ", groups
[i
]);
548 mutex_unlock(&pctldev
->mutex
);
553 static int pinmux_pins_show(struct seq_file
*s
, void *what
)
555 struct pinctrl_dev
*pctldev
= s
->private;
556 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
557 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
563 seq_puts(s
, "Pinmux settings per pin\n");
566 "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
569 "Format: pin (name): mux_owner gpio_owner hog?\n");
571 mutex_lock(&pctldev
->mutex
);
573 /* The pin number can be retrived from the pin controller descriptor */
574 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
575 struct pin_desc
*desc
;
578 pin
= pctldev
->desc
->pins
[i
].number
;
579 desc
= pin_desc_get(pctldev
, pin
);
580 /* Skip if we cannot search the pin */
584 if (desc
->mux_owner
&&
585 !strcmp(desc
->mux_owner
, pinctrl_dev_get_name(pctldev
)))
588 if (pmxops
->strict
) {
590 seq_printf(s
, "pin %d (%s): device %s%s",
591 pin
, desc
->name
, desc
->mux_owner
,
592 is_hog
? " (HOG)" : "");
593 else if (desc
->gpio_owner
)
594 seq_printf(s
, "pin %d (%s): GPIO %s",
595 pin
, desc
->name
, desc
->gpio_owner
);
597 seq_printf(s
, "pin %d (%s): UNCLAIMED",
600 /* For non-strict controllers */
601 seq_printf(s
, "pin %d (%s): %s %s%s", pin
, desc
->name
,
602 desc
->mux_owner
? desc
->mux_owner
604 desc
->gpio_owner
? desc
->gpio_owner
605 : "(GPIO UNCLAIMED)",
606 is_hog
? " (HOG)" : "");
609 /* If mux: print function+group claiming the pin */
610 if (desc
->mux_setting
)
611 seq_printf(s
, " function %s group %s\n",
612 pmxops
->get_function_name(pctldev
,
613 desc
->mux_setting
->func
),
614 pctlops
->get_group_name(pctldev
,
615 desc
->mux_setting
->group
));
620 mutex_unlock(&pctldev
->mutex
);
625 void pinmux_show_map(struct seq_file
*s
, const struct pinctrl_map
*map
)
627 seq_printf(s
, "group %s\nfunction %s\n",
628 map
->data
.mux
.group
? map
->data
.mux
.group
: "(default)",
629 map
->data
.mux
.function
);
632 void pinmux_show_setting(struct seq_file
*s
,
633 const struct pinctrl_setting
*setting
)
635 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
636 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
637 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
639 seq_printf(s
, "group: %s (%u) function: %s (%u)\n",
640 pctlops
->get_group_name(pctldev
, setting
->data
.mux
.group
),
641 setting
->data
.mux
.group
,
642 pmxops
->get_function_name(pctldev
, setting
->data
.mux
.func
),
643 setting
->data
.mux
.func
);
646 DEFINE_SHOW_ATTRIBUTE(pinmux_functions
);
647 DEFINE_SHOW_ATTRIBUTE(pinmux_pins
);
649 void pinmux_init_device_debugfs(struct dentry
*devroot
,
650 struct pinctrl_dev
*pctldev
)
652 debugfs_create_file("pinmux-functions", S_IFREG
| S_IRUGO
,
653 devroot
, pctldev
, &pinmux_functions_fops
);
654 debugfs_create_file("pinmux-pins", S_IFREG
| S_IRUGO
,
655 devroot
, pctldev
, &pinmux_pins_fops
);
658 #endif /* CONFIG_DEBUG_FS */
660 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
663 * pinmux_generic_get_function_count() - returns number of functions
664 * @pctldev: pin controller device
666 int pinmux_generic_get_function_count(struct pinctrl_dev
*pctldev
)
668 return pctldev
->num_functions
;
670 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count
);
673 * pinmux_generic_get_function_name() - returns the function name
674 * @pctldev: pin controller device
675 * @selector: function number
678 pinmux_generic_get_function_name(struct pinctrl_dev
*pctldev
,
679 unsigned int selector
)
681 struct function_desc
*function
;
683 function
= radix_tree_lookup(&pctldev
->pin_function_tree
,
688 return function
->name
;
690 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name
);
693 * pinmux_generic_get_function_groups() - gets the function groups
694 * @pctldev: pin controller device
695 * @selector: function number
696 * @groups: array of pin groups
697 * @num_groups: number of pin groups
699 int pinmux_generic_get_function_groups(struct pinctrl_dev
*pctldev
,
700 unsigned int selector
,
701 const char * const **groups
,
702 unsigned * const num_groups
)
704 struct function_desc
*function
;
706 function
= radix_tree_lookup(&pctldev
->pin_function_tree
,
709 dev_err(pctldev
->dev
, "%s could not find function%i\n",
713 *groups
= function
->group_names
;
714 *num_groups
= function
->num_group_names
;
718 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups
);
721 * pinmux_generic_get_function() - returns a function based on the number
722 * @pctldev: pin controller device
723 * @group_selector: function number
725 struct function_desc
*pinmux_generic_get_function(struct pinctrl_dev
*pctldev
,
726 unsigned int selector
)
728 struct function_desc
*function
;
730 function
= radix_tree_lookup(&pctldev
->pin_function_tree
,
737 EXPORT_SYMBOL_GPL(pinmux_generic_get_function
);
740 * pinmux_generic_add_function() - adds a function group
741 * @pctldev: pin controller device
742 * @name: name of the function
743 * @groups: array of pin groups
744 * @num_groups: number of pin groups
745 * @data: pin controller driver specific data
747 int pinmux_generic_add_function(struct pinctrl_dev
*pctldev
,
750 const unsigned int num_groups
,
753 struct function_desc
*function
;
759 selector
= pinmux_func_name_to_selector(pctldev
, name
);
763 selector
= pctldev
->num_functions
;
765 function
= devm_kzalloc(pctldev
->dev
, sizeof(*function
), GFP_KERNEL
);
769 function
->name
= name
;
770 function
->group_names
= groups
;
771 function
->num_group_names
= num_groups
;
772 function
->data
= data
;
774 radix_tree_insert(&pctldev
->pin_function_tree
, selector
, function
);
776 pctldev
->num_functions
++;
780 EXPORT_SYMBOL_GPL(pinmux_generic_add_function
);
783 * pinmux_generic_remove_function() - removes a numbered function
784 * @pctldev: pin controller device
785 * @selector: function number
787 * Note that the caller must take care of locking.
789 int pinmux_generic_remove_function(struct pinctrl_dev
*pctldev
,
790 unsigned int selector
)
792 struct function_desc
*function
;
794 function
= radix_tree_lookup(&pctldev
->pin_function_tree
,
799 radix_tree_delete(&pctldev
->pin_function_tree
, selector
);
800 devm_kfree(pctldev
->dev
, function
);
802 pctldev
->num_functions
--;
806 EXPORT_SYMBOL_GPL(pinmux_generic_remove_function
);
809 * pinmux_generic_free_functions() - removes all functions
810 * @pctldev: pin controller device
812 * Note that the caller must take care of locking. The pinctrl
813 * functions are allocated with devm_kzalloc() so no need to free
816 void pinmux_generic_free_functions(struct pinctrl_dev
*pctldev
)
818 struct radix_tree_iter iter
;
821 radix_tree_for_each_slot(slot
, &pctldev
->pin_function_tree
, &iter
, 0)
822 radix_tree_delete(&pctldev
->pin_function_tree
, iter
.index
);
824 pctldev
->num_functions
= 0;
827 #endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */