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/ctype.h>
16 #include <linux/debugfs.h>
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/kernel.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 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 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 dev_err(pctldev->dev, "request() failed for pin %d\n", pin);
178 module_put(pctldev->owner);
184 desc->gpio_owner = NULL;
186 desc->mux_usecount--;
187 if (!desc->mux_usecount)
188 desc->mux_owner = NULL;
193 dev_err(pctldev->dev, "pin-%d (%s) status %d\n",
200 * pin_free() - release a single muxed in pin so something else can be muxed
201 * @pctldev: pin controller device handling this pin
202 * @pin: the pin to free
203 * @gpio_range: the range matching the GPIO pin if this is a request for a
206 * This function returns a pointer to the previous owner. This is used
207 * for callers that dynamically allocate an owner name so it can be freed
208 * once the pin is free. This is done for GPIO request functions.
210 static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
211 struct pinctrl_gpio_range *gpio_range)
213 const struct pinmux_ops *ops = pctldev->desc->pmxops;
214 struct pin_desc *desc;
217 desc = pin_desc_get(pctldev, pin);
219 dev_err(pctldev->dev,
220 "pin is not registered so it cannot be freed\n");
226 * A pin should not be freed more times than allocated.
228 if (WARN_ON(!desc->mux_usecount))
230 desc->mux_usecount--;
231 if (desc->mux_usecount)
236 * If there is no kind of request function for the pin we just assume
237 * we got it by default and proceed.
239 if (gpio_range && ops->gpio_disable_free)
240 ops->gpio_disable_free(pctldev, gpio_range, pin);
242 ops->free(pctldev, pin);
245 owner = desc->gpio_owner;
246 desc->gpio_owner = NULL;
248 owner = desc->mux_owner;
249 desc->mux_owner = NULL;
250 desc->mux_setting = NULL;
253 module_put(pctldev->owner);
259 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
260 * @pctldev: pin controller device affected
261 * @pin: the pin to mux in for GPIO
262 * @range: the applicable GPIO range
263 * @gpio: number of requested GPIO
265 int pinmux_request_gpio(struct pinctrl_dev *pctldev,
266 struct pinctrl_gpio_range *range,
267 unsigned pin, unsigned gpio)
272 /* Conjure some name stating what chip and pin this is taken by */
273 owner = kasprintf(GFP_KERNEL, "%s:%d", range->name, gpio);
277 ret = pin_request(pctldev, pin, owner, range);
285 * pinmux_free_gpio() - release a pin from GPIO muxing
286 * @pctldev: the pin controller device for the pin
287 * @pin: the affected currently GPIO-muxed in pin
288 * @range: applicable GPIO range
290 void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
291 struct pinctrl_gpio_range *range)
295 owner = pin_free(pctldev, pin, range);
300 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
301 * @pctldev: the pin controller handling this pin
302 * @range: applicable GPIO range
303 * @pin: the affected GPIO pin in this controller
304 * @input: true if we set the pin as input, false for output
306 int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
307 struct pinctrl_gpio_range *range,
308 unsigned pin, bool input)
310 const struct pinmux_ops *ops;
313 ops = pctldev->desc->pmxops;
315 if (ops->gpio_set_direction)
316 ret = ops->gpio_set_direction(pctldev, range, pin, input);
323 static int pinmux_func_name_to_selector(struct pinctrl_dev *pctldev,
324 const char *function)
326 const struct pinmux_ops *ops = pctldev->desc->pmxops;
327 unsigned nfuncs = ops->get_functions_count(pctldev);
328 unsigned selector = 0;
330 /* See if this pctldev has this function */
331 while (selector < nfuncs) {
332 const char *fname = ops->get_function_name(pctldev, selector);
334 if (!strcmp(function, fname))
343 int pinmux_map_to_setting(const struct pinctrl_map *map,
344 struct pinctrl_setting *setting)
346 struct pinctrl_dev *pctldev = setting->pctldev;
347 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
348 char const * const *groups;
354 dev_err(pctldev->dev, "does not support mux function\n");
358 ret = pinmux_func_name_to_selector(pctldev, map->data.mux.function);
360 dev_err(pctldev->dev, "invalid function %s in map table\n",
361 map->data.mux.function);
364 setting->data.mux.func = ret;
366 ret = pmxops->get_function_groups(pctldev, setting->data.mux.func,
367 &groups, &num_groups);
369 dev_err(pctldev->dev, "can't query groups for function %s\n",
370 map->data.mux.function);
374 dev_err(pctldev->dev,
375 "function %s can't be selected on any group\n",
376 map->data.mux.function);
379 if (map->data.mux.group) {
380 group = map->data.mux.group;
381 ret = match_string(groups, num_groups, group);
383 dev_err(pctldev->dev,
384 "invalid group \"%s\" for function \"%s\"\n",
385 group, map->data.mux.function);
392 ret = pinctrl_get_group_selector(pctldev, group);
394 dev_err(pctldev->dev, "invalid group %s in map table\n",
395 map->data.mux.group);
398 setting->data.mux.group = ret;
403 void pinmux_free_setting(const struct pinctrl_setting *setting)
405 /* This function is currently unused */
408 int pinmux_enable_setting(const struct pinctrl_setting *setting)
410 struct pinctrl_dev *pctldev = setting->pctldev;
411 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
412 const struct pinmux_ops *ops = pctldev->desc->pmxops;
414 const unsigned *pins = NULL;
415 unsigned num_pins = 0;
417 struct pin_desc *desc;
419 if (pctlops->get_group_pins)
420 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
426 /* errors only affect debug data, so just warn */
427 gname = pctlops->get_group_name(pctldev,
428 setting->data.mux.group);
429 dev_warn(pctldev->dev,
430 "could not get pins for group %s\n",
435 /* Try to allocate all pins in this group, one by one */
436 for (i = 0; i < num_pins; i++) {
437 ret = pin_request(pctldev, pins[i], setting->dev_name, NULL);
442 desc = pin_desc_get(pctldev, pins[i]);
443 pname = desc ? desc->name : "non-existing";
444 gname = pctlops->get_group_name(pctldev,
445 setting->data.mux.group);
446 dev_err(pctldev->dev,
447 "could not request pin %d (%s) from group %s "
449 pins[i], pname, gname,
450 pinctrl_dev_get_name(pctldev));
451 goto err_pin_request;
455 /* Now that we have acquired the pins, encode the mux setting */
456 for (i = 0; i < num_pins; i++) {
457 desc = pin_desc_get(pctldev, pins[i]);
459 dev_warn(pctldev->dev,
460 "could not get pin desc for pin %d\n",
464 desc->mux_setting = &(setting->data.mux);
467 ret = ops->set_mux(pctldev, setting->data.mux.func,
468 setting->data.mux.group);
476 for (i = 0; i < num_pins; i++) {
477 desc = pin_desc_get(pctldev, pins[i]);
479 desc->mux_setting = NULL;
482 /* On error release all taken pins */
484 pin_free(pctldev, pins[i], NULL);
489 void pinmux_disable_setting(const struct pinctrl_setting *setting)
491 struct pinctrl_dev *pctldev = setting->pctldev;
492 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
494 const unsigned *pins = NULL;
495 unsigned num_pins = 0;
497 struct pin_desc *desc;
499 if (pctlops->get_group_pins)
500 ret = pctlops->get_group_pins(pctldev, setting->data.mux.group,
505 /* errors only affect debug data, so just warn */
506 gname = pctlops->get_group_name(pctldev,
507 setting->data.mux.group);
508 dev_warn(pctldev->dev,
509 "could not get pins for group %s\n",
514 /* Flag the descs that no setting is active */
515 for (i = 0; i < num_pins; i++) {
516 desc = pin_desc_get(pctldev, pins[i]);
518 dev_warn(pctldev->dev,
519 "could not get pin desc for pin %d\n",
523 if (desc->mux_setting == &(setting->data.mux)) {
524 pin_free(pctldev, pins[i], NULL);
528 gname = pctlops->get_group_name(pctldev,
529 setting->data.mux.group);
530 dev_warn(pctldev->dev,
531 "not freeing pin %d (%s) as part of "
532 "deactivating group %s - it is already "
533 "used for some other setting",
534 pins[i], desc->name, gname);
539 #ifdef CONFIG_DEBUG_FS
541 /* Called from pincontrol core */
542 static int pinmux_functions_show(struct seq_file *s, void *what)
544 struct pinctrl_dev *pctldev = s->private;
545 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
547 unsigned func_selector = 0;
552 mutex_lock(&pctldev->mutex);
553 nfuncs = pmxops->get_functions_count(pctldev);
554 while (func_selector < nfuncs) {
555 const char *func = pmxops->get_function_name(pctldev,
557 const char * const *groups;
562 ret = pmxops->get_function_groups(pctldev, func_selector,
563 &groups, &num_groups);
565 seq_printf(s, "function %s: COULD NOT GET GROUPS\n",
571 seq_printf(s, "function %d: %s, groups = [ ", func_selector, func);
572 for (i = 0; i < num_groups; i++)
573 seq_printf(s, "%s ", groups[i]);
579 mutex_unlock(&pctldev->mutex);
584 static int pinmux_pins_show(struct seq_file *s, void *what)
586 struct pinctrl_dev *pctldev = s->private;
587 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
588 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
594 seq_puts(s, "Pinmux settings per pin\n");
597 "Format: pin (name): mux_owner|gpio_owner (strict) hog?\n");
600 "Format: pin (name): mux_owner gpio_owner hog?\n");
602 mutex_lock(&pctldev->mutex);
604 /* The pin number can be retrived from the pin controller descriptor */
605 for (i = 0; i < pctldev->desc->npins; i++) {
606 struct pin_desc *desc;
609 pin = pctldev->desc->pins[i].number;
610 desc = pin_desc_get(pctldev, pin);
611 /* Skip if we cannot search the pin */
615 if (desc->mux_owner &&
616 !strcmp(desc->mux_owner, pinctrl_dev_get_name(pctldev)))
619 if (pmxops->strict) {
621 seq_printf(s, "pin %d (%s): device %s%s",
622 pin, desc->name, desc->mux_owner,
623 is_hog ? " (HOG)" : "");
624 else if (desc->gpio_owner)
625 seq_printf(s, "pin %d (%s): GPIO %s",
626 pin, desc->name, desc->gpio_owner);
628 seq_printf(s, "pin %d (%s): UNCLAIMED",
631 /* For non-strict controllers */
632 seq_printf(s, "pin %d (%s): %s %s%s", pin, desc->name,
633 desc->mux_owner ? desc->mux_owner
635 desc->gpio_owner ? desc->gpio_owner
636 : "(GPIO UNCLAIMED)",
637 is_hog ? " (HOG)" : "");
640 /* If mux: print function+group claiming the pin */
641 if (desc->mux_setting)
642 seq_printf(s, " function %s group %s\n",
643 pmxops->get_function_name(pctldev,
644 desc->mux_setting->func),
645 pctlops->get_group_name(pctldev,
646 desc->mux_setting->group));
651 mutex_unlock(&pctldev->mutex);
656 void pinmux_show_map(struct seq_file *s, const struct pinctrl_map *map)
658 seq_printf(s, "group %s\nfunction %s\n",
659 map->data.mux.group ? map->data.mux.group : "(default)",
660 map->data.mux.function);
663 void pinmux_show_setting(struct seq_file *s,
664 const struct pinctrl_setting *setting)
666 struct pinctrl_dev *pctldev = setting->pctldev;
667 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
668 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
670 seq_printf(s, "group: %s (%u) function: %s (%u)\n",
671 pctlops->get_group_name(pctldev, setting->data.mux.group),
672 setting->data.mux.group,
673 pmxops->get_function_name(pctldev, setting->data.mux.func),
674 setting->data.mux.func);
677 DEFINE_SHOW_ATTRIBUTE(pinmux_functions);
678 DEFINE_SHOW_ATTRIBUTE(pinmux_pins);
680 #define PINMUX_SELECT_MAX 128
681 static ssize_t pinmux_select(struct file *file, const char __user *user_buf,
682 size_t len, loff_t *ppos)
684 struct seq_file *sfile = file->private_data;
685 struct pinctrl_dev *pctldev = sfile->private;
686 const struct pinmux_ops *pmxops = pctldev->desc->pmxops;
687 const char *const *groups;
688 char *buf, *gname, *fname;
689 unsigned int num_groups;
692 if (len > PINMUX_SELECT_MAX)
695 buf = kzalloc(PINMUX_SELECT_MAX, GFP_KERNEL);
699 ret = strncpy_from_user(buf, user_buf, PINMUX_SELECT_MAX);
704 /* remove leading and trailing spaces of input buffer */
705 gname = strstrip(buf);
706 if (*gname == '\0') {
711 /* find a separator which is a spacelike character */
712 for (fname = gname; !isspace(*fname); fname++) {
713 if (*fname == '\0') {
720 /* drop extra spaces between function and group names */
721 fname = skip_spaces(fname + 1);
722 if (*fname == '\0') {
727 ret = pinmux_func_name_to_selector(pctldev, fname);
729 dev_err(pctldev->dev, "invalid function %s in map table\n", fname);
734 ret = pmxops->get_function_groups(pctldev, fsel, &groups, &num_groups);
736 dev_err(pctldev->dev, "no groups for function %d (%s)", fsel, fname);
740 ret = match_string(groups, num_groups, gname);
742 dev_err(pctldev->dev, "invalid group %s", gname);
746 ret = pinctrl_get_group_selector(pctldev, gname);
748 dev_err(pctldev->dev, "failed to get group selector for %s", gname);
753 ret = pmxops->set_mux(pctldev, fsel, gsel);
755 dev_err(pctldev->dev, "set_mux() failed: %d", ret);
766 static int pinmux_select_open(struct inode *inode, struct file *file)
768 return single_open(file, NULL, inode->i_private);
771 static const struct file_operations pinmux_select_ops = {
772 .owner = THIS_MODULE,
773 .open = pinmux_select_open,
774 .write = pinmux_select,
776 .release = single_release,
779 void pinmux_init_device_debugfs(struct dentry *devroot,
780 struct pinctrl_dev *pctldev)
782 debugfs_create_file("pinmux-functions", 0444,
783 devroot, pctldev, &pinmux_functions_fops);
784 debugfs_create_file("pinmux-pins", 0444,
785 devroot, pctldev, &pinmux_pins_fops);
786 debugfs_create_file("pinmux-select", 0200,
787 devroot, pctldev, &pinmux_select_ops);
790 #endif /* CONFIG_DEBUG_FS */
792 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
795 * pinmux_generic_get_function_count() - returns number of functions
796 * @pctldev: pin controller device
798 int pinmux_generic_get_function_count(struct pinctrl_dev *pctldev)
800 return pctldev->num_functions;
802 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_count);
805 * pinmux_generic_get_function_name() - returns the function name
806 * @pctldev: pin controller device
807 * @selector: function number
810 pinmux_generic_get_function_name(struct pinctrl_dev *pctldev,
811 unsigned int selector)
813 struct function_desc *function;
815 function = radix_tree_lookup(&pctldev->pin_function_tree,
820 return function->name;
822 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_name);
825 * pinmux_generic_get_function_groups() - gets the function groups
826 * @pctldev: pin controller device
827 * @selector: function number
828 * @groups: array of pin groups
829 * @num_groups: number of pin groups
831 int pinmux_generic_get_function_groups(struct pinctrl_dev *pctldev,
832 unsigned int selector,
833 const char * const **groups,
834 unsigned * const num_groups)
836 struct function_desc *function;
838 function = radix_tree_lookup(&pctldev->pin_function_tree,
841 dev_err(pctldev->dev, "%s could not find function%i\n",
845 *groups = function->group_names;
846 *num_groups = function->num_group_names;
850 EXPORT_SYMBOL_GPL(pinmux_generic_get_function_groups);
853 * pinmux_generic_get_function() - returns a function based on the number
854 * @pctldev: pin controller device
855 * @selector: function number
857 struct function_desc *pinmux_generic_get_function(struct pinctrl_dev *pctldev,
858 unsigned int selector)
860 struct function_desc *function;
862 function = radix_tree_lookup(&pctldev->pin_function_tree,
869 EXPORT_SYMBOL_GPL(pinmux_generic_get_function);
872 * pinmux_generic_add_function() - adds a function group
873 * @pctldev: pin controller device
874 * @name: name of the function
875 * @groups: array of pin groups
876 * @num_groups: number of pin groups
877 * @data: pin controller driver specific data
879 int pinmux_generic_add_function(struct pinctrl_dev *pctldev,
881 const char * const *groups,
882 const unsigned int num_groups,
885 struct function_desc *function;
891 selector = pinmux_func_name_to_selector(pctldev, name);
895 selector = pctldev->num_functions;
897 function = devm_kzalloc(pctldev->dev, sizeof(*function), GFP_KERNEL);
901 function->name = name;
902 function->group_names = groups;
903 function->num_group_names = num_groups;
904 function->data = data;
906 radix_tree_insert(&pctldev->pin_function_tree, selector, function);
908 pctldev->num_functions++;
912 EXPORT_SYMBOL_GPL(pinmux_generic_add_function);
915 * pinmux_generic_remove_function() - removes a numbered function
916 * @pctldev: pin controller device
917 * @selector: function number
919 * Note that the caller must take care of locking.
921 int pinmux_generic_remove_function(struct pinctrl_dev *pctldev,
922 unsigned int selector)
924 struct function_desc *function;
926 function = radix_tree_lookup(&pctldev->pin_function_tree,
931 radix_tree_delete(&pctldev->pin_function_tree, selector);
932 devm_kfree(pctldev->dev, function);
934 pctldev->num_functions--;
938 EXPORT_SYMBOL_GPL(pinmux_generic_remove_function);
941 * pinmux_generic_free_functions() - removes all functions
942 * @pctldev: pin controller device
944 * Note that the caller must take care of locking. The pinctrl
945 * functions are allocated with devm_kzalloc() so no need to free
948 void pinmux_generic_free_functions(struct pinctrl_dev *pctldev)
950 struct radix_tree_iter iter;
953 radix_tree_for_each_slot(slot, &pctldev->pin_function_tree, &iter, 0)
954 radix_tree_delete(&pctldev->pin_function_tree, iter.index);
956 pctldev->num_functions = 0;
959 #endif /* CONFIG_GENERIC_PINMUX_FUNCTIONS */