[PATCH v4 11/22] phy: renesas: rcar-gen3-usb2: Use mux-state for phyrst management

Tommaso Merciai posted 22 patches 1 week, 3 days ago
There is a newer version of this series
[PATCH v4 11/22] phy: renesas: rcar-gen3-usb2: Use mux-state for phyrst management
Posted by Tommaso Merciai 1 week, 3 days ago
Add support for selecting the phyrst mux-state using the Linux mux
subsystem in the R-Car Gen3 USB2 PHY driver. This ensures correct hardware
initialization and integration with systems utilizing the mux-state device
tree property.

A temporary wrapper for optional muxes is introduced until native support
is available in the multiplexer subsystem.

Signed-off-by: Tommaso Merciai <tommaso.merciai.xr@bp.renesas.com>
---
v3->v4:
 - Added missing select MULTIPLEXER into Kconfig.

v2->v3:
 - No changes

v1->v2:
 - New patch

 drivers/phy/renesas/Kconfig              |  1 +
 drivers/phy/renesas/phy-rcar-gen3-usb2.c | 34 ++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/drivers/phy/renesas/Kconfig b/drivers/phy/renesas/Kconfig
index 16211072098e..d217c630b2fd 100644
--- a/drivers/phy/renesas/Kconfig
+++ b/drivers/phy/renesas/Kconfig
@@ -30,6 +30,7 @@ config PHY_RCAR_GEN3_USB2
 	depends on EXTCON || !EXTCON # if EXTCON=m, this cannot be built-in
 	depends on USB_SUPPORT
 	select GENERIC_PHY
+	select MULTIPLEXER
 	select USB_COMMON
 	help
 	  Support for USB 2.0 PHY found on Renesas R-Car generation 3 SoCs.
diff --git a/drivers/phy/renesas/phy-rcar-gen3-usb2.c b/drivers/phy/renesas/phy-rcar-gen3-usb2.c
index d2c03a846b58..9a1ea15b9869 100644
--- a/drivers/phy/renesas/phy-rcar-gen3-usb2.c
+++ b/drivers/phy/renesas/phy-rcar-gen3-usb2.c
@@ -17,6 +17,7 @@
 #include <linux/io.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
+#include <linux/mux/consumer.h>
 #include <linux/of.h>
 #include <linux/phy/phy.h>
 #include <linux/platform_device.h>
@@ -938,11 +939,27 @@ static int rcar_gen3_phy_usb2_vbus_regulator_register(struct rcar_gen3_chan *cha
 	return rcar_gen3_phy_usb2_vbus_regulator_get_exclusive_enable(channel, enable);
 }
 
+/* Temporary wrapper until the multiplexer subsystem supports optional muxes */
+static inline struct mux_state *
+devm_mux_state_get_optional(struct device *dev, const char *mux_name)
+{
+	if (!of_property_present(dev->of_node, "mux-states"))
+		return NULL;
+
+	return devm_mux_state_get(dev, mux_name);
+}
+
+static void rcar_gen3_phy_mux_state_deselect(void *data)
+{
+	mux_state_deselect(data);
+}
+
 static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
 	struct rcar_gen3_chan *channel;
 	struct phy_provider *provider;
+	struct mux_state *mux_state;
 	int ret = 0, i, irq;
 
 	if (!dev->of_node) {
@@ -1019,6 +1036,23 @@ static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)
 		phy_set_drvdata(channel->rphys[i].phy, &channel->rphys[i]);
 	}
 
+	mux_state = devm_mux_state_get_optional(dev, NULL);
+	if (IS_ERR(mux_state)) {
+		if (PTR_ERR(mux_state) == -EPROBE_DEFER)
+			return PTR_ERR(mux_state);
+		mux_state = NULL;
+	} else {
+		ret = mux_state_select(mux_state);
+		if (ret)
+			return dev_err_probe(dev, ret, "Failed to select USB mux\n");
+
+		ret = devm_add_action_or_reset(dev, rcar_gen3_phy_mux_state_deselect,
+					       mux_state);
+		if (ret)
+			return dev_err_probe(dev, ret,
+					     "Failed to register USB mux state deselect\n");
+	}
+
 	if (channel->phy_data->no_adp_ctrl && channel->is_otg_channel) {
 		ret = rcar_gen3_phy_usb2_vbus_regulator_register(channel);
 		if (ret)
-- 
2.43.0
Re: [PATCH v4 11/22] phy: renesas: rcar-gen3-usb2: Use mux-state for phyrst management
Posted by Geert Uytterhoeven 6 days, 11 hours ago
Hi Tommaso,

On Fri, 21 Nov 2025 at 16:14, Tommaso Merciai
<tommaso.merciai.xr@bp.renesas.com> wrote:
> Add support for selecting the phyrst mux-state using the Linux mux
> subsystem in the R-Car Gen3 USB2 PHY driver. This ensures correct hardware
> initialization and integration with systems utilizing the mux-state device
> tree property.
>
> A temporary wrapper for optional muxes is introduced until native support
> is available in the multiplexer subsystem.
>
> Signed-off-by: Tommaso Merciai <tommaso.merciai.xr@bp.renesas.com>

Thanks for your patch!

> --- a/drivers/phy/renesas/phy-rcar-gen3-usb2.c
> +++ b/drivers/phy/renesas/phy-rcar-gen3-usb2.c
> @@ -938,11 +939,27 @@ static int rcar_gen3_phy_usb2_vbus_regulator_register(struct rcar_gen3_chan *cha
>         return rcar_gen3_phy_usb2_vbus_regulator_get_exclusive_enable(channel, enable);
>  }
>
> +/* Temporary wrapper until the multiplexer subsystem supports optional muxes */
> +static inline struct mux_state *
> +devm_mux_state_get_optional(struct device *dev, const char *mux_name)
> +{
> +       if (!of_property_present(dev->of_node, "mux-states"))
> +               return NULL;
> +
> +       return devm_mux_state_get(dev, mux_name);
> +}
> +
> +static void rcar_gen3_phy_mux_state_deselect(void *data)
> +{
> +       mux_state_deselect(data);
> +}
> +
>  static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)
>  {
>         struct device *dev = &pdev->dev;
>         struct rcar_gen3_chan *channel;
>         struct phy_provider *provider;
> +       struct mux_state *mux_state;
>         int ret = 0, i, irq;
>
>         if (!dev->of_node) {
> @@ -1019,6 +1036,23 @@ static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)
>                 phy_set_drvdata(channel->rphys[i].phy, &channel->rphys[i]);
>         }
>
> +       mux_state = devm_mux_state_get_optional(dev, NULL);
> +       if (IS_ERR(mux_state)) {
> +               if (PTR_ERR(mux_state) == -EPROBE_DEFER)
> +                       return PTR_ERR(mux_state);
> +               mux_state = NULL;

No need to set mux_state to NULL, as mux_state is not used below.

However, shouldn't you propagate all errors up?
If the mux is not present, mux_state should already be NULL,
i.e. IS_ERR(mux_state) would be false.

> +       } else {
> +               ret = mux_state_select(mux_state);

This causes a crash on R-Car Gen3 and RZ/Five, as mux_state_select()
doesn't handle NULL pointers gracefully yet.

Adding a check like

    -       } else {
    +       } else if (mux_state) {

fixes the issue.

> +               if (ret)
> +                       return dev_err_probe(dev, ret, "Failed to select USB mux\n");
> +
> +               ret = devm_add_action_or_reset(dev, rcar_gen3_phy_mux_state_deselect,
> +                                              mux_state);
> +               if (ret)
> +                       return dev_err_probe(dev, ret,
> +                                            "Failed to register USB mux state deselect\n");
> +       }
> +
>         if (channel->phy_data->no_adp_ctrl && channel->is_otg_channel) {
>                 ret = rcar_gen3_phy_usb2_vbus_regulator_register(channel);
>                 if (ret)

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Re: [PATCH v4 11/22] phy: renesas: rcar-gen3-usb2: Use mux-state for phyrst management
Posted by Tommaso Merciai 6 days, 9 hours ago
Hi Geert,
Thanks for your review!

On Tue, Nov 25, 2025 at 03:10:22PM +0100, Geert Uytterhoeven wrote:
> Hi Tommaso,
> 
> On Fri, 21 Nov 2025 at 16:14, Tommaso Merciai
> <tommaso.merciai.xr@bp.renesas.com> wrote:
> > Add support for selecting the phyrst mux-state using the Linux mux
> > subsystem in the R-Car Gen3 USB2 PHY driver. This ensures correct hardware
> > initialization and integration with systems utilizing the mux-state device
> > tree property.
> >
> > A temporary wrapper for optional muxes is introduced until native support
> > is available in the multiplexer subsystem.
> >
> > Signed-off-by: Tommaso Merciai <tommaso.merciai.xr@bp.renesas.com>
> 
> Thanks for your patch!
> 
> > --- a/drivers/phy/renesas/phy-rcar-gen3-usb2.c
> > +++ b/drivers/phy/renesas/phy-rcar-gen3-usb2.c
> > @@ -938,11 +939,27 @@ static int rcar_gen3_phy_usb2_vbus_regulator_register(struct rcar_gen3_chan *cha
> >         return rcar_gen3_phy_usb2_vbus_regulator_get_exclusive_enable(channel, enable);
> >  }
> >
> > +/* Temporary wrapper until the multiplexer subsystem supports optional muxes */
> > +static inline struct mux_state *
> > +devm_mux_state_get_optional(struct device *dev, const char *mux_name)
> > +{
> > +       if (!of_property_present(dev->of_node, "mux-states"))
> > +               return NULL;
> > +
> > +       return devm_mux_state_get(dev, mux_name);
> > +}
> > +
> > +static void rcar_gen3_phy_mux_state_deselect(void *data)
> > +{
> > +       mux_state_deselect(data);
> > +}
> > +
> >  static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)
> >  {
> >         struct device *dev = &pdev->dev;
> >         struct rcar_gen3_chan *channel;
> >         struct phy_provider *provider;
> > +       struct mux_state *mux_state;
> >         int ret = 0, i, irq;
> >
> >         if (!dev->of_node) {
> > @@ -1019,6 +1036,23 @@ static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)
> >                 phy_set_drvdata(channel->rphys[i].phy, &channel->rphys[i]);
> >         }
> >
> > +       mux_state = devm_mux_state_get_optional(dev, NULL);
> > +       if (IS_ERR(mux_state)) {
> > +               if (PTR_ERR(mux_state) == -EPROBE_DEFER)
> > +                       return PTR_ERR(mux_state);
> > +               mux_state = NULL;
> 
> No need to set mux_state to NULL, as mux_state is not used below.
> 
> However, shouldn't you propagate all errors up?
> If the mux is not present, mux_state should already be NULL,
> i.e. IS_ERR(mux_state) would be false.
> 
> > +       } else {
> > +               ret = mux_state_select(mux_state);
> 
> This causes a crash on R-Car Gen3 and RZ/Five, as mux_state_select()
> doesn't handle NULL pointers gracefully yet.
> 
> Adding a check like
> 
>     -       } else {
>     +       } else if (mux_state) {
> 
> fixes the issue.

Thank you for checking this!

Ack :)
I will switch to:

	mux_state = devm_mux_state_get_optional(dev, NULL);
	if (IS_ERR(mux_state)) {
		return PTR_ERR(mux_state);
	} else if (mux_state) {
		ret = mux_state_select(mux_state);
		if (ret)
			return dev_err_probe(dev, ret, "Failed to select USB mux\n");

		ret = devm_add_action_or_reset(dev, rcar_gen3_phy_mux_state_deselect,
					       mux_state);
		if (ret)
			return dev_err_probe(dev, ret,
					     "Failed to register USB mux state deselect\n");
	}

In v5.


Kind Regards,
Tommaso

> 
> > +               if (ret)
> > +                       return dev_err_probe(dev, ret, "Failed to select USB mux\n");
> > +
> > +               ret = devm_add_action_or_reset(dev, rcar_gen3_phy_mux_state_deselect,
> > +                                              mux_state);
> > +               if (ret)
> > +                       return dev_err_probe(dev, ret,
> > +                                            "Failed to register USB mux state deselect\n");
> > +       }
> > +
> >         if (channel->phy_data->no_adp_ctrl && channel->is_otg_channel) {
> >                 ret = rcar_gen3_phy_usb2_vbus_regulator_register(channel);
> >                 if (ret)
> 
> Gr{oetje,eeting}s,
> 
>                         Geert
> 
> -- 
> Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
> 
> In personal conversations with technical people, I call myself a hacker. But
> when I'm talking to journalists I just say "programmer" or something like that.
>                                 -- Linus Torvalds
>
Re: [PATCH v4 11/22] phy: renesas: rcar-gen3-usb2: Use mux-state for phyrst management
Posted by Geert Uytterhoeven 5 days, 17 hours ago
Hi Tommaso,

On Tue, 25 Nov 2025 at 16:57, Tommaso Merciai
<tommaso.merciai.xr@bp.renesas.com> wrote:
> On Tue, Nov 25, 2025 at 03:10:22PM +0100, Geert Uytterhoeven wrote:
> > On Fri, 21 Nov 2025 at 16:14, Tommaso Merciai
> > <tommaso.merciai.xr@bp.renesas.com> wrote:
> > > Add support for selecting the phyrst mux-state using the Linux mux
> > > subsystem in the R-Car Gen3 USB2 PHY driver. This ensures correct hardware
> > > initialization and integration with systems utilizing the mux-state device
> > > tree property.
> > >
> > > A temporary wrapper for optional muxes is introduced until native support
> > > is available in the multiplexer subsystem.
> > >
> > > Signed-off-by: Tommaso Merciai <tommaso.merciai.xr@bp.renesas.com>

> > > --- a/drivers/phy/renesas/phy-rcar-gen3-usb2.c
> > > +++ b/drivers/phy/renesas/phy-rcar-gen3-usb2.c
> > > @@ -938,11 +939,27 @@ static int rcar_gen3_phy_usb2_vbus_regulator_register(struct rcar_gen3_chan *cha
> > >         return rcar_gen3_phy_usb2_vbus_regulator_get_exclusive_enable(channel, enable);
> > >  }
> > >
> > > +/* Temporary wrapper until the multiplexer subsystem supports optional muxes */
> > > +static inline struct mux_state *
> > > +devm_mux_state_get_optional(struct device *dev, const char *mux_name)
> > > +{
> > > +       if (!of_property_present(dev->of_node, "mux-states"))
> > > +               return NULL;
> > > +
> > > +       return devm_mux_state_get(dev, mux_name);
> > > +}
> > > +
> > > +static void rcar_gen3_phy_mux_state_deselect(void *data)
> > > +{
> > > +       mux_state_deselect(data);
> > > +}
> > > +
> > >  static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)
> > >  {
> > >         struct device *dev = &pdev->dev;
> > >         struct rcar_gen3_chan *channel;
> > >         struct phy_provider *provider;
> > > +       struct mux_state *mux_state;
> > >         int ret = 0, i, irq;
> > >
> > >         if (!dev->of_node) {
> > > @@ -1019,6 +1036,23 @@ static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)
> > >                 phy_set_drvdata(channel->rphys[i].phy, &channel->rphys[i]);
> > >         }
> > >
> > > +       mux_state = devm_mux_state_get_optional(dev, NULL);
> > > +       if (IS_ERR(mux_state)) {
> > > +               if (PTR_ERR(mux_state) == -EPROBE_DEFER)
> > > +                       return PTR_ERR(mux_state);
> > > +               mux_state = NULL;
> >
> > No need to set mux_state to NULL, as mux_state is not used below.
> >
> > However, shouldn't you propagate all errors up?
> > If the mux is not present, mux_state should already be NULL,
> > i.e. IS_ERR(mux_state) would be false.
> >
> > > +       } else {
> > > +               ret = mux_state_select(mux_state);
> >
> > This causes a crash on R-Car Gen3 and RZ/Five, as mux_state_select()
> > doesn't handle NULL pointers gracefully yet.
> >
> > Adding a check like
> >
> >     -       } else {
> >     +       } else if (mux_state) {
> >
> > fixes the issue.
>
> Thank you for checking this!
>
> Ack :)
> I will switch to:
>
>         mux_state = devm_mux_state_get_optional(dev, NULL);
>         if (IS_ERR(mux_state)) {
>                 return PTR_ERR(mux_state);
>         } else if (mux_state) {

Please no else after return.

>                 ret = mux_state_select(mux_state);
>                 if (ret)
>                         return dev_err_probe(dev, ret, "Failed to select USB mux\n");
>
>                 ret = devm_add_action_or_reset(dev, rcar_gen3_phy_mux_state_deselect,
>                                                mux_state);
>                 if (ret)
>                         return dev_err_probe(dev, ret,
>                                              "Failed to register USB mux state deselect\n");
>         }
>
> In v5.

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds