Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

pinctrl: pinmux: Add pinmux-select debugfs file

Add "pinmux-select" to debugfs which will activate a pin function for a
given pin group:

echo "<group-name function-name>" > pinmux-select

The write operation pinmux_select() handles this by checking that the
names map to valid selectors and then calling ops->set_mux().

The existing "pinmux-functions" debugfs file lists the pin functions
registered for the pin controller. For example:

function: pinmux-uart0, groups = [ pinmux-uart0-pins ]
function: pinmux-mmc0, groups = [ pinmux-mmc0-pins ]
function: pinmux-mmc1, groups = [ pinmux-mmc1-pins ]
function: pinmux-i2c0, groups = [ pinmux-i2c0-pins ]
function: pinmux-i2c1, groups = [ pinmux-i2c1-pins ]
function: pinmux-spi1, groups = [ pinmux-spi1-pins ]

To activate function pinmux-i2c1 on group pinmux-i2c1-pins:

echo "pinmux-i2c1-pins pinmux-i2c1" > pinmux-select

Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Reviewed-by: Tony Lindgren <tony@atomide.com>
Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
Tested-by: Geert Uytterhoeven <geert+renesas@glider.be>
Signed-off-by: Drew Fustini <drew@beagleboard.org>
Link: https://lore.kernel.org/r/20210302053059.1049035-3-drew@beagleboard.org
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

authored by

Drew Fustini and committed by
Linus Walleij
6199f6be 47473813

+102
+102
drivers/pinctrl/pinmux.c
··· 12 12 */ 13 13 #define pr_fmt(fmt) "pinmux core: " fmt 14 14 15 + #include <linux/ctype.h> 15 16 #include <linux/kernel.h> 16 17 #include <linux/module.h> 17 18 #include <linux/init.h> ··· 674 673 DEFINE_SHOW_ATTRIBUTE(pinmux_functions); 675 674 DEFINE_SHOW_ATTRIBUTE(pinmux_pins); 676 675 676 + #define PINMUX_SELECT_MAX 128 677 + static ssize_t pinmux_select(struct file *file, const char __user *user_buf, 678 + size_t len, loff_t *ppos) 679 + { 680 + struct seq_file *sfile = file->private_data; 681 + struct pinctrl_dev *pctldev = sfile->private; 682 + const struct pinmux_ops *pmxops = pctldev->desc->pmxops; 683 + const char *const *groups; 684 + char *buf, *gname, *fname; 685 + unsigned int num_groups; 686 + int fsel, gsel, ret; 687 + 688 + if (len > PINMUX_SELECT_MAX) 689 + return -ENOMEM; 690 + 691 + buf = kzalloc(PINMUX_SELECT_MAX, GFP_KERNEL); 692 + if (!buf) 693 + return -ENOMEM; 694 + 695 + ret = strncpy_from_user(buf, user_buf, PINMUX_SELECT_MAX); 696 + if (ret < 0) 697 + goto exit_free_buf; 698 + buf[len-1] = '\0'; 699 + 700 + /* remove leading and trailing spaces of input buffer */ 701 + gname = strstrip(buf); 702 + if (*gname == '\0') { 703 + ret = -EINVAL; 704 + goto exit_free_buf; 705 + } 706 + 707 + /* find a separator which is a spacelike character */ 708 + for (fname = gname; !isspace(*fname); fname++) { 709 + if (*fname == '\0') { 710 + ret = -EINVAL; 711 + goto exit_free_buf; 712 + } 713 + } 714 + *fname = '\0'; 715 + 716 + /* drop extra spaces between function and group names */ 717 + fname = skip_spaces(fname + 1); 718 + if (*fname == '\0') { 719 + ret = -EINVAL; 720 + goto exit_free_buf; 721 + } 722 + 723 + ret = pinmux_func_name_to_selector(pctldev, fname); 724 + if (ret < 0) { 725 + dev_err(pctldev->dev, "invalid function %s in map table\n", fname); 726 + goto exit_free_buf; 727 + } 728 + fsel = ret; 729 + 730 + ret = pmxops->get_function_groups(pctldev, fsel, &groups, &num_groups); 731 + if (ret) { 732 + dev_err(pctldev->dev, "no groups for function %d (%s)", fsel, fname); 733 + goto exit_free_buf; 734 + } 735 + 736 + ret = match_string(groups, num_groups, gname); 737 + if (ret < 0) { 738 + dev_err(pctldev->dev, "invalid group %s", gname); 739 + goto exit_free_buf; 740 + } 741 + 742 + ret = pinctrl_get_group_selector(pctldev, gname); 743 + if (ret < 0) { 744 + dev_err(pctldev->dev, "failed to get group selector for %s", gname); 745 + goto exit_free_buf; 746 + } 747 + gsel = ret; 748 + 749 + ret = pmxops->set_mux(pctldev, fsel, gsel); 750 + if (ret) { 751 + dev_err(pctldev->dev, "set_mux() failed: %d", ret); 752 + goto exit_free_buf; 753 + } 754 + ret = len; 755 + 756 + exit_free_buf: 757 + kfree(buf); 758 + 759 + return ret; 760 + } 761 + 762 + static int pinmux_select_open(struct inode *inode, struct file *file) 763 + { 764 + return single_open(file, NULL, inode->i_private); 765 + } 766 + 767 + static const struct file_operations pinmux_select_ops = { 768 + .owner = THIS_MODULE, 769 + .open = pinmux_select_open, 770 + .write = pinmux_select, 771 + .llseek = no_llseek, 772 + .release = single_release, 773 + }; 774 + 677 775 void pinmux_init_device_debugfs(struct dentry *devroot, 678 776 struct pinctrl_dev *pctldev) 679 777 { ··· 780 680 devroot, pctldev, &pinmux_functions_fops); 781 681 debugfs_create_file("pinmux-pins", 0444, 782 682 devroot, pctldev, &pinmux_pins_fops); 683 + debugfs_create_file("pinmux-select", 0200, 684 + devroot, pctldev, &pinmux_select_ops); 783 685 } 784 686 785 687 #endif /* CONFIG_DEBUG_FS */