2023-06-21 16:19:08

by Benjamin Bara

[permalink] [raw]
Subject: [PATCH v3 1/3] usb: misc: onboard-hub: support multiple power supplies

From: Benjamin Bara <[email protected]>

As some of the onboard hubs require multiple power supplies, provide the
environment to support them.

Signed-off-by: Benjamin Bara <[email protected]>
---
v3:
- fix nits mentioned in v2

v2:
- replace (err != 0) with (err)
---
drivers/usb/misc/onboard_usb_hub.c | 39 ++++++++++++++++++++++++++++++--------
drivers/usb/misc/onboard_usb_hub.h | 1 +
2 files changed, 32 insertions(+), 8 deletions(-)

diff --git a/drivers/usb/misc/onboard_usb_hub.c b/drivers/usb/misc/onboard_usb_hub.c
index 12fc6eb67c3b..a56e712d3a45 100644
--- a/drivers/usb/misc/onboard_usb_hub.c
+++ b/drivers/usb/misc/onboard_usb_hub.c
@@ -27,6 +27,13 @@

#include "onboard_usb_hub.h"

+#define MAX_SUPPLIES 2
+
+static const char * const supply_names[] = {
+ "vdd",
+ "vdd2",
+};
+
static void onboard_hub_attach_usb_driver(struct work_struct *work);

static struct usb_device_driver onboard_hub_usbdev_driver;
@@ -40,7 +47,8 @@ struct usbdev_node {
};

struct onboard_hub {
- struct regulator *vdd;
+ struct regulator_bulk_data supplies[MAX_SUPPLIES];
+ unsigned int num_supplies;
struct device *dev;
const struct onboard_hub_pdata *pdata;
struct gpio_desc *reset_gpio;
@@ -55,9 +63,9 @@ static int onboard_hub_power_on(struct onboard_hub *hub)
{
int err;

- err = regulator_enable(hub->vdd);
+ err = regulator_bulk_enable(hub->num_supplies, hub->supplies);
if (err) {
- dev_err(hub->dev, "failed to enable regulator: %d\n", err);
+ dev_err(hub->dev, "failed to enable supplies: %d\n", err);
return err;
}

@@ -75,9 +83,9 @@ static int onboard_hub_power_off(struct onboard_hub *hub)

gpiod_set_value_cansleep(hub->reset_gpio, 1);

- err = regulator_disable(hub->vdd);
+ err = regulator_bulk_disable(hub->num_supplies, hub->supplies);
if (err) {
- dev_err(hub->dev, "failed to disable regulator: %d\n", err);
+ dev_err(hub->dev, "failed to disable supplies: %d\n", err);
return err;
}

@@ -232,6 +240,7 @@ static int onboard_hub_probe(struct platform_device *pdev)
const struct of_device_id *of_id;
struct device *dev = &pdev->dev;
struct onboard_hub *hub;
+ unsigned int i;
int err;

hub = devm_kzalloc(dev, sizeof(*hub), GFP_KERNEL);
@@ -246,9 +255,23 @@ static int onboard_hub_probe(struct platform_device *pdev)
if (!hub->pdata)
return -EINVAL;

- hub->vdd = devm_regulator_get(dev, "vdd");
- if (IS_ERR(hub->vdd))
- return PTR_ERR(hub->vdd);
+ if (hub->pdata->num_supplies > MAX_SUPPLIES)
+ return dev_err_probe(dev, -EINVAL, "max %d supplies supported!\n",
+ MAX_SUPPLIES);
+
+ if (hub->pdata->num_supplies)
+ hub->num_supplies = hub->pdata->num_supplies;
+ else
+ hub->num_supplies = 1;
+
+ for (i = 0; i < MAX_SUPPLIES; i++)
+ hub->supplies[i].supply = supply_names[i];
+
+ err = devm_regulator_bulk_get(dev, hub->num_supplies, hub->supplies);
+ if (err) {
+ dev_err(dev, "Failed to get regulator supplies: %d\n", err);
+ return err;
+ }

hub->reset_gpio = devm_gpiod_get_optional(dev, "reset",
GPIOD_OUT_HIGH);
diff --git a/drivers/usb/misc/onboard_usb_hub.h b/drivers/usb/misc/onboard_usb_hub.h
index aca5f50eb0da..7a6d497ce849 100644
--- a/drivers/usb/misc/onboard_usb_hub.h
+++ b/drivers/usb/misc/onboard_usb_hub.h
@@ -8,6 +8,7 @@

struct onboard_hub_pdata {
unsigned long reset_us; /* reset pulse width in us */
+ unsigned int num_supplies; /* number of supplies: 0 considered as 1 */
};

static const struct onboard_hub_pdata microchip_usb424_data = {

--
2.34.1



2023-06-21 16:32:40

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH v3 1/3] usb: misc: onboard-hub: support multiple power supplies

On Wed, Jun 21, 2023 at 05:58:30PM +0200, Benjamin Bara wrote:
> From: Benjamin Bara <[email protected]>
>
> As some of the onboard hubs require multiple power supplies, provide the
> environment to support them.
>
> Signed-off-by: Benjamin Bara <[email protected]>
> ---
> v3:
> - fix nits mentioned in v2
>
> v2:
> - replace (err != 0) with (err)
> ---
> drivers/usb/misc/onboard_usb_hub.c | 39 ++++++++++++++++++++++++++++++--------
> drivers/usb/misc/onboard_usb_hub.h | 1 +
> 2 files changed, 32 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/usb/misc/onboard_usb_hub.c b/drivers/usb/misc/onboard_usb_hub.c
> index 12fc6eb67c3b..a56e712d3a45 100644
> --- a/drivers/usb/misc/onboard_usb_hub.c
> +++ b/drivers/usb/misc/onboard_usb_hub.c
> @@ -27,6 +27,13 @@
>
> #include "onboard_usb_hub.h"
>
> +#define MAX_SUPPLIES 2

Why 2?

> +
> +static const char * const supply_names[] = {
> + "vdd",
> + "vdd2",
> +};

Do those names have anything to do with the number above? If so, please
document it!

> struct onboard_hub_pdata {
> unsigned long reset_us; /* reset pulse width in us */
> + unsigned int num_supplies; /* number of supplies: 0 considered as 1 */

I can not understand that comment at all :(


2023-06-21 16:36:29

by Benjamin Bara

[permalink] [raw]
Subject: Re: [PATCH v3 1/3] usb: misc: onboard-hub: support multiple power supplies

On Wed, 21 Jun 2023 at 18:07, Greg Kroah-Hartman
<[email protected]> wrote:
> On Wed, Jun 21, 2023 at 05:58:30PM +0200, Benjamin Bara wrote:
> > From: Benjamin Bara <[email protected]>
> >
> > As some of the onboard hubs require multiple power supplies, provide the
> > environment to support them.
> >
> > Signed-off-by: Benjamin Bara <[email protected]>
> > ---
> > v3:
> > - fix nits mentioned in v2
> >
> > v2:
> > - replace (err != 0) with (err)
> > ---
> > drivers/usb/misc/onboard_usb_hub.c | 39 ++++++++++++++++++++++++++++++--------
> > drivers/usb/misc/onboard_usb_hub.h | 1 +
> > 2 files changed, 32 insertions(+), 8 deletions(-)
> >
> > diff --git a/drivers/usb/misc/onboard_usb_hub.c b/drivers/usb/misc/onboard_usb_hub.c
> > index 12fc6eb67c3b..a56e712d3a45 100644
> > --- a/drivers/usb/misc/onboard_usb_hub.c
> > +++ b/drivers/usb/misc/onboard_usb_hub.c
> > @@ -27,6 +27,13 @@
> >
> > #include "onboard_usb_hub.h"
> >
> > +#define MAX_SUPPLIES 2
>
> Why 2?

I picked 2 because with 3/3, this is the maximum of "required" supplies. The
currently implemented ones require only one (up to now just named "vdd"). The
new one added in 3/3 requires 2, therefore I tried to be generic if some future
hub might require 3 or more.

> > +
> > +static const char * const supply_names[] = {
> > + "vdd",
> > + "vdd2",
> > +};
>
> Do those names have anything to do with the number above? If so, please
> document it!

I picked "vdd" for the first to be compatible with the existing device-trees. As
the actual names differ between hubs, I thought it might be generic to just use
"vdd2" here. If I should add some comment like "if you increase MAX_SUPPLIES,
please also add a supply_name below", I can do that. I could also implement
"vdd${i+1}" for i>0 instead.

>
> > struct onboard_hub_pdata {
> > unsigned long reset_us; /* reset pulse width in us */
> > + unsigned int num_supplies; /* number of supplies: 0 considered as 1 */
>
> I can not understand that comment at all :(

This should just indicate that leaving the field empty means one supply is
required. Maybe "defaults to 1" is better?

2023-06-21 16:47:29

by Matthias Kaehlcke

[permalink] [raw]
Subject: Re: [PATCH v3 1/3] usb: misc: onboard-hub: support multiple power supplies

On Wed, Jun 21, 2023 at 06:22:41PM +0200, Benjamin Bara wrote:
> On Wed, 21 Jun 2023 at 18:07, Greg Kroah-Hartman
> <[email protected]> wrote:
> > On Wed, Jun 21, 2023 at 05:58:30PM +0200, Benjamin Bara wrote:
> > > From: Benjamin Bara <[email protected]>
> > >
> > > As some of the onboard hubs require multiple power supplies, provide the
> > > environment to support them.
> > >
> > > Signed-off-by: Benjamin Bara <[email protected]>
> > > ---
> > > v3:
> > > - fix nits mentioned in v2
> > >
> > > v2:
> > > - replace (err != 0) with (err)
> > > ---
> > > drivers/usb/misc/onboard_usb_hub.c | 39 ++++++++++++++++++++++++++++++--------
> > > drivers/usb/misc/onboard_usb_hub.h | 1 +
> > > 2 files changed, 32 insertions(+), 8 deletions(-)
> > >
> > > diff --git a/drivers/usb/misc/onboard_usb_hub.c b/drivers/usb/misc/onboard_usb_hub.c
> > > index 12fc6eb67c3b..a56e712d3a45 100644
> > > --- a/drivers/usb/misc/onboard_usb_hub.c
> > > +++ b/drivers/usb/misc/onboard_usb_hub.c
> > > @@ -27,6 +27,13 @@
> > >
> > > #include "onboard_usb_hub.h"
> > >
> > > +#define MAX_SUPPLIES 2
> >
> > Why 2?
>
> I picked 2 because with 3/3, this is the maximum of "required" supplies. The
> currently implemented ones require only one (up to now just named "vdd"). The
> new one added in 3/3 requires 2, therefore I tried to be generic if some future
> hub might require 3 or more.
>
> > > +
> > > +static const char * const supply_names[] = {
> > > + "vdd",
> > > + "vdd2",
> > > +};
> >
> > Do those names have anything to do with the number above? If so, please
> > document it!
>
> I picked "vdd" for the first to be compatible with the existing device-trees. As
> the actual names differ between hubs, I thought it might be generic to just use
> "vdd2" here. If I should add some comment like "if you increase MAX_SUPPLIES,
> please also add a supply_name below", I can do that. I could also implement
> "vdd${i+1}" for i>0 instead.
>
> >
> > > struct onboard_hub_pdata {
> > > unsigned long reset_us; /* reset pulse width in us */
> > > + unsigned int num_supplies; /* number of supplies: 0 considered as 1 */
> >
> > I can not understand that comment at all :(
>
> This should just indicate that leaving the field empty means one supply is
> required. Maybe "defaults to 1" is better?

Whatever the comment, I suggest to put it in parentheses, rather than after a
colon. If you keep the current style maybe s/considered/is interpreted/.

Another option would be to initialize all existing hubs to num_supplies = 1,
then there is no need for a comment and the check in _probe(). That might
be the clearest thing to do, even though it adds a few extra lines.