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/array_size.h>
16 #include <linux/ctype.h>
17 #include <linux/debugfs.h>
18 #include <linux/device.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
23 #include <linux/radix-tree.h>
24 #include <linux/seq_file.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
28 #include <linux/pinctrl/machine.h>
29 #include <linux/pinctrl/pinctrl.h>
30 #include <linux/pinctrl/pinmux.h>
35 int pinmux_check_ops(struct pinctrl_dev *pctldev)
37 const struct pinmux_ops *ops = pctldev->desc->pmxops;
39 unsigned int selector = 0;
41 /* Check that we implement required operations */
43 !ops->get_functions_count ||
44 !ops->get_function_name ||
45 !ops->get_function_groups ||
47 dev_err(pctldev->dev, "pinmux ops lacks necessary functions\n");
50 /* Check that all functions registered have names */
51 nfuncs = ops->get_functions_count(pctldev);
52 while (selector < nfuncs) {
53 const char *fname = ops->get_function_name(pctldev,
56 dev_err(pctldev->dev, "pinmux ops has no name for function%u\n",
66 int pinmux_validate_map(const struct pinctrl_map *map, int i)
68 if (!map->data.mux.function) {
69 pr_err("failed to register map %s (%d): no function given\n",
78 * pinmux_can_be_used_for_gpio() - check if a specific pin
79 * is either muxed to a different function or used as gpio.
81 * @pctldev: the associated pin controller device
82 * @pin: the pin number in the global pin space
84 * Controllers not defined as strict will always return true,
85 * menaning that the gpio can be used.
87 bool pinmux_can_be_used_for_gpio(struct pinctrl_dev *pctldev, unsigned int pin)
89 struct pin_desc *desc = pin_desc_get(pctldev, pin);
90 const struct pinmux_ops *ops = pctldev->desc->pmxops;
92 /* Can't inspect pin, assume it can be used */
96 if (ops->strict && desc->mux_usecount)
99 return !(ops->strict && !!desc->gpio_owner);
103 * pin_request() - request a single pin to be muxed in, typically for GPIO
104 * @pctldev: the associated pin controller device
105 * @pin: the pin number in the global pin space
106 * @owner: a representation of the owner of this pin; typically the device
107 * name that controls its mux function, or the requested GPIO name
108 * @gpio_range: the range matching the GPIO pin if this is a request for a
111 static int pin_request(struct pinctrl_dev *pctldev,
112 int pin, const char *owner,
113 struct pinctrl_gpio_range *gpio_range)
115 struct pin_desc *desc;
116 const struct pinmux_ops *ops = pctldev->desc->pmxops;
117 int status = -EINVAL;
119 desc = pin_desc_get(pctldev, pin);
121 dev_err(pctldev->dev,
122 "pin %d is not registered so it cannot be requested\n",
127 dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
128 pin, desc->name, owner);
130 if ((!gpio_range || ops->strict) &&
131 desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
132 dev_err(pctldev->dev,
133 "pin %s already requested by %s; cannot claim for %s\n",
134 desc->name, desc->mux_owner, owner);
138 if ((gpio_range || ops->strict) && desc->gpio_owner) {
139 dev_err(pctldev->dev,
140 "pin %s already requested by %s; cannot claim for %s\n",
141 desc->name, desc->gpio_owner, owner);
146 desc->gpio_owner = owner;
148 desc->mux_usecount++;
149 if (desc->mux_usecount > 1)
152 desc->mux_owner = owner;
155 /* Let each pin increase references to this module */
156 if (!try_module_get(pctldev->owner)) {
157 dev_err(pctldev->dev,
158 "could not increase module refcount for pin %d\n",
165 * If there is no kind of request function for the pin we just assume
166 * we got it by default and proceed.
168 if (gpio_range && ops->gpio_request_enable)
169 /* This requests and enables a single GPIO pin */
170 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
171 else if (ops->request)
172 status = ops->request(pctldev, pin);
177 module_put(pctldev->owner);
182 desc->gpio_owner = NULL;
184 desc->mux_usecount--;
185 if (!desc->mux_usecount)
186 desc->mux_owner = NULL;
191 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
198 * pin_free() - release a single muxed in pin so something else can be muxed
199 * @pctldev: pin controller device handling this pin
200 * @pin: the pin to free
201 * @gpio_range: the range matching the GPIO pin if this is a request for a
204 * This function returns a pointer to the previous owner. This is used
205 * for callers that dynamically allocate an owner name so it can be freed
206 * once the pin is free. This is done for GPIO request functions.
208 static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
209 struct pinctrl_gpio_range *gpio_range)
211 const struct pinmux_ops *ops = pctldev->desc->pmxops;
212 struct pin_desc *desc;
215 desc = pin_desc_get(pctldev, pin);
217 dev_err(pctldev->dev,
218 "pin is not registered so it cannot be freed\n");
224 * A pin should not be freed more times than allocated.
226 if (WARN_ON(!desc->mux_usecount))
228 desc->mux_usecount--;
229 if (desc->mux_usecount)
234 * If there is no kind of request function for the pin we just assume
235 * we got it by default and proceed.
237 if (gpio_range && ops->gpio_disable_free)
238 ops->gpio_disable_free(pctldev, gpio_range, pin);
240 ops->free(pctldev, pin);
243 owner = desc->gpio_owner;
244 desc->gpio_owner = NULL;
246 owner = desc->mux_owner;
247 desc->mux_owner = NULL;
248 desc->mux_setting = NULL;
251 module_put(pctldev->owner);
257 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
258 * @pctldev: pin controller device affected
259 * @pin: the pin to mux in for GPIO
260 * @range: the applicable GPIO range
261 * @gpio: number of requested GPIO
263 int pinmux_request_gpio(struct pinctrl_dev *pctldev,
264 struct pinctrl_gpio_range *range,
265 unsigned int pin, unsigned int gpio)
270 /* Conjure some name stating what chip and pin this is taken by */
271 owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
275 ret = pin_request(pctldev, pin, owner, range);
283 * pinmux_free_gpio() - release a pin from GPIO muxing
284 * @pctldev: the pin controller device for the pin
285 * @pin: the affected currently GPIO-muxed in pin
286 * @range: applicable GPIO range
288 void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned int pin,
289 struct pinctrl_gpio_range *range)
293 owner = pin_free(pctldev, pin, range);
298 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
299 * @pctldev: the pin controller handling this pin
300 * @range: applicable GPIO range
301 * @pin: the affected GPIO pin in this controller
302 * @input: true if we set the pin as input, false for output
304 int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
305 struct pinctrl_gpio_range *range,
306 unsigned int pin, bool input)
308 const struct pinmux_ops *ops;
311 ops = pctldev->desc->pmxops;
313 if (ops->gpio_set_direction)
314 ret = ops->gpio_set_direction(pctldev, range, pin, input);
321 static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
322 const char *function)
324 const struct pinmux_ops *ops = pctldev->desc->pmxops;
325 unsigned int nfuncs = ops->get_functions_count(pctldev);
326 unsigned int selector = 0;
328 /* See if this pctldev has this function */
329 while (selector < nfuncs) {
330 const char *fname = ops->get_function_name(pctldev, selector);
332 if (!strcmp(function, fname))
341 int pinmux_map_to_setting(const struct pinctrl_map *map,
342 struct pinctrl_setting *setting)
344 struct pinctrl_dev *pctldev = setting->pctldev;
345 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
346 char const * const *groups;
347 unsigned int num_groups;
352 dev_err(pctldev->dev, "does not support mux function\n");
356 ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
358 dev_err(pctldev->dev, "invalid function %s in map table\n",
359 map->data.mux.function);
362 setting->data.mux.func = ret;
364 ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
365 &groups, &num_groups);
367 dev_err(pctldev->dev, "can't query groups for function %s\n",
368 map->data.mux.function);
372 dev_err(pctldev->dev,
373 "function %s can't be selected on any group\n",
374 map->data.mux.function);
377 if (map->data.mux.group) {
378 group = map->data.mux.group;
379 ret = match_string(groups, num_groups, group);
381 dev_err(pctldev->dev,
382 "invalid group \"%s\" for function \"%s\"\n",
383 group, map->data.mux.function);
390 ret = pinctrl_get_group_selector(pctldev, group);
392 dev_err(pctldev->dev, "invalid group %s in map table\n",
393 map->data.mux.group);
396 setting->data.mux.group = ret;
401 void pinmux_free_setting(const struct pinctrl_setting *setting)
403 /* This function is currently unused */
406 int pinmux_enable_setting(const struct pinctrl_setting *setting)
408 struct pinctrl_dev *pctldev = setting->pctldev;
409 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
410 const struct pinmux_ops *ops = pctldev->desc->pmxops;
412 const unsigned int *pins = NULL;
413 unsigned int num_pins = 0;
415 struct pin_desc *desc;
417 if (pctlops->get_group_pins)
418 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
424 /* errors only affect debug data, so just warn */
425 gname = pctlops->get_group_name(pctldev,
426 setting->data.mux.group);
427 dev_warn(pctldev->dev,
428 "could not get pins for group %s\n",
433 /* Try to allocate all pins in this group, one by one */
434 for (i = 0; i < num_pins; i++) {
435 ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
440 desc = pin_desc_get(pctldev, pins[i]);
441 pname = desc ? desc->name : "non-existing";
442 gname = pctlops->get_group_name(pctldev,
443 setting->data.mux.group);
444 dev_err(pctldev->dev,
445 "could not request pin %d (%s) from group %s "
447 pins[i], pname, gname,
448 pinctrl_dev_get_name(pctldev));
449 goto err_pin_request;
453 /* Now that we have acquired the pins, encode the mux setting */
454 for (i = 0; i < num_pins; i++) {
455 desc = pin_desc_get(pctldev, pins[i]);
457 dev_warn(pctldev->dev,
458 "could not get pin desc for pin %d\n",
462 desc->mux_setting = &(setting->data.mux);
465 ret = ops->set_mux(pctldev, setting->data.mux.func,
466 setting->data.mux.group);
474 for (i = 0; i < num_pins; i++) {
475 desc = pin_desc_get(pctldev, pins[i]);
477 desc->mux_setting = NULL;
480 /* On error release all taken pins */
482 pin_free(pctldev, pins[i], NULL);
487 void pinmux_disable_setting(const struct pinctrl_setting *setting)
489 struct pinctrl_dev *pctldev = setting->pctldev;
490 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
492 const unsigned int *pins = NULL;
493 unsigned int num_pins = 0;
495 struct pin_desc *desc;
497 if (pctlops->get_group_pins)
498 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
503 /* errors only affect debug data, so just warn */
504 gname = pctlops->get_group_name(pctldev,
505 setting->data.mux.group);
506 dev_warn(pctldev->dev,
507 "could not get pins for group %s\n",
512 /* Flag the descs that no setting is active */
513 for (i = 0; i < num_pins; i++) {
514 desc = pin_desc_get(pctldev, pins[i]);
516 dev_warn(pctldev->dev,
517 "could not get pin desc for pin %d\n",
521 if (desc->mux_setting == &(setting->data.mux)) {
522 pin_free(pctldev, pins[i], NULL);
526 gname = pctlops->get_group_name(pctldev,
527 setting->data.mux.group);
528 dev_warn(pctldev->dev,
529 "not freeing pin %d (%s) as part of "
530 "deactivating group %s - it is already "
531 "used for some other setting",
532 pins[i], desc->name, gname);
537 #ifdef CONFIG_DEBUG_FS
539 /* Called from pincontrol core */
540 static int pinmux_functions_show(struct seq_file *s, void *what)
542 struct pinctrl_dev *pctldev = s->private;
543 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
545 unsigned int func_selector = 0;
550 mutex_lock(&pctldev->mutex);
551 nfuncs = pmxops->get_functions_count(pctldev);
552 while (func_selector < nfuncs) {
553 const char *func = pmxops->get_function_name(pctldev,
555 const char * const *groups;
556 unsigned int num_groups;
560 ret = pmxops->get_function_groups(pctldev, func_selector,
561 &groups, &num_groups);
563 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
569 seq_printf(s, "function %d: %s, groups = [ ", func_selector, func);
570 for (i = 0; i < num_groups; i++)
571 seq_printf(s, "%s ", groups[i]);
577 mutex_unlock(&pctldev->mutex);
582 static int pinmux_pins_show(struct seq_file *s, void *what)
584 struct pinctrl_dev *pctldev = s->private;
585 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
586 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
592 seq_puts(s, "Pinmux settings per pin\n");
595 "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
598 "Format: pin (name): mux_owner gpio_owner hog?\n");
600 mutex_lock(&pctldev->mutex);
602 /* The pin number can be retrived from the pin controller descriptor */
603 for (i = 0; i < pctldev->desc->npins; i++) {
604 struct pin_desc *desc;
607 pin = pctldev->desc->pins[i].number;
608 desc = pin_desc_get(pctldev, pin);
609 /* Skip if we cannot search the pin */
613 if (desc->mux_owner &&
614 !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
617 if (pmxops->strict) {
619 seq_printf(s, "pin %d (%s): device %s%s",
620 pin, desc->name, desc->mux_owner,
621 is_hog ? " (HOG)" : "");
622 else if (desc->gpio_owner)
623 seq_printf(s, "pin %d (%s): GPIO %s",
624 pin, desc->name, desc->gpio_owner);
626 seq_printf(s, "pin %d (%s): UNCLAIMED",
629 /* For non-strict controllers */
630 seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name,
631 desc->mux_owner ? desc->mux_owner
633 desc->gpio_owner ? desc->gpio_owner
634 : "(GPIO UNCLAIMED)",
635 is_hog ? " (HOG)" : "");
638 /* If mux: print function+group claiming the pin */
639 if (desc->mux_setting)
640 seq_printf(s, " function %s group %s\n",
641 pmxops->get_function_name(pctldev,
642 desc->mux_setting->func),
643 pctlops->get_group_name(pctldev,
644 desc->mux_setting->group));
649 mutex_unlock(&pctldev->mutex);
654 void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map)
656 seq_printf(s, "group %s\nfunction %s\n",
657 map->data.mux.group ? map->data.mux.group : "(default)",
658 map->data.mux.function);
661 void pinmux_show_setting(struct seq_file *s,
662 const struct pinctrl_setting *setting)
664 struct pinctrl_dev *pctldev = setting->pctldev;
665 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
666 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
668 seq_printf(s, "group: %s (%u) function: %s (%u)\n",
669 pctlops->get_group_name(pctldev, setting->data.mux.group),
670 setting->data.mux.group,
671 pmxops->get_function_name(pctldev, setting->data.mux.func),
672 setting->data.mux.func);
675 DEFINE_SHOW_ATTRIBUTE(pinmux_functions);
676 DEFINE_SHOW_ATTRIBUTE(pinmux_pins);
678 static ssize_t pinmux_select(struct file *file, const char __user *user_buf,
679 size_t len, loff_t *ppos)
681 struct seq_file *sfile = file->private_data;
682 struct pinctrl_dev *pctldev = sfile->private;
683 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
684 const char *const *groups;
685 char *buf, *gname, *fname;
686 unsigned int num_groups;
689 buf = memdup_user_nul(user_buf, len);
693 /* remove leading and trailing spaces of input buffer */
694 gname = strstrip(buf);
695 if (*gname == '\0') {
700 /* find a separator which is a spacelike character */
701 for (fname = gname; !isspace(*fname); fname++) {
702 if (*fname == '\0') {
709 /* drop extra spaces between function and group names */
710 fname = skip_spaces(fname + 1);
711 if (*fname == '\0') {
716 ret = pinmux_func_name_to_selector(pctldev, fname);
718 dev_err(pctldev->dev, "invalid function %s in map table\n", fname);
723 ret = pmxops->get_function_groups(pctldev, fsel, &groups, &num_groups);
725 dev_err(pctldev->dev, "no groups for function %d (%s)", fsel, fname);
729 ret = match_string(groups, num_groups, gname);
731 dev_err(pctldev->dev, "invalid group %s", gname);
735 ret = pinctrl_get_group_selector(pctldev, gname);
740 ret = pmxops->set_mux(pctldev, fsel, gsel);
742 dev_err(pctldev->dev, "set_mux() failed: %d", ret);
753 static int pinmux_select_open(struct inode *inode, struct file *file)
755 return single_open(file, NULL, inode->i_private);
758 static const struct file_operations pinmux_select_ops = {
759 .owner = THIS_MODULE,
760 .open = pinmux_select_open,
761 .write = pinmux_select,
763 .release = single_release,
766 void pinmux_init_device_debugfs(struct dentry *devroot,
767 struct pinctrl_dev *pctldev)
769 debugfs_create_file("pinmux-functions", 0444,
770 devroot, pctldev, &pinmux_functions_fops);
771 debugfs_create_file("pinmux-pins", 0444,
772 devroot, pctldev, &pinmux_pins_fops);
773 debugfs_create_file("pinmux-select", 0200,
774 devroot, pctldev, &pinmux_select_ops);
777 #endif /* CONFIG_DEBUG_FS */
779 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
782 * pinmux_generic_get_function_count() - returns number of functions
783 * @pctldev: pin controller device
785 int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)
787 return pctldev->num_functions;
789 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);
792 * pinmux_generic_get_function_name() - returns the function name
793 * @pctldev: pin controller device
794 * @selector: function number
797 pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,
798 unsigned int selector)
800 struct function_desc *function;
802 function = radix_tree_lookup(&pctldev->pin_function_tree,
807 return function->name;
809 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);
812 * pinmux_generic_get_function_groups() - gets the function groups
813 * @pctldev: pin controller device
814 * @selector: function number
815 * @groups: array of pin groups
816 * @num_groups: number of pin groups
818 int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,
819 unsigned int selector,
820 const char * const **groups,
821 unsigned int * const num_groups)
823 struct function_desc *function;
825 function = radix_tree_lookup(&pctldev->pin_function_tree,
828 dev_err(pctldev->dev, "%s could not find function%i\n",
832 *groups = function->group_names;
833 *num_groups = function->num_group_names;
837 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);
840 * pinmux_generic_get_function() - returns a function based on the number
841 * @pctldev: pin controller device
842 * @selector: function number
844 struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev,
845 unsigned int selector)
847 struct function_desc *function;
849 function = radix_tree_lookup(&pctldev->pin_function_tree,
856 EXPORT_SYMBOL_GPL(pinmux_generic_get_function);
859 * pinmux_generic_add_function() - adds a function group
860 * @pctldev: pin controller device
861 * @name: name of the function
862 * @groups: array of pin groups
863 * @num_groups: number of pin groups
864 * @data: pin controller driver specific data
866 int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
868 const char * const *groups,
869 const unsigned int num_groups,
872 struct function_desc *function;
878 selector = pinmux_func_name_to_selector(pctldev, name);
882 selector = pctldev->num_functions;
884 function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
888 function->name = name;
889 function->group_names = groups;
890 function->num_group_names = num_groups;
891 function->data = data;
893 error = radix_tree_insert(&pctldev->pin_function_tree, selector, function);
897 pctldev->num_functions++;
901 EXPORT_SYMBOL_GPL(pinmux_generic_add_function);
904 * pinmux_generic_remove_function() - removes a numbered function
905 * @pctldev: pin controller device
906 * @selector: function number
908 * Note that the caller must take care of locking.
910 int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
911 unsigned int selector)
913 struct function_desc *function;
915 function = radix_tree_lookup(&pctldev->pin_function_tree,
920 radix_tree_delete(&pctldev->pin_function_tree, selector);
921 devm_kfree(pctldev->dev, function);
923 pctldev->num_functions--;
927 EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
930 * pinmux_generic_free_functions() - removes all functions
931 * @pctldev: pin controller device
933 * Note that the caller must take care of locking. The pinctrl
934 * functions are allocated with devm_kzalloc() so no need to free
937 void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
939 struct radix_tree_iter iter;
942 radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
943 radix_tree_delete(&pctldev->pin_function_tree, iter.index);
945 pctldev->num_functions = 0;
948 #endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */