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
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/cleanup.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/init.h>
22 #include <linux/list.h>
23 #include <linux/module.h>
24 #include <linux/radix-tree.h>
25 #include <linux/seq_file.h>
26 #include <linux/slab.h>
27 #include <linux/string.h>
29 #include <linux/pinctrl/machine.h>
30 #include <linux/pinctrl/pinctrl.h>
31 #include <linux/pinctrl/pinmux.h>
36 int pinmux_check_ops(struct pinctrl_dev *pctldev)
38 const struct pinmux_ops *ops = pctldev->desc->pmxops;
40 unsigned int selector = 0;
42 /* Check that we implement required operations */
44 !ops->get_functions_count ||
45 !ops->get_function_name ||
46 !ops->get_function_groups ||
48 dev_err(pctldev->dev, "pinmux ops lacks necessary functions\n");
51 /* Check that all functions registered have names */
52 nfuncs = ops->get_functions_count(pctldev);
53 while (selector < nfuncs) {
54 const char *fname = ops->get_function_name(pctldev,
57 dev_err(pctldev->dev, "pinmux ops has no name for function%u\n",
67 int pinmux_validate_map(const struct pinctrl_map *map, int i)
69 if (!map->data.mux.function) {
70 pr_err("failed to register map %s (%d): no function given\n",
79 * pinmux_can_be_used_for_gpio() - check if a specific pin
80 * is either muxed to a different function or used as gpio.
82 * @pctldev: the associated pin controller device
83 * @pin: the pin number in the global pin space
85 * Controllers not defined as strict will always return true,
86 * menaning that the gpio can be used.
88 bool pinmux_can_be_used_for_gpio(struct pinctrl_dev *pctldev, unsigned int pin)
90 struct pin_desc *desc = pin_desc_get(pctldev, pin);
91 const struct pinmux_ops *ops = pctldev->desc->pmxops;
93 /* Can't inspect pin, assume it can be used */
97 guard(mutex)(&desc->mux_lock);
98 if (ops->strict && desc->mux_usecount)
101 return !(ops->strict && !!desc->gpio_owner);
105 * pin_request() - request a single pin to be muxed in, typically for GPIO
106 * @pctldev: the associated pin controller device
107 * @pin: the pin number in the global pin space
108 * @owner: a representation of the owner of this pin; typically the device
109 * name that controls its mux function, or the requested GPIO name
110 * @gpio_range: the range matching the GPIO pin if this is a request for a
113 static int pin_request(struct pinctrl_dev *pctldev,
114 int pin, const char *owner,
115 struct pinctrl_gpio_range *gpio_range)
117 struct pin_desc *desc;
118 const struct pinmux_ops *ops = pctldev->desc->pmxops;
119 int status = -EINVAL;
121 desc = pin_desc_get(pctldev, pin);
123 dev_err(pctldev->dev,
124 "pin %d is not registered so it cannot be requested\n",
129 dev_dbg(pctldev->dev, "request pin %d (%s) for %s\n",
130 pin, desc->name, owner);
132 scoped_guard(mutex, &desc->mux_lock) {
133 if ((!gpio_range || ops->strict) &&
134 desc->mux_usecount && strcmp(desc->mux_owner, owner)) {
135 dev_err(pctldev->dev,
136 "pin %s already requested by %s; cannot claim for %s\n",
137 desc->name, desc->mux_owner, owner);
141 if ((gpio_range || ops->strict) && desc->gpio_owner) {
142 dev_err(pctldev->dev,
143 "pin %s already requested by %s; cannot claim for %s\n",
144 desc->name, desc->gpio_owner, owner);
149 desc->gpio_owner = owner;
151 desc->mux_usecount++;
152 if (desc->mux_usecount > 1)
155 desc->mux_owner = owner;
159 /* Let each pin increase references to this module */
160 if (!try_module_get(pctldev->owner)) {
161 dev_err(pctldev->dev,
162 "could not increase module refcount for pin %d\n",
169 * If there is no kind of request function for the pin we just assume
170 * we got it by default and proceed.
172 if (gpio_range && ops->gpio_request_enable)
173 /* This requests and enables a single GPIO pin */
174 status = ops->gpio_request_enable(pctldev, gpio_range, pin);
175 else if (ops->request)
176 status = ops->request(pctldev, pin);
181 module_put(pctldev->owner);
185 scoped_guard(mutex, &desc->mux_lock) {
187 desc->gpio_owner = NULL;
189 desc->mux_usecount--;
190 if (!desc->mux_usecount)
191 desc->mux_owner = NULL;
197 dev_err_probe(pctldev->dev, status, "pin-%d (%s)\n",
204 * pin_free() - release a single muxed in pin so something else can be muxed
205 * @pctldev: pin controller device handling this pin
206 * @pin: the pin to free
207 * @gpio_range: the range matching the GPIO pin if this is a request for a
210 * This function returns a pointer to the previous owner. This is used
211 * for callers that dynamically allocate an owner name so it can be freed
212 * once the pin is free. This is done for GPIO request functions.
214 static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
215 struct pinctrl_gpio_range *gpio_range)
217 const struct pinmux_ops *ops = pctldev->desc->pmxops;
218 struct pin_desc *desc;
221 desc = pin_desc_get(pctldev, pin);
223 dev_err(pctldev->dev,
224 "pin is not registered so it cannot be freed\n");
228 scoped_guard(mutex, &desc->mux_lock) {
231 * A pin should not be freed more times than allocated.
233 if (WARN_ON(!desc->mux_usecount))
235 desc->mux_usecount--;
236 if (desc->mux_usecount)
242 * If there is no kind of request function for the pin we just assume
243 * we got it by default and proceed.
245 if (gpio_range && ops->gpio_disable_free)
246 ops->gpio_disable_free(pctldev, gpio_range, pin);
248 ops->free(pctldev, pin);
250 scoped_guard(mutex, &desc->mux_lock) {
252 owner = desc->gpio_owner;
253 desc->gpio_owner = NULL;
255 owner = desc->mux_owner;
256 desc->mux_owner = NULL;
257 desc->mux_setting = NULL;
261 module_put(pctldev->owner);
267 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
268 * @pctldev: pin controller device affected
269 * @pin: the pin to mux in for GPIO
270 * @range: the applicable GPIO range
271 * @gpio: number of requested GPIO
273 int pinmux_request_gpio(struct pinctrl_dev *pctldev,
274 struct pinctrl_gpio_range *range,
275 unsigned int pin, unsigned int gpio)
280 /* Conjure some name stating what chip and pin this is taken by */
281 owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
285 ret = pin_request(pctldev, pin, owner, range);
293 * pinmux_free_gpio() - release a pin from GPIO muxing
294 * @pctldev: the pin controller device for the pin
295 * @pin: the affected currently GPIO-muxed in pin
296 * @range: applicable GPIO range
298 void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned int pin,
299 struct pinctrl_gpio_range *range)
303 owner = pin_free(pctldev, pin, range);
308 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
309 * @pctldev: the pin controller handling this pin
310 * @range: applicable GPIO range
311 * @pin: the affected GPIO pin in this controller
312 * @input: true if we set the pin as input, false for output
314 int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
315 struct pinctrl_gpio_range *range,
316 unsigned int pin, bool input)
318 const struct pinmux_ops *ops;
321 ops = pctldev->desc->pmxops;
323 if (ops->gpio_set_direction)
324 ret = ops->gpio_set_direction(pctldev, range, pin, input);
331 static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
332 const char *function)
334 const struct pinmux_ops *ops = pctldev->desc->pmxops;
335 unsigned int nfuncs = ops->get_functions_count(pctldev);
336 unsigned int selector = 0;
338 /* See if this pctldev has this function */
339 while (selector < nfuncs) {
340 const char *fname = ops->get_function_name(pctldev, selector);
342 if (!strcmp(function, fname))
351 int pinmux_map_to_setting(const struct pinctrl_map *map,
352 struct pinctrl_setting *setting)
354 struct pinctrl_dev *pctldev = setting->pctldev;
355 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
356 char const * const *groups;
357 unsigned int num_groups;
362 dev_err(pctldev->dev, "does not support mux function\n");
366 ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
368 dev_err(pctldev->dev, "invalid function %s in map table\n",
369 map->data.mux.function);
372 setting->data.mux.func = ret;
374 ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
375 &groups, &num_groups);
377 dev_err(pctldev->dev, "can't query groups for function %s\n",
378 map->data.mux.function);
382 dev_err(pctldev->dev,
383 "function %s can't be selected on any group\n",
384 map->data.mux.function);
387 if (map->data.mux.group) {
388 group = map->data.mux.group;
389 ret = match_string(groups, num_groups, group);
391 dev_err(pctldev->dev,
392 "invalid group \"%s\" for function \"%s\"\n",
393 group, map->data.mux.function);
400 ret = pinctrl_get_group_selector(pctldev, group);
402 dev_err(pctldev->dev, "invalid group %s in map table\n",
403 map->data.mux.group);
406 setting->data.mux.group = ret;
411 void pinmux_free_setting(const struct pinctrl_setting *setting)
413 /* This function is currently unused */
416 int pinmux_enable_setting(const struct pinctrl_setting *setting)
418 struct pinctrl_dev *pctldev = setting->pctldev;
419 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
420 const struct pinmux_ops *ops = pctldev->desc->pmxops;
422 const unsigned int *pins = NULL;
423 unsigned int num_pins = 0;
425 struct pin_desc *desc;
427 if (pctlops->get_group_pins)
428 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
434 /* errors only affect debug data, so just warn */
435 gname = pctlops->get_group_name(pctldev,
436 setting->data.mux.group);
437 dev_warn(pctldev->dev,
438 "could not get pins for group %s\n",
443 /* Try to allocate all pins in this group, one by one */
444 for (i = 0; i < num_pins; i++) {
445 ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
450 desc = pin_desc_get(pctldev, pins[i]);
451 pname = desc ? desc->name : "non-existing";
452 gname = pctlops->get_group_name(pctldev,
453 setting->data.mux.group);
454 dev_err_probe(pctldev->dev, ret,
455 "could not request pin %d (%s) from group %s on device %s\n",
456 pins[i], pname, gname,
457 pinctrl_dev_get_name(pctldev));
458 goto err_pin_request;
462 /* Now that we have acquired the pins, encode the mux setting */
463 for (i = 0; i < num_pins; i++) {
464 desc = pin_desc_get(pctldev, pins[i]);
466 dev_warn(pctldev->dev,
467 "could not get pin desc for pin %d\n",
471 scoped_guard(mutex, &desc->mux_lock)
472 desc->mux_setting = &(setting->data.mux);
475 ret = ops->set_mux(pctldev, setting->data.mux.func,
476 setting->data.mux.group);
484 for (i = 0; i < num_pins; i++) {
485 desc = pin_desc_get(pctldev, pins[i]);
487 scoped_guard(mutex, &desc->mux_lock)
488 desc->mux_setting = NULL;
492 /* On error release all taken pins */
494 pin_free(pctldev, pins[i], NULL);
499 void pinmux_disable_setting(const struct pinctrl_setting *setting)
501 struct pinctrl_dev *pctldev = setting->pctldev;
502 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
504 const unsigned int *pins = NULL;
505 unsigned int num_pins = 0;
507 struct pin_desc *desc;
510 if (pctlops->get_group_pins)
511 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
516 /* errors only affect debug data, so just warn */
517 gname = pctlops->get_group_name(pctldev,
518 setting->data.mux.group);
519 dev_warn(pctldev->dev,
520 "could not get pins for group %s\n",
525 /* Flag the descs that no setting is active */
526 for (i = 0; i < num_pins; i++) {
527 desc = pin_desc_get(pctldev, pins[i]);
529 dev_warn(pctldev->dev,
530 "could not get pin desc for pin %d\n",
534 scoped_guard(mutex, &desc->mux_lock)
535 is_equal = (desc->mux_setting == &(setting->data.mux));
538 pin_free(pctldev, pins[i], NULL);
542 gname = pctlops->get_group_name(pctldev,
543 setting->data.mux.group);
544 dev_warn(pctldev->dev,
545 "not freeing pin %d (%s) as part of deactivating group %s - it is already used for some other setting",
546 pins[i], desc->name, gname);
551 #ifdef CONFIG_DEBUG_FS
553 /* Called from pincontrol core */
554 static int pinmux_functions_show(struct seq_file *s, void *what)
556 struct pinctrl_dev *pctldev = s->private;
557 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
559 unsigned int func_selector = 0;
564 mutex_lock(&pctldev->mutex);
565 nfuncs = pmxops->get_functions_count(pctldev);
566 while (func_selector < nfuncs) {
567 const char *func = pmxops->get_function_name(pctldev,
569 const char * const *groups;
570 unsigned int num_groups;
574 ret = pmxops->get_function_groups(pctldev, func_selector,
575 &groups, &num_groups);
577 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
583 seq_printf(s, "function %d: %s, groups = [ ", func_selector, func);
584 for (i = 0; i < num_groups; i++)
585 seq_printf(s, "%s ", groups[i]);
591 mutex_unlock(&pctldev->mutex);
595 DEFINE_SHOW_ATTRIBUTE(pinmux_functions);
597 static int pinmux_pins_show(struct seq_file *s, void *what)
599 struct pinctrl_dev *pctldev = s->private;
600 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
601 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
607 seq_puts(s, "Pinmux settings per pin\n");
610 "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
613 "Format: pin (name): mux_owner gpio_owner hog?\n");
615 mutex_lock(&pctldev->mutex);
617 /* The pin number can be retrived from the pin controller descriptor */
618 for (i = 0; i < pctldev->desc->npins; i++) {
619 struct pin_desc *desc;
622 pin = pctldev->desc->pins[i].number;
623 desc = pin_desc_get(pctldev, pin);
624 /* Skip if we cannot search the pin */
628 scoped_guard(mutex, &desc->mux_lock) {
629 if (desc->mux_owner &&
630 !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
633 if (pmxops->strict) {
635 seq_printf(s, "pin %d (%s): device %s%s",
636 pin, desc->name, desc->mux_owner,
637 is_hog ? " (HOG)" : "");
638 else if (desc->gpio_owner)
639 seq_printf(s, "pin %d (%s): GPIO %s",
640 pin, desc->name, desc->gpio_owner);
642 seq_printf(s, "pin %d (%s): UNCLAIMED",
645 /* For non-strict controllers */
646 seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name,
647 desc->mux_owner ? desc->mux_owner
649 desc->gpio_owner ? desc->gpio_owner
650 : "(GPIO UNCLAIMED)",
651 is_hog ? " (HOG)" : "");
654 /* If mux: print function+group claiming the pin */
655 if (desc->mux_setting)
656 seq_printf(s, " function %s group %s\n",
657 pmxops->get_function_name(pctldev,
658 desc->mux_setting->func),
659 pctlops->get_group_name(pctldev,
660 desc->mux_setting->group));
666 mutex_unlock(&pctldev->mutex);
670 DEFINE_SHOW_ATTRIBUTE(pinmux_pins);
672 void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map)
674 seq_printf(s, "group %s\nfunction %s\n",
675 map->data.mux.group ? map->data.mux.group : "(default)",
676 map->data.mux.function);
679 void pinmux_show_setting(struct seq_file *s,
680 const struct pinctrl_setting *setting)
682 struct pinctrl_dev *pctldev = setting->pctldev;
683 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
684 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
686 seq_printf(s, "group: %s (%u) function: %s (%u)\n",
687 pctlops->get_group_name(pctldev, setting->data.mux.group),
688 setting->data.mux.group,
689 pmxops->get_function_name(pctldev, setting->data.mux.func),
690 setting->data.mux.func);
693 static int pinmux_select_show(struct seq_file *s, void *unused)
698 static ssize_t pinmux_select_write(struct file *file, const char __user *user_buf,
699 size_t len, loff_t *ppos)
701 struct seq_file *sfile = file->private_data;
702 struct pinctrl_dev *pctldev = sfile->private;
703 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
704 const char *const *groups;
705 char *buf, *gname, *fname;
706 unsigned int num_groups;
709 buf = memdup_user_nul(user_buf, len);
713 /* remove leading and trailing spaces of input buffer */
714 gname = strstrip(buf);
715 if (*gname == '\0') {
720 /* find a separator which is a spacelike character */
721 for (fname = gname; !isspace(*fname); fname++) {
722 if (*fname == '\0') {
729 /* drop extra spaces between function and group names */
730 fname = skip_spaces(fname + 1);
731 if (*fname == '\0') {
736 ret = pinmux_func_name_to_selector(pctldev, fname);
738 dev_err(pctldev->dev, "invalid function %s in map table\n", fname);
743 ret = pmxops->get_function_groups(pctldev, fsel, &groups, &num_groups);
745 dev_err(pctldev->dev, "no groups for function %d (%s)", fsel, fname);
749 ret = match_string(groups, num_groups, gname);
751 dev_err(pctldev->dev, "invalid group %s", gname);
755 ret = pinctrl_get_group_selector(pctldev, gname);
760 ret = pmxops->set_mux(pctldev, fsel, gsel);
762 dev_err(pctldev->dev, "set_mux() failed: %d", ret);
772 DEFINE_SHOW_STORE_ATTRIBUTE(pinmux_select);
774 void pinmux_init_device_debugfs(struct dentry *devroot,
775 struct pinctrl_dev *pctldev)
777 debugfs_create_file("pinmux-functions", 0444,
778 devroot, pctldev, &pinmux_functions_fops);
779 debugfs_create_file("pinmux-pins", 0444,
780 devroot, pctldev, &pinmux_pins_fops);
781 debugfs_create_file("pinmux-select", 0200,
782 devroot, pctldev, &pinmux_select_fops);
785 #endif /* CONFIG_DEBUG_FS */
787 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
790 * pinmux_generic_get_function_count() - returns number of functions
791 * @pctldev: pin controller device
793 int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)
795 return pctldev->num_functions;
797 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);
800 * pinmux_generic_get_function_name() - returns the function name
801 * @pctldev: pin controller device
802 * @selector: function number
805 pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,
806 unsigned int selector)
808 struct function_desc *function;
810 function = radix_tree_lookup(&pctldev->pin_function_tree,
815 return function->func.name;
817 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);
820 * pinmux_generic_get_function_groups() - gets the function groups
821 * @pctldev: pin controller device
822 * @selector: function number
823 * @groups: array of pin groups
824 * @ngroups: number of pin groups
826 int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,
827 unsigned int selector,
828 const char * const **groups,
829 unsigned int * const ngroups)
831 struct function_desc *function;
833 function = radix_tree_lookup(&pctldev->pin_function_tree,
836 dev_err(pctldev->dev, "%s could not find function%i\n",
840 *groups = function->func.groups;
841 *ngroups = function->func.ngroups;
845 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);
848 * pinmux_generic_get_function() - returns a function based on the number
849 * @pctldev: pin controller device
850 * @selector: function number
852 struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev,
853 unsigned int selector)
855 struct function_desc *function;
857 function = radix_tree_lookup(&pctldev->pin_function_tree,
864 EXPORT_SYMBOL_GPL(pinmux_generic_get_function);
867 * pinmux_generic_add_function() - adds a function group
868 * @pctldev: pin controller device
869 * @name: name of the function
870 * @groups: array of pin groups
871 * @ngroups: number of pin groups
872 * @data: pin controller driver specific data
874 int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
876 const char * const *groups,
877 const unsigned int ngroups,
880 struct function_desc *function;
886 selector = pinmux_func_name_to_selector(pctldev, name);
890 selector = pctldev->num_functions;
892 function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
896 *function = PINCTRL_FUNCTION_DESC(name, groups, ngroups, data);
898 error = radix_tree_insert(&pctldev->pin_function_tree, selector, function);
902 pctldev->num_functions++;
906 EXPORT_SYMBOL_GPL(pinmux_generic_add_function);
909 * pinmux_generic_remove_function() - removes a numbered function
910 * @pctldev: pin controller device
911 * @selector: function number
913 * Note that the caller must take care of locking.
915 int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
916 unsigned int selector)
918 struct function_desc *function;
920 function = radix_tree_lookup(&pctldev->pin_function_tree,
925 radix_tree_delete(&pctldev->pin_function_tree, selector);
926 devm_kfree(pctldev->dev, function);
928 pctldev->num_functions--;
932 EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
935 * pinmux_generic_free_functions() - removes all functions
936 * @pctldev: pin controller device
938 * Note that the caller must take care of locking. The pinctrl
939 * functions are allocated with devm_kzalloc() so no need to free
942 void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
944 struct radix_tree_iter iter;
947 radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
948 radix_tree_delete(&pctldev->pin_function_tree, iter.index);
950 pctldev->num_functions = 0;
953 #endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */