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/ctype.h>
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 * pinmux_can_be_used_for_gpio() - check if a specific pin
76 * is either muxed to a different function or used as gpio.
78 * @pctldev: the associated pin controller device
79 * @pin: the pin number in the global pin space
81 * Controllers not defined as strict will always return true,
82 * menaning that the gpio can be used.
84 bool pinmux_can_be_used_for_gpio(struct pinctrl_dev *pctldev, unsigned pin)
86 struct pin_desc *desc = pin_desc_get(pctldev, pin);
87 const struct pinmux_ops *ops = pctldev->desc->pmxops;
89 /* Can't inspect pin, assume it can be used */
93 if (ops->strict && desc->mux_usecount)
96 return !(ops->strict && !!desc->gpio_owner);
100 * pin_request() - request a single pin to be muxed in, typically for GPIO
101 * @pctldev: the associated pin controller device
102 * @pin: the pin number in the global pin space
103 * @owner: a representation of the owner of this pin; typically the device
104 * name that controls its mux function, or the requested GPIO name
105 * @gpio_range: the range matching the GPIO pin if this is a request for a
108 static int pin_request(struct pinctrl_dev *pctldev,
109 int pin, const char *owner,
110 struct pinctrl_gpio_range *gpio_range)
112 struct pin_desc *desc;
113 const struct pinmux_ops *ops = pctldev->desc->pmxops;
114 int status = -EINVAL;
116 desc = pin_desc_get(pctldev, pin);
118 dev_err(pctldev->dev,
119 "pin %d is not registered so it cannot be requested\n",
124 dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
125 pin, desc->name, owner);
127 if ((!gpio_range || ops->strict) &&
128 desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
129 dev_err(pctldev->dev,
130 "pin %s already requested by %s; cannot claim for %s\n",
131 desc->name, desc->mux_owner, owner);
135 if ((gpio_range || ops->strict) && desc->gpio_owner) {
136 dev_err(pctldev->dev,
137 "pin %s already requested by %s; cannot claim for %s\n",
138 desc->name, desc->gpio_owner, owner);
143 desc->gpio_owner = owner;
145 desc->mux_usecount++;
146 if (desc->mux_usecount > 1)
149 desc->mux_owner = owner;
152 /* Let each pin increase references to this module */
153 if (!try_module_get(pctldev->owner)) {
154 dev_err(pctldev->dev,
155 "could not increase module refcount for pin %d\n",
162 * If there is no kind of request function for the pin we just assume
163 * we got it by default and proceed.
165 if (gpio_range && ops->gpio_request_enable)
166 /* This requests and enables a single GPIO pin */
167 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
168 else if (ops->request)
169 status = ops->request(pctldev, pin);
174 dev_err(pctldev->dev, "request() failed for pin %d\n", pin);
175 module_put(pctldev->owner);
181 desc->gpio_owner = NULL;
183 desc->mux_usecount--;
184 if (!desc->mux_usecount)
185 desc->mux_owner = NULL;
190 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
197 * pin_free() - release a single muxed in pin so something else can be muxed
198 * @pctldev: pin controller device handling this pin
199 * @pin: the pin to free
200 * @gpio_range: the range matching the GPIO pin if this is a request for a
203 * This function returns a pointer to the previous owner. This is used
204 * for callers that dynamically allocate an owner name so it can be freed
205 * once the pin is free. This is done for GPIO request functions.
207 static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
208 struct pinctrl_gpio_range *gpio_range)
210 const struct pinmux_ops *ops = pctldev->desc->pmxops;
211 struct pin_desc *desc;
214 desc = pin_desc_get(pctldev, pin);
216 dev_err(pctldev->dev,
217 "pin is not registered so it cannot be freed\n");
223 * A pin should not be freed more times than allocated.
225 if (WARN_ON(!desc->mux_usecount))
227 desc->mux_usecount--;
228 if (desc->mux_usecount)
233 * If there is no kind of request function for the pin we just assume
234 * we got it by default and proceed.
236 if (gpio_range && ops->gpio_disable_free)
237 ops->gpio_disable_free(pctldev, gpio_range, pin);
239 ops->free(pctldev, pin);
242 owner = desc->gpio_owner;
243 desc->gpio_owner = NULL;
245 owner = desc->mux_owner;
246 desc->mux_owner = NULL;
247 desc->mux_setting = NULL;
250 module_put(pctldev->owner);
256 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
257 * @pctldev: pin controller device affected
258 * @pin: the pin to mux in for GPIO
259 * @range: the applicable GPIO range
260 * @gpio: number of requested GPIO
262 int pinmux_request_gpio(struct pinctrl_dev *pctldev,
263 struct pinctrl_gpio_range *range,
264 unsigned pin, unsigned gpio)
269 /* Conjure some name stating what chip and pin this is taken by */
270 owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
274 ret = pin_request(pctldev, pin, owner, range);
282 * pinmux_free_gpio() - release a pin from GPIO muxing
283 * @pctldev: the pin controller device for the pin
284 * @pin: the affected currently GPIO-muxed in pin
285 * @range: applicable GPIO range
287 void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
288 struct pinctrl_gpio_range *range)
292 owner = pin_free(pctldev, pin, range);
297 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
298 * @pctldev: the pin controller handling this pin
299 * @range: applicable GPIO range
300 * @pin: the affected GPIO pin in this controller
301 * @input: true if we set the pin as input, false for output
303 int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
304 struct pinctrl_gpio_range *range,
305 unsigned pin, bool input)
307 const struct pinmux_ops *ops;
310 ops = pctldev->desc->pmxops;
312 if (ops->gpio_set_direction)
313 ret = ops->gpio_set_direction(pctldev, range, pin, input);
320 static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
321 const char *function)
323 const struct pinmux_ops *ops = pctldev->desc->pmxops;
324 unsigned nfuncs = ops->get_functions_count(pctldev);
325 unsigned selector = 0;
327 /* See if this pctldev has this function */
328 while (selector < nfuncs) {
329 const char *fname = ops->get_function_name(pctldev, selector);
331 if (!strcmp(function, fname))
340 int pinmux_map_to_setting(const struct pinctrl_map *map,
341 struct pinctrl_setting *setting)
343 struct pinctrl_dev *pctldev = setting->pctldev;
344 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
345 char const * const *groups;
351 dev_err(pctldev->dev, "does not support mux function\n");
355 ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
357 dev_err(pctldev->dev, "invalid function %s in map table\n",
358 map->data.mux.function);
361 setting->data.mux.func = ret;
363 ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
364 &groups, &num_groups);
366 dev_err(pctldev->dev, "can't query groups for function %s\n",
367 map->data.mux.function);
371 dev_err(pctldev->dev,
372 "function %s can't be selected on any group\n",
373 map->data.mux.function);
376 if (map->data.mux.group) {
377 group = map->data.mux.group;
378 ret = match_string(groups, num_groups, group);
380 dev_err(pctldev->dev,
381 "invalid group \"%s\" for function \"%s\"\n",
382 group, map->data.mux.function);
389 ret = pinctrl_get_group_selector(pctldev, group);
391 dev_err(pctldev->dev, "invalid group %s in map table\n",
392 map->data.mux.group);
395 setting->data.mux.group = ret;
400 void pinmux_free_setting(const struct pinctrl_setting *setting)
402 /* This function is currently unused */
405 int pinmux_enable_setting(const struct pinctrl_setting *setting)
407 struct pinctrl_dev *pctldev = setting->pctldev;
408 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
409 const struct pinmux_ops *ops = pctldev->desc->pmxops;
411 const unsigned *pins = NULL;
412 unsigned num_pins = 0;
414 struct pin_desc *desc;
416 if (pctlops->get_group_pins)
417 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
423 /* errors only affect debug data, so just warn */
424 gname = pctlops->get_group_name(pctldev,
425 setting->data.mux.group);
426 dev_warn(pctldev->dev,
427 "could not get pins for group %s\n",
432 /* Try to allocate all pins in this group, one by one */
433 for (i = 0; i < num_pins; i++) {
434 ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
439 desc = pin_desc_get(pctldev, pins[i]);
440 pname = desc ? desc->name : "non-existing";
441 gname = pctlops->get_group_name(pctldev,
442 setting->data.mux.group);
443 dev_err(pctldev->dev,
444 "could not request pin %d (%s) from group %s "
446 pins[i], pname, gname,
447 pinctrl_dev_get_name(pctldev));
448 goto err_pin_request;
452 /* Now that we have acquired the pins, encode the mux setting */
453 for (i = 0; i < num_pins; i++) {
454 desc = pin_desc_get(pctldev, pins[i]);
456 dev_warn(pctldev->dev,
457 "could not get pin desc for pin %d\n",
461 desc->mux_setting = &(setting->data.mux);
464 ret = ops->set_mux(pctldev, setting->data.mux.func,
465 setting->data.mux.group);
473 for (i = 0; i < num_pins; i++) {
474 desc = pin_desc_get(pctldev, pins[i]);
476 desc->mux_setting = NULL;
479 /* On error release all taken pins */
481 pin_free(pctldev, pins[i], NULL);
486 void pinmux_disable_setting(const struct pinctrl_setting *setting)
488 struct pinctrl_dev *pctldev = setting->pctldev;
489 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
491 const unsigned *pins = NULL;
492 unsigned num_pins = 0;
494 struct pin_desc *desc;
496 if (pctlops->get_group_pins)
497 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
502 /* errors only affect debug data, so just warn */
503 gname = pctlops->get_group_name(pctldev,
504 setting->data.mux.group);
505 dev_warn(pctldev->dev,
506 "could not get pins for group %s\n",
511 /* Flag the descs that no setting is active */
512 for (i = 0; i < num_pins; i++) {
513 desc = pin_desc_get(pctldev, pins[i]);
515 dev_warn(pctldev->dev,
516 "could not get pin desc for pin %d\n",
520 if (desc->mux_setting == &(setting->data.mux)) {
521 pin_free(pctldev, pins[i], NULL);
525 gname = pctlops->get_group_name(pctldev,
526 setting->data.mux.group);
527 dev_warn(pctldev->dev,
528 "not freeing pin %d (%s) as part of "
529 "deactivating group %s - it is already "
530 "used for some other setting",
531 pins[i], desc->name, gname);
536 #ifdef CONFIG_DEBUG_FS
538 /* Called from pincontrol core */
539 static int pinmux_functions_show(struct seq_file *s, void *what)
541 struct pinctrl_dev *pctldev = s->private;
542 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
544 unsigned func_selector = 0;
549 mutex_lock(&pctldev->mutex);
550 nfuncs = pmxops->get_functions_count(pctldev);
551 while (func_selector < nfuncs) {
552 const char *func = pmxops->get_function_name(pctldev,
554 const char * const *groups;
559 ret = pmxops->get_function_groups(pctldev, func_selector,
560 &groups, &num_groups);
562 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
568 seq_printf(s, "function %d: %s, groups = [ ", func_selector, func);
569 for (i = 0; i < num_groups; i++)
570 seq_printf(s, "%s ", groups[i]);
576 mutex_unlock(&pctldev->mutex);
581 static int pinmux_pins_show(struct seq_file *s, void *what)
583 struct pinctrl_dev *pctldev = s->private;
584 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
585 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
591 seq_puts(s, "Pinmux settings per pin\n");
594 "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
597 "Format: pin (name): mux_owner gpio_owner hog?\n");
599 mutex_lock(&pctldev->mutex);
601 /* The pin number can be retrived from the pin controller descriptor */
602 for (i = 0; i < pctldev->desc->npins; i++) {
603 struct pin_desc *desc;
606 pin = pctldev->desc->pins[i].number;
607 desc = pin_desc_get(pctldev, pin);
608 /* Skip if we cannot search the pin */
612 if (desc->mux_owner &&
613 !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
616 if (pmxops->strict) {
618 seq_printf(s, "pin %d (%s): device %s%s",
619 pin, desc->name, desc->mux_owner,
620 is_hog ? " (HOG)" : "");
621 else if (desc->gpio_owner)
622 seq_printf(s, "pin %d (%s): GPIO %s",
623 pin, desc->name, desc->gpio_owner);
625 seq_printf(s, "pin %d (%s): UNCLAIMED",
628 /* For non-strict controllers */
629 seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name,
630 desc->mux_owner ? desc->mux_owner
632 desc->gpio_owner ? desc->gpio_owner
633 : "(GPIO UNCLAIMED)",
634 is_hog ? " (HOG)" : "");
637 /* If mux: print function+group claiming the pin */
638 if (desc->mux_setting)
639 seq_printf(s, " function %s group %s\n",
640 pmxops->get_function_name(pctldev,
641 desc->mux_setting->func),
642 pctlops->get_group_name(pctldev,
643 desc->mux_setting->group));
648 mutex_unlock(&pctldev->mutex);
653 void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map)
655 seq_printf(s, "group %s\nfunction %s\n",
656 map->data.mux.group ? map->data.mux.group : "(default)",
657 map->data.mux.function);
660 void pinmux_show_setting(struct seq_file *s,
661 const struct pinctrl_setting *setting)
663 struct pinctrl_dev *pctldev = setting->pctldev;
664 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
665 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
667 seq_printf(s, "group: %s (%u) function: %s (%u)\n",
668 pctlops->get_group_name(pctldev, setting->data.mux.group),
669 setting->data.mux.group,
670 pmxops->get_function_name(pctldev, setting->data.mux.func),
671 setting->data.mux.func);
674 DEFINE_SHOW_ATTRIBUTE(pinmux_functions);
675 DEFINE_SHOW_ATTRIBUTE(pinmux_pins);
677 #define PINMUX_SELECT_MAX 128
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 if (len > PINMUX_SELECT_MAX)
692 buf = kzalloc(PINMUX_SELECT_MAX, GFP_KERNEL);
696 ret = strncpy_from_user(buf, user_buf, PINMUX_SELECT_MAX);
701 /* remove leading and trailing spaces of input buffer */
702 gname = strstrip(buf);
703 if (*gname == '\0') {
708 /* find a separator which is a spacelike character */
709 for (fname = gname; !isspace(*fname); fname++) {
710 if (*fname == '\0') {
717 /* drop extra spaces between function and group names */
718 fname = skip_spaces(fname + 1);
719 if (*fname == '\0') {
724 ret = pinmux_func_name_to_selector(pctldev, fname);
726 dev_err(pctldev->dev, "invalid function %s in map table\n", fname);
731 ret = pmxops->get_function_groups(pctldev, fsel, &groups, &num_groups);
733 dev_err(pctldev->dev, "no groups for function %d (%s)", fsel, fname);
737 ret = match_string(groups, num_groups, gname);
739 dev_err(pctldev->dev, "invalid group %s", gname);
743 ret = pinctrl_get_group_selector(pctldev, gname);
745 dev_err(pctldev->dev, "failed to get group selector for %s", gname);
750 ret = pmxops->set_mux(pctldev, fsel, gsel);
752 dev_err(pctldev->dev, "set_mux() failed: %d", ret);
763 static int pinmux_select_open(struct inode *inode, struct file *file)
765 return single_open(file, NULL, inode->i_private);
768 static const struct file_operations pinmux_select_ops = {
769 .owner = THIS_MODULE,
770 .open = pinmux_select_open,
771 .write = pinmux_select,
773 .release = single_release,
776 void pinmux_init_device_debugfs(struct dentry *devroot,
777 struct pinctrl_dev *pctldev)
779 debugfs_create_file("pinmux-functions", 0444,
780 devroot, pctldev, &pinmux_functions_fops);
781 debugfs_create_file("pinmux-pins", 0444,
782 devroot, pctldev, &pinmux_pins_fops);
783 debugfs_create_file("pinmux-select", 0200,
784 devroot, pctldev, &pinmux_select_ops);
787 #endif /* CONFIG_DEBUG_FS */
789 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
792 * pinmux_generic_get_function_count() - returns number of functions
793 * @pctldev: pin controller device
795 int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)
797 return pctldev->num_functions;
799 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);
802 * pinmux_generic_get_function_name() - returns the function name
803 * @pctldev: pin controller device
804 * @selector: function number
807 pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,
808 unsigned int selector)
810 struct function_desc *function;
812 function = radix_tree_lookup(&pctldev->pin_function_tree,
817 return function->name;
819 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);
822 * pinmux_generic_get_function_groups() - gets the function groups
823 * @pctldev: pin controller device
824 * @selector: function number
825 * @groups: array of pin groups
826 * @num_groups: number of pin groups
828 int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,
829 unsigned int selector,
830 const char * const **groups,
831 unsigned * const num_groups)
833 struct function_desc *function;
835 function = radix_tree_lookup(&pctldev->pin_function_tree,
838 dev_err(pctldev->dev, "%s could not find function%i\n",
842 *groups = function->group_names;
843 *num_groups = function->num_group_names;
847 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);
850 * pinmux_generic_get_function() - returns a function based on the number
851 * @pctldev: pin controller device
852 * @selector: function number
854 struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev,
855 unsigned int selector)
857 struct function_desc *function;
859 function = radix_tree_lookup(&pctldev->pin_function_tree,
866 EXPORT_SYMBOL_GPL(pinmux_generic_get_function);
869 * pinmux_generic_add_function() - adds a function group
870 * @pctldev: pin controller device
871 * @name: name of the function
872 * @groups: array of pin groups
873 * @num_groups: number of pin groups
874 * @data: pin controller driver specific data
876 int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
879 const unsigned int num_groups,
882 struct function_desc *function;
888 selector = pinmux_func_name_to_selector(pctldev, name);
892 selector = pctldev->num_functions;
894 function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
898 function->name = name;
899 function->group_names = groups;
900 function->num_group_names = num_groups;
901 function->data = data;
903 radix_tree_insert(&pctldev->pin_function_tree, selector, function);
905 pctldev->num_functions++;
909 EXPORT_SYMBOL_GPL(pinmux_generic_add_function);
912 * pinmux_generic_remove_function() - removes a numbered function
913 * @pctldev: pin controller device
914 * @selector: function number
916 * Note that the caller must take care of locking.
918 int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
919 unsigned int selector)
921 struct function_desc *function;
923 function = radix_tree_lookup(&pctldev->pin_function_tree,
928 radix_tree_delete(&pctldev->pin_function_tree, selector);
929 devm_kfree(pctldev->dev, function);
931 pctldev->num_functions--;
935 EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
938 * pinmux_generic_free_functions() - removes all functions
939 * @pctldev: pin controller device
941 * Note that the caller must take care of locking. The pinctrl
942 * functions are allocated with devm_kzalloc() so no need to free
945 void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
947 struct radix_tree_iter iter;
950 radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
951 radix_tree_delete(&pctldev->pin_function_tree, iter.index);
953 pctldev->num_functions = 0;
956 #endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */