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

Configure Feed

Select the types of activity you want to include in your feed.

phy: renesas: rcar-gen3-usb2: Add regulator for OTG VBUS control

Enable OTG VBUS control on R-Car Gen3 USB2 PHY by registering a regulator
driver that manages the VBOUT line. This change allows the controller to
handle VBUS output for OTG ports using the regulator framework when the
platform requires hardware-based VBUS control.

Without this, some platforms cannot properly manage VBUS power on OTG-
capable ports, leading to potential USB functionality issues.

Signed-off-by: Tommaso Merciai <tommaso.merciai.xr@bp.renesas.com>
Link: https://patch.msgid.link/6c1aebf60b4d8ff0c51a8243c68b397c1a384867.1766405010.git.tommaso.merciai.xr@bp.renesas.com
Signed-off-by: Vinod Koul <vkoul@kernel.org>

authored by

Tommaso Merciai and committed by
Vinod Koul
b6d7dd15 230c817a

+137 -5
+137 -5
drivers/phy/renesas/phy-rcar-gen3-usb2.c
··· 22 22 #include <linux/platform_device.h> 23 23 #include <linux/pm_runtime.h> 24 24 #include <linux/regulator/consumer.h> 25 + #include <linux/regulator/driver.h> 25 26 #include <linux/reset.h> 26 27 #include <linux/string.h> 27 28 #include <linux/usb/of.h> ··· 142 141 bool extcon_host; 143 142 bool is_otg_channel; 144 143 bool uses_otg_pins; 144 + bool otg_internal_reg; 145 145 }; 146 146 147 147 struct rcar_gen3_phy_drv_data { ··· 227 225 228 226 static void rcar_gen3_enable_vbus_ctrl(struct rcar_gen3_chan *ch, int vbus) 229 227 { 228 + if (ch->otg_internal_reg) { 229 + regulator_hardware_enable(ch->vbus, vbus); 230 + return; 231 + } 232 + 230 233 if (ch->phy_data->no_adp_ctrl || ch->phy_data->vblvl_ctrl) { 231 234 if (ch->vbus) 232 235 regulator_hardware_enable(ch->vbus, vbus); ··· 600 593 u32 val; 601 594 int ret = 0; 602 595 603 - if (channel->vbus) { 596 + if (channel->vbus && !channel->otg_internal_reg) { 604 597 ret = regulator_enable(channel->vbus); 605 598 if (ret) 606 599 return ret; ··· 641 634 } 642 635 } 643 636 644 - if (channel->vbus) 637 + if (channel->vbus && !channel->otg_internal_reg) 645 638 ret = regulator_disable(channel->vbus); 646 639 647 640 return ret; ··· 816 809 return 0; 817 810 } 818 811 812 + static int rcar_gen3_phy_usb2_regulator_endisable(struct regulator_dev *rdev, 813 + bool enable) 814 + { 815 + struct rcar_gen3_chan *channel = rdev_get_drvdata(rdev); 816 + struct device *dev = channel->dev; 817 + int ret; 818 + 819 + ret = pm_runtime_resume_and_get(dev); 820 + if (ret < 0) { 821 + dev_warn(dev, "pm_runtime_get failed: %i\n", ret); 822 + return ret; 823 + } 824 + 825 + rcar_gen3_phy_usb2_set_vbus(channel, USB2_VBCTRL, 826 + USB2_VBCTRL_VBOUT, enable); 827 + pm_runtime_put_noidle(dev); 828 + 829 + return ret; 830 + } 831 + 832 + static int rcar_gen3_phy_usb2_regulator_enable(struct regulator_dev *rdev) 833 + { 834 + return rcar_gen3_phy_usb2_regulator_endisable(rdev, true); 835 + } 836 + 837 + static int rcar_gen3_phy_usb2_regulator_disable(struct regulator_dev *rdev) 838 + { 839 + return rcar_gen3_phy_usb2_regulator_endisable(rdev, false); 840 + } 841 + 842 + static int rcar_gen3_phy_usb2_regulator_is_enabled(struct regulator_dev *rdev) 843 + { 844 + struct rcar_gen3_chan *channel = rdev_get_drvdata(rdev); 845 + void __iomem *usb2_base = channel->base; 846 + struct device *dev = channel->dev; 847 + u32 vbus_ctrl_reg = USB2_VBCTRL; 848 + u32 val; 849 + int ret; 850 + 851 + ret = pm_runtime_resume_and_get(dev); 852 + if (ret < 0) { 853 + dev_warn(dev, "pm_runtime_get failed: %i\n", ret); 854 + return ret; 855 + } 856 + 857 + val = readl(usb2_base + vbus_ctrl_reg); 858 + 859 + pm_runtime_put_noidle(dev); 860 + dev_dbg(channel->dev, "%s: %08x\n", __func__, val); 861 + 862 + return (val & USB2_VBCTRL_VBOUT) ? 1 : 0; 863 + } 864 + 865 + static const struct regulator_ops rcar_gen3_phy_usb2_regulator_ops = { 866 + .enable = rcar_gen3_phy_usb2_regulator_enable, 867 + .disable = rcar_gen3_phy_usb2_regulator_disable, 868 + .is_enabled = rcar_gen3_phy_usb2_regulator_is_enabled, 869 + }; 870 + 871 + static const struct regulator_desc rcar_gen3_phy_usb2_regulator = { 872 + .name = "otg-vbus-regulator", 873 + .of_match = of_match_ptr("vbus-regulator"), 874 + .ops = &rcar_gen3_phy_usb2_regulator_ops, 875 + .type = REGULATOR_VOLTAGE, 876 + .owner = THIS_MODULE, 877 + .fixed_uV = 5000000, 878 + .n_voltages = 1, 879 + }; 880 + 881 + static void rcar_gen3_phy_usb2_vbus_disable_action(void *data) 882 + { 883 + struct regulator *vbus = data; 884 + 885 + regulator_disable(vbus); 886 + } 887 + 888 + static int rcar_gen3_phy_usb2_vbus_regulator_get_exclusive_enable(struct rcar_gen3_chan *channel, 889 + bool enable) 890 + { 891 + struct device *dev = channel->dev; 892 + int ret; 893 + 894 + channel->vbus = devm_regulator_get_exclusive(dev, "vbus"); 895 + if (IS_ERR(channel->vbus)) 896 + return PTR_ERR(channel->vbus); 897 + 898 + if (!enable) 899 + return 0; 900 + 901 + ret = regulator_enable(channel->vbus); 902 + if (ret) 903 + return ret; 904 + 905 + return devm_add_action_or_reset(dev, rcar_gen3_phy_usb2_vbus_disable_action, 906 + channel->vbus); 907 + } 908 + 909 + static int rcar_gen3_phy_usb2_vbus_regulator_register(struct rcar_gen3_chan *channel) 910 + { 911 + struct device *dev = channel->dev; 912 + struct regulator_config rcfg = { .dev = dev, }; 913 + struct regulator_dev *rdev; 914 + bool enable = false; 915 + 916 + rcfg.of_node = of_get_available_child_by_name(dev->of_node, 917 + "vbus-regulator"); 918 + if (rcfg.of_node) { 919 + rcfg.driver_data = channel; 920 + rdev = devm_regulator_register(dev, &rcar_gen3_phy_usb2_regulator, 921 + &rcfg); 922 + of_node_put(rcfg.of_node); 923 + if (IS_ERR(rdev)) 924 + return dev_err_probe(dev, PTR_ERR(rdev), 925 + "Failed to create vbus-regulator\n"); 926 + 927 + channel->otg_internal_reg = true; 928 + enable = true; 929 + } 930 + 931 + return rcar_gen3_phy_usb2_vbus_regulator_get_exclusive_enable(channel, enable); 932 + } 933 + 819 934 static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev) 820 935 { 821 936 struct device *dev = &pdev->dev; ··· 1019 890 phy_set_drvdata(channel->rphys[i].phy, &channel->rphys[i]); 1020 891 } 1021 892 1022 - if (channel->phy_data->no_adp_ctrl && channel->is_otg_channel) 1023 - channel->vbus = devm_regulator_get_exclusive(dev, "vbus"); 1024 - else 893 + if (channel->phy_data->no_adp_ctrl && channel->is_otg_channel) { 894 + ret = rcar_gen3_phy_usb2_vbus_regulator_register(channel); 895 + if (ret) 896 + return ret; 897 + } else { 1025 898 channel->vbus = devm_regulator_get_optional(dev, "vbus"); 899 + } 1026 900 if (IS_ERR(channel->vbus)) { 1027 901 if (PTR_ERR(channel->vbus) == -EPROBE_DEFER) 1028 902 return PTR_ERR(channel->vbus);