2 * Core driver for the pin muxing portions of 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) "pinmux core: " fmt
16 #include <linux/kernel.h>
17 #include <linux/module.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/string.h>
25 #include <linux/debugfs.h>
26 #include <linux/seq_file.h>
27 #include <linux/pinctrl/machine.h>
28 #include <linux/pinctrl/pinmux.h>
32 int pinmux_check_ops(struct pinctrl_dev
*pctldev
)
34 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
36 unsigned selector
= 0;
38 /* Check that we implement required operations */
40 !ops
->get_functions_count
||
41 !ops
->get_function_name
||
42 !ops
->get_function_groups
||
44 dev_err(pctldev
->dev
, "pinmux ops lacks necessary functions\n");
47 /* Check that all functions registered have names */
48 nfuncs
= ops
->get_functions_count(pctldev
);
49 while (selector
< nfuncs
) {
50 const char *fname
= ops
->get_function_name(pctldev
,
53 dev_err(pctldev
->dev
, "pinmux ops has no name for function%u\n",
63 int pinmux_validate_map(const struct pinctrl_map
*map
, int i
)
65 if (!map
->data
.mux
.function
) {
66 pr_err("failed to register map %s (%d): no function given\n",
75 * pin_request() - request a single pin to be muxed in, typically for GPIO
76 * @pin: the pin number in the global pin space
77 * @owner: a representation of the owner of this pin; typically the device
78 * name that controls its mux function, or the requested GPIO name
79 * @gpio_range: the range matching the GPIO pin if this is a request for a
82 static int pin_request(struct pinctrl_dev
*pctldev
,
83 int pin
, const char *owner
,
84 struct pinctrl_gpio_range
*gpio_range
)
86 struct pin_desc
*desc
;
87 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
90 desc
= pin_desc_get(pctldev
, pin
);
93 "pin %d is not registered so it cannot be requested\n",
98 dev_dbg(pctldev
->dev
, "request pin %d (%s) for %s\n",
99 pin
, desc
->name
, owner
);
101 if ((!gpio_range
|| ops
->strict
) &&
102 desc
->mux_usecount
&& strcmp(desc
->mux_owner
, owner
)) {
103 dev_err(pctldev
->dev
,
104 "pin %s already requested by %s; cannot claim for %s\n",
105 desc
->name
, desc
->mux_owner
, owner
);
109 if ((gpio_range
|| ops
->strict
) && desc
->gpio_owner
) {
110 dev_err(pctldev
->dev
,
111 "pin %s already requested by %s; cannot claim for %s\n",
112 desc
->name
, desc
->gpio_owner
, owner
);
117 desc
->gpio_owner
= owner
;
119 desc
->mux_usecount
++;
120 if (desc
->mux_usecount
> 1)
123 desc
->mux_owner
= owner
;
126 /* Let each pin increase references to this module */
127 if (!try_module_get(pctldev
->owner
)) {
128 dev_err(pctldev
->dev
,
129 "could not increase module refcount for pin %d\n",
136 * If there is no kind of request function for the pin we just assume
137 * we got it by default and proceed.
139 if (gpio_range
&& ops
->gpio_request_enable
)
140 /* This requests and enables a single GPIO pin */
141 status
= ops
->gpio_request_enable(pctldev
, gpio_range
, pin
);
142 else if (ops
->request
)
143 status
= ops
->request(pctldev
, pin
);
148 dev_err(pctldev
->dev
, "request() failed for pin %d\n", pin
);
149 module_put(pctldev
->owner
);
155 desc
->gpio_owner
= NULL
;
157 desc
->mux_usecount
--;
158 if (!desc
->mux_usecount
)
159 desc
->mux_owner
= NULL
;
164 dev_err(pctldev
->dev
, "pin-%d (%s) status %d\n",
171 * pin_free() - release a single muxed in pin so something else can be muxed
172 * @pctldev: pin controller device handling this pin
173 * @pin: the pin to free
174 * @gpio_range: the range matching the GPIO pin if this is a request for a
177 * This function returns a pointer to the previous owner. This is used
178 * for callers that dynamically allocate an owner name so it can be freed
179 * once the pin is free. This is done for GPIO request functions.
181 static const char *pin_free(struct pinctrl_dev
*pctldev
, int pin
,
182 struct pinctrl_gpio_range
*gpio_range
)
184 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
185 struct pin_desc
*desc
;
188 desc
= pin_desc_get(pctldev
, pin
);
190 dev_err(pctldev
->dev
,
191 "pin is not registered so it cannot be freed\n");
197 * A pin should not be freed more times than allocated.
199 if (WARN_ON(!desc
->mux_usecount
))
201 desc
->mux_usecount
--;
202 if (desc
->mux_usecount
)
207 * If there is no kind of request function for the pin we just assume
208 * we got it by default and proceed.
210 if (gpio_range
&& ops
->gpio_disable_free
)
211 ops
->gpio_disable_free(pctldev
, gpio_range
, pin
);
213 ops
->free(pctldev
, pin
);
216 owner
= desc
->gpio_owner
;
217 desc
->gpio_owner
= NULL
;
219 owner
= desc
->mux_owner
;
220 desc
->mux_owner
= NULL
;
221 desc
->mux_setting
= NULL
;
224 module_put(pctldev
->owner
);
230 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
231 * @pctldev: pin controller device affected
232 * @pin: the pin to mux in for GPIO
233 * @range: the applicable GPIO range
235 int pinmux_request_gpio(struct pinctrl_dev
*pctldev
,
236 struct pinctrl_gpio_range
*range
,
237 unsigned pin
, unsigned gpio
)
242 /* Conjure some name stating what chip and pin this is taken by */
243 owner
= kasprintf(GFP_KERNEL
, "%s:%d", range
->name
, gpio
);
247 ret
= pin_request(pctldev
, pin
, owner
, range
);
255 * pinmux_free_gpio() - release a pin from GPIO muxing
256 * @pctldev: the pin controller device for the pin
257 * @pin: the affected currently GPIO-muxed in pin
258 * @range: applicable GPIO range
260 void pinmux_free_gpio(struct pinctrl_dev
*pctldev
, unsigned pin
,
261 struct pinctrl_gpio_range
*range
)
265 owner
= pin_free(pctldev
, pin
, range
);
270 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
271 * @pctldev: the pin controller handling this pin
272 * @range: applicable GPIO range
273 * @pin: the affected GPIO pin in this controller
274 * @input: true if we set the pin as input, false for output
276 int pinmux_gpio_direction(struct pinctrl_dev
*pctldev
,
277 struct pinctrl_gpio_range
*range
,
278 unsigned pin
, bool input
)
280 const struct pinmux_ops
*ops
;
283 ops
= pctldev
->desc
->pmxops
;
285 if (ops
->gpio_set_direction
)
286 ret
= ops
->gpio_set_direction(pctldev
, range
, pin
, input
);
293 static int pinmux_func_name_to_selector(struct pinctrl_dev
*pctldev
,
294 const char *function
)
296 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
297 unsigned nfuncs
= ops
->get_functions_count(pctldev
);
298 unsigned selector
= 0;
300 /* See if this pctldev has this function */
301 while (selector
< nfuncs
) {
302 const char *fname
= ops
->get_function_name(pctldev
, selector
);
304 if (!strcmp(function
, fname
))
313 int pinmux_map_to_setting(const struct pinctrl_map
*map
,
314 struct pinctrl_setting
*setting
)
316 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
317 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
318 char const * const *groups
;
324 dev_err(pctldev
->dev
, "does not support mux function\n");
328 ret
= pinmux_func_name_to_selector(pctldev
, map
->data
.mux
.function
);
330 dev_err(pctldev
->dev
, "invalid function %s in map table\n",
331 map
->data
.mux
.function
);
334 setting
->data
.mux
.func
= ret
;
336 ret
= pmxops
->get_function_groups(pctldev
, setting
->data
.mux
.func
,
337 &groups
, &num_groups
);
339 dev_err(pctldev
->dev
, "can't query groups for function %s\n",
340 map
->data
.mux
.function
);
344 dev_err(pctldev
->dev
,
345 "function %s can't be selected on any group\n",
346 map
->data
.mux
.function
);
349 if (map
->data
.mux
.group
) {
350 group
= map
->data
.mux
.group
;
351 ret
= match_string(groups
, num_groups
, group
);
353 dev_err(pctldev
->dev
,
354 "invalid group \"%s\" for function \"%s\"\n",
355 group
, map
->data
.mux
.function
);
362 ret
= pinctrl_get_group_selector(pctldev
, group
);
364 dev_err(pctldev
->dev
, "invalid group %s in map table\n",
365 map
->data
.mux
.group
);
368 setting
->data
.mux
.group
= ret
;
373 void pinmux_free_setting(const struct pinctrl_setting
*setting
)
375 /* This function is currently unused */
378 int pinmux_enable_setting(const struct pinctrl_setting
*setting
)
380 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
381 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
382 const struct pinmux_ops
*ops
= pctldev
->desc
->pmxops
;
384 const unsigned *pins
= NULL
;
385 unsigned num_pins
= 0;
387 struct pin_desc
*desc
;
389 if (pctlops
->get_group_pins
)
390 ret
= pctlops
->get_group_pins(pctldev
, setting
->data
.mux
.group
,
396 /* errors only affect debug data, so just warn */
397 gname
= pctlops
->get_group_name(pctldev
,
398 setting
->data
.mux
.group
);
399 dev_warn(pctldev
->dev
,
400 "could not get pins for group %s\n",
405 /* Try to allocate all pins in this group, one by one */
406 for (i
= 0; i
< num_pins
; i
++) {
407 ret
= pin_request(pctldev
, pins
[i
], setting
->dev_name
, NULL
);
412 desc
= pin_desc_get(pctldev
, pins
[i
]);
413 pname
= desc
? desc
->name
: "non-existing";
414 gname
= pctlops
->get_group_name(pctldev
,
415 setting
->data
.mux
.group
);
416 dev_err(pctldev
->dev
,
417 "could not request pin %d (%s) from group %s "
419 pins
[i
], pname
, gname
,
420 pinctrl_dev_get_name(pctldev
));
421 goto err_pin_request
;
425 /* Now that we have acquired the pins, encode the mux setting */
426 for (i
= 0; i
< num_pins
; i
++) {
427 desc
= pin_desc_get(pctldev
, pins
[i
]);
429 dev_warn(pctldev
->dev
,
430 "could not get pin desc for pin %d\n",
434 desc
->mux_setting
= &(setting
->data
.mux
);
437 ret
= ops
->set_mux(pctldev
, setting
->data
.mux
.func
,
438 setting
->data
.mux
.group
);
446 for (i
= 0; i
< num_pins
; i
++) {
447 desc
= pin_desc_get(pctldev
, pins
[i
]);
449 desc
->mux_setting
= NULL
;
452 /* On error release all taken pins */
454 pin_free(pctldev
, pins
[i
], NULL
);
459 void pinmux_disable_setting(const struct pinctrl_setting
*setting
)
461 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
462 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
464 const unsigned *pins
= NULL
;
465 unsigned num_pins
= 0;
467 struct pin_desc
*desc
;
469 if (pctlops
->get_group_pins
)
470 ret
= pctlops
->get_group_pins(pctldev
, setting
->data
.mux
.group
,
475 /* errors only affect debug data, so just warn */
476 gname
= pctlops
->get_group_name(pctldev
,
477 setting
->data
.mux
.group
);
478 dev_warn(pctldev
->dev
,
479 "could not get pins for group %s\n",
484 /* Flag the descs that no setting is active */
485 for (i
= 0; i
< num_pins
; i
++) {
486 desc
= pin_desc_get(pctldev
, pins
[i
]);
488 dev_warn(pctldev
->dev
,
489 "could not get pin desc for pin %d\n",
493 if (desc
->mux_setting
== &(setting
->data
.mux
)) {
494 pin_free(pctldev
, pins
[i
], NULL
);
498 gname
= pctlops
->get_group_name(pctldev
,
499 setting
->data
.mux
.group
);
500 dev_warn(pctldev
->dev
,
501 "not freeing pin %d (%s) as part of "
502 "deactivating group %s - it is already "
503 "used for some other setting",
504 pins
[i
], desc
->name
, gname
);
509 #ifdef CONFIG_DEBUG_FS
511 /* Called from pincontrol core */
512 static int pinmux_functions_show(struct seq_file
*s
, void *what
)
514 struct pinctrl_dev
*pctldev
= s
->private;
515 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
517 unsigned func_selector
= 0;
522 mutex_lock(&pctldev
->mutex
);
523 nfuncs
= pmxops
->get_functions_count(pctldev
);
524 while (func_selector
< nfuncs
) {
525 const char *func
= pmxops
->get_function_name(pctldev
,
527 const char * const *groups
;
532 ret
= pmxops
->get_function_groups(pctldev
, func_selector
,
533 &groups
, &num_groups
);
535 seq_printf(s
, "function %s: COULD NOT GET GROUPS\n",
541 seq_printf(s
, "function: %s, groups = [ ", func
);
542 for (i
= 0; i
< num_groups
; i
++)
543 seq_printf(s
, "%s ", groups
[i
]);
549 mutex_unlock(&pctldev
->mutex
);
554 static int pinmux_pins_show(struct seq_file
*s
, void *what
)
556 struct pinctrl_dev
*pctldev
= s
->private;
557 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
558 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
564 seq_puts(s
, "Pinmux settings per pin\n");
567 "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
570 "Format: pin (name): mux_owner gpio_owner hog?\n");
572 mutex_lock(&pctldev
->mutex
);
574 /* The pin number can be retrived from the pin controller descriptor */
575 for (i
= 0; i
< pctldev
->desc
->npins
; i
++) {
576 struct pin_desc
*desc
;
579 pin
= pctldev
->desc
->pins
[i
].number
;
580 desc
= pin_desc_get(pctldev
, pin
);
581 /* Skip if we cannot search the pin */
585 if (desc
->mux_owner
&&
586 !strcmp(desc
->mux_owner
, pinctrl_dev_get_name(pctldev
)))
589 if (pmxops
->strict
) {
591 seq_printf(s
, "pin %d (%s): device %s%s",
592 pin
, desc
->name
, desc
->mux_owner
,
593 is_hog
? " (HOG)" : "");
594 else if (desc
->gpio_owner
)
595 seq_printf(s
, "pin %d (%s): GPIO %s",
596 pin
, desc
->name
, desc
->gpio_owner
);
598 seq_printf(s
, "pin %d (%s): UNCLAIMED",
601 /* For non-strict controllers */
602 seq_printf(s
, "pin %d (%s): %s %s%s", pin
, desc
->name
,
603 desc
->mux_owner
? desc
->mux_owner
605 desc
->gpio_owner
? desc
->gpio_owner
606 : "(GPIO UNCLAIMED)",
607 is_hog
? " (HOG)" : "");
610 /* If mux: print function+group claiming the pin */
611 if (desc
->mux_setting
)
612 seq_printf(s
, " function %s group %s\n",
613 pmxops
->get_function_name(pctldev
,
614 desc
->mux_setting
->func
),
615 pctlops
->get_group_name(pctldev
,
616 desc
->mux_setting
->group
));
621 mutex_unlock(&pctldev
->mutex
);
626 void pinmux_show_map(struct seq_file
*s
, const struct pinctrl_map
*map
)
628 seq_printf(s
, "group %s\nfunction %s\n",
629 map
->data
.mux
.group
? map
->data
.mux
.group
: "(default)",
630 map
->data
.mux
.function
);
633 void pinmux_show_setting(struct seq_file
*s
,
634 const struct pinctrl_setting
*setting
)
636 struct pinctrl_dev
*pctldev
= setting
->pctldev
;
637 const struct pinmux_ops
*pmxops
= pctldev
->desc
->pmxops
;
638 const struct pinctrl_ops
*pctlops
= pctldev
->desc
->pctlops
;
640 seq_printf(s
, "group: %s (%u) function: %s (%u)\n",
641 pctlops
->get_group_name(pctldev
, setting
->data
.mux
.group
),
642 setting
->data
.mux
.group
,
643 pmxops
->get_function_name(pctldev
, setting
->data
.mux
.func
),
644 setting
->data
.mux
.func
);
647 static int pinmux_functions_open(struct inode
*inode
, struct file
*file
)
649 return single_open(file
, pinmux_functions_show
, inode
->i_private
);
652 static int pinmux_pins_open(struct inode
*inode
, struct file
*file
)
654 return single_open(file
, pinmux_pins_show
, inode
->i_private
);
657 static const struct file_operations pinmux_functions_ops
= {
658 .open
= pinmux_functions_open
,
661 .release
= single_release
,
664 static const struct file_operations pinmux_pins_ops
= {
665 .open
= pinmux_pins_open
,
668 .release
= single_release
,
671 void pinmux_init_device_debugfs(struct dentry
*devroot
,
672 struct pinctrl_dev
*pctldev
)
674 debugfs_create_file("pinmux-functions", S_IFREG
| S_IRUGO
,
675 devroot
, pctldev
, &pinmux_functions_ops
);
676 debugfs_create_file("pinmux-pins", S_IFREG
| S_IRUGO
,
677 devroot
, pctldev
, &pinmux_pins_ops
);
680 #endif /* CONFIG_DEBUG_FS */
682 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
685 * pinmux_generic_get_function_count() - returns number of functions
686 * @pctldev: pin controller device
688 int pinmux_generic_get_function_count(struct pinctrl_dev
*pctldev
)
690 return pctldev
->num_functions
;
692 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count
);
695 * pinmux_generic_get_function_name() - returns the function name
696 * @pctldev: pin controller device
697 * @selector: function number
700 pinmux_generic_get_function_name(struct pinctrl_dev
*pctldev
,
701 unsigned int selector
)
703 struct function_desc
*function
;
705 function
= radix_tree_lookup(&pctldev
->pin_function_tree
,
710 return function
->name
;
712 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name
);
715 * pinmux_generic_get_function_groups() - gets the function groups
716 * @pctldev: pin controller device
717 * @selector: function number
718 * @groups: array of pin groups
719 * @num_groups: number of pin groups
721 int pinmux_generic_get_function_groups(struct pinctrl_dev
*pctldev
,
722 unsigned int selector
,
723 const char * const **groups
,
724 unsigned * const num_groups
)
726 struct function_desc
*function
;
728 function
= radix_tree_lookup(&pctldev
->pin_function_tree
,
731 dev_err(pctldev
->dev
, "%s could not find function%i\n",
735 *groups
= function
->group_names
;
736 *num_groups
= function
->num_group_names
;
740 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups
);
743 * pinmux_generic_get_function() - returns a function based on the number
744 * @pctldev: pin controller device
745 * @group_selector: function number
747 struct function_desc
*pinmux_generic_get_function(struct pinctrl_dev
*pctldev
,
748 unsigned int selector
)
750 struct function_desc
*function
;
752 function
= radix_tree_lookup(&pctldev
->pin_function_tree
,
759 EXPORT_SYMBOL_GPL(pinmux_generic_get_function
);
762 * pinmux_generic_add_function() - adds a function group
763 * @pctldev: pin controller device
764 * @name: name of the function
765 * @groups: array of pin groups
766 * @num_groups: number of pin groups
767 * @data: pin controller driver specific data
769 int pinmux_generic_add_function(struct pinctrl_dev
*pctldev
,
772 const unsigned int num_groups
,
775 struct function_desc
*function
;
781 selector
= pinmux_func_name_to_selector(pctldev
, name
);
785 selector
= pctldev
->num_functions
;
787 function
= devm_kzalloc(pctldev
->dev
, sizeof(*function
), GFP_KERNEL
);
791 function
->name
= name
;
792 function
->group_names
= groups
;
793 function
->num_group_names
= num_groups
;
794 function
->data
= data
;
796 radix_tree_insert(&pctldev
->pin_function_tree
, selector
, function
);
798 pctldev
->num_functions
++;
802 EXPORT_SYMBOL_GPL(pinmux_generic_add_function
);
805 * pinmux_generic_remove_function() - removes a numbered function
806 * @pctldev: pin controller device
807 * @selector: function number
809 * Note that the caller must take care of locking.
811 int pinmux_generic_remove_function(struct pinctrl_dev
*pctldev
,
812 unsigned int selector
)
814 struct function_desc
*function
;
816 function
= radix_tree_lookup(&pctldev
->pin_function_tree
,
821 radix_tree_delete(&pctldev
->pin_function_tree
, selector
);
822 devm_kfree(pctldev
->dev
, function
);
824 pctldev
->num_functions
--;
828 EXPORT_SYMBOL_GPL(pinmux_generic_remove_function
);
831 * pinmux_generic_free_functions() - removes all functions
832 * @pctldev: pin controller device
834 * Note that the caller must take care of locking. The pinctrl
835 * functions are allocated with devm_kzalloc() so no need to free
838 void pinmux_generic_free_functions(struct pinctrl_dev
*pctldev
)
840 struct radix_tree_iter iter
;
843 radix_tree_for_each_slot(slot
, &pctldev
->pin_function_tree
, &iter
, 0)
844 radix_tree_delete(&pctldev
->pin_function_tree
, iter
.index
);
846 pctldev
->num_functions
= 0;
849 #endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */