2023-01-12 22:27:02

by Prashant Malani

[permalink] [raw]
Subject: [PATCH 0/3] usb: typec: Add retimer support to bus code

This is a short series which introduces retimer control to Type-C bus
code, chiefly in the context of alternate mode drivers.

Patch 1/3 adds retimer handles to the altmode struct.

Patch 2/3 introduces a wrapper where all Type-C switch commands can be
placed; this is a foundation patch for Patch 3/3. No functional changes
are introduced by this patch.

Patch 3/3 introduces the typec_retimer_set() call to the switch wrapper.

Prashant Malani (3):
usb: typec: Add retimer handle to port altmode
usb: typec: Add wrapper for bus switch set code
usb: typec: Make bus switch code retimer-aware

drivers/usb/typec/bus.c | 33 +++++++++++++++++++++++++++++++--
drivers/usb/typec/bus.h | 2 ++
drivers/usb/typec/class.c | 15 +++++++++++++--
drivers/usb/typec/retimer.h | 2 +-
4 files changed, 47 insertions(+), 5 deletions(-)

--
2.39.0.314.g84b9a713c41-goog


2023-01-12 22:27:02

by Prashant Malani

[permalink] [raw]
Subject: [PATCH 1/3] usb: typec: Add retimer handle to port altmode

Just like it does with muxes, the Type-C bus code can update the state
of connected retimers (especially when altmode-related transitions
occur). Add a retimer handle to the port altmode struct to enable this.

Signed-off-by: Prashant Malani <[email protected]>
---
drivers/usb/typec/bus.h | 2 ++
drivers/usb/typec/class.c | 15 +++++++++++++--
2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/typec/bus.h b/drivers/usb/typec/bus.h
index 56dec268d4dd..c89168857417 100644
--- a/drivers/usb/typec/bus.h
+++ b/drivers/usb/typec/bus.h
@@ -7,11 +7,13 @@

struct bus_type;
struct typec_mux;
+struct typec_retimer;

struct altmode {
unsigned int id;
struct typec_altmode adev;
struct typec_mux *mux;
+ struct typec_retimer *retimer;

enum typec_port_data roles;

diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index 5897905cb4f0..ed3d070b1ca4 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -583,6 +583,7 @@ void typec_unregister_altmode(struct typec_altmode *adev)
{
if (IS_ERR_OR_NULL(adev))
return;
+ typec_retimer_put(to_altmode(adev)->retimer);
typec_mux_put(to_altmode(adev)->mux);
device_unregister(&adev->dev);
}
@@ -2108,16 +2109,26 @@ typec_port_register_altmode(struct typec_port *port,
{
struct typec_altmode *adev;
struct typec_mux *mux;
+ struct typec_retimer *retimer;

mux = typec_mux_get(&port->dev, desc);
if (IS_ERR(mux))
return ERR_CAST(mux);

+ retimer = typec_retimer_get(&port->dev);
+ if (IS_ERR(retimer)) {
+ typec_mux_put(mux);
+ return ERR_CAST(retimer);
+ }
+
adev = typec_register_altmode(&port->dev, desc);
- if (IS_ERR(adev))
+ if (IS_ERR(adev)) {
+ typec_retimer_put(retimer);
typec_mux_put(mux);
- else
+ } else {
to_altmode(adev)->mux = mux;
+ to_altmode(adev)->retimer = retimer;
+ }

return adev;
}
--
2.39.0.314.g84b9a713c41-goog

2023-01-12 23:02:28

by Prashant Malani

[permalink] [raw]
Subject: [PATCH 2/3] usb: typec: Add wrapper for bus switch set code

Add a wrapper that calls the set() function for various switches
associated with a port altmode.

Right now, it just wraps the existing typec_mux_set() command,
but it can be expanded to include other switches in future patches.

No functional changes introduced by this patch.

Signed-off-by: Prashant Malani <[email protected]>
---
drivers/usb/typec/bus.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/typec/bus.c b/drivers/usb/typec/bus.c
index 31c2a3130cad..9f1bbd26ca47 100644
--- a/drivers/usb/typec/bus.c
+++ b/drivers/usb/typec/bus.c
@@ -27,6 +27,13 @@ typec_altmode_set_mux(struct altmode *alt, unsigned long conf, void *data)
return typec_mux_set(alt->mux, &state);
}

+/* Wrapper to set various Type-C port switches together. */
+static inline int
+typec_altmode_set_switches(struct altmode *alt, unsigned long conf, void *data)
+{
+ return typec_altmode_set_mux(alt, conf, data);
+}
+
static int typec_altmode_set_state(struct typec_altmode *adev,
unsigned long conf, void *data)
{
@@ -35,7 +42,7 @@ static int typec_altmode_set_state(struct typec_altmode *adev,

port_altmode = is_port ? to_altmode(adev) : to_altmode(adev)->partner;

- return typec_altmode_set_mux(port_altmode, conf, data);
+ return typec_altmode_set_switches(port_altmode, conf, data);
}

/* -------------------------------------------------------------------------- */
@@ -73,7 +80,7 @@ int typec_altmode_notify(struct typec_altmode *adev,
is_port = is_typec_port(adev->dev.parent);
partner = altmode->partner;

- ret = typec_altmode_set_mux(is_port ? altmode : partner, conf, data);
+ ret = typec_altmode_set_switches(is_port ? altmode : partner, conf, data);
if (ret)
return ret;

--
2.39.0.314.g84b9a713c41-goog

2023-01-12 23:03:55

by Prashant Malani

[permalink] [raw]
Subject: [PATCH 3/3] usb: typec: Make bus switch code retimer-aware

Since ports can have retimers associated with them, update the Type-C
alternate mode bus code to also set retimer state when the switch state
is updated.

While we are here, make the typec_retimer_dev_type declaration in the
retimer.h file as extern, so that the header file can be successfully
included in the bus code without redeclaration compilation errors.

Signed-off-by: Prashant Malani <[email protected]>
---
drivers/usb/typec/bus.c | 22 ++++++++++++++++++++++
drivers/usb/typec/retimer.h | 2 +-
2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/typec/bus.c b/drivers/usb/typec/bus.c
index 9f1bbd26ca47..0c8d554240be 100644
--- a/drivers/usb/typec/bus.c
+++ b/drivers/usb/typec/bus.c
@@ -11,6 +11,22 @@
#include "bus.h"
#include "class.h"
#include "mux.h"
+#include "retimer.h"
+
+static inline int
+typec_altmode_set_retimer(struct altmode *alt, unsigned long conf, void *data)
+{
+ struct typec_retimer_state state;
+
+ if (!alt->retimer)
+ return 0;
+
+ state.alt = &alt->adev;
+ state.mode = conf;
+ state.data = data;
+
+ return typec_retimer_set(alt->retimer, &state);
+}

static inline int
typec_altmode_set_mux(struct altmode *alt, unsigned long conf, void *data)
@@ -31,6 +47,12 @@ typec_altmode_set_mux(struct altmode *alt, unsigned long conf, void *data)
static inline int
typec_altmode_set_switches(struct altmode *alt, unsigned long conf, void *data)
{
+ int ret;
+
+ ret = typec_altmode_set_retimer(alt, conf, data);
+ if (ret)
+ return ret;
+
return typec_altmode_set_mux(alt, conf, data);
}

diff --git a/drivers/usb/typec/retimer.h b/drivers/usb/typec/retimer.h
index e34bd23323be..d6a5ef9881e1 100644
--- a/drivers/usb/typec/retimer.h
+++ b/drivers/usb/typec/retimer.h
@@ -12,7 +12,7 @@ struct typec_retimer {

#define to_typec_retimer(_dev_) container_of(_dev_, struct typec_retimer, dev)

-const struct device_type typec_retimer_dev_type;
+extern const struct device_type typec_retimer_dev_type;

#define is_typec_retimer(dev) ((dev)->type == &typec_retimer_dev_type)

--
2.39.0.314.g84b9a713c41-goog

2023-01-17 12:22:38

by Heikki Krogerus

[permalink] [raw]
Subject: Re: [PATCH 1/3] usb: typec: Add retimer handle to port altmode

On Thu, Jan 12, 2023 at 10:16:06PM +0000, Prashant Malani wrote:
> Just like it does with muxes, the Type-C bus code can update the state
> of connected retimers (especially when altmode-related transitions
> occur). Add a retimer handle to the port altmode struct to enable this.
>
> Signed-off-by: Prashant Malani <[email protected]>

Reviewed-by: Heikki Krogerus <[email protected]>

> ---
> drivers/usb/typec/bus.h | 2 ++
> drivers/usb/typec/class.c | 15 +++++++++++++--
> 2 files changed, 15 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/usb/typec/bus.h b/drivers/usb/typec/bus.h
> index 56dec268d4dd..c89168857417 100644
> --- a/drivers/usb/typec/bus.h
> +++ b/drivers/usb/typec/bus.h
> @@ -7,11 +7,13 @@
>
> struct bus_type;
> struct typec_mux;
> +struct typec_retimer;
>
> struct altmode {
> unsigned int id;
> struct typec_altmode adev;
> struct typec_mux *mux;
> + struct typec_retimer *retimer;
>
> enum typec_port_data roles;
>
> diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
> index 5897905cb4f0..ed3d070b1ca4 100644
> --- a/drivers/usb/typec/class.c
> +++ b/drivers/usb/typec/class.c
> @@ -583,6 +583,7 @@ void typec_unregister_altmode(struct typec_altmode *adev)
> {
> if (IS_ERR_OR_NULL(adev))
> return;
> + typec_retimer_put(to_altmode(adev)->retimer);
> typec_mux_put(to_altmode(adev)->mux);
> device_unregister(&adev->dev);
> }
> @@ -2108,16 +2109,26 @@ typec_port_register_altmode(struct typec_port *port,
> {
> struct typec_altmode *adev;
> struct typec_mux *mux;
> + struct typec_retimer *retimer;
>
> mux = typec_mux_get(&port->dev, desc);
> if (IS_ERR(mux))
> return ERR_CAST(mux);
>
> + retimer = typec_retimer_get(&port->dev);
> + if (IS_ERR(retimer)) {
> + typec_mux_put(mux);
> + return ERR_CAST(retimer);
> + }
> +
> adev = typec_register_altmode(&port->dev, desc);
> - if (IS_ERR(adev))
> + if (IS_ERR(adev)) {
> + typec_retimer_put(retimer);
> typec_mux_put(mux);
> - else
> + } else {
> to_altmode(adev)->mux = mux;
> + to_altmode(adev)->retimer = retimer;
> + }
>
> return adev;
> }
> --
> 2.39.0.314.g84b9a713c41-goog

--
heikki

2023-01-17 12:22:40

by Heikki Krogerus

[permalink] [raw]
Subject: Re: [PATCH 2/3] usb: typec: Add wrapper for bus switch set code

On Thu, Jan 12, 2023 at 10:16:07PM +0000, Prashant Malani wrote:
> Add a wrapper that calls the set() function for various switches
> associated with a port altmode.
>
> Right now, it just wraps the existing typec_mux_set() command,
> but it can be expanded to include other switches in future patches.
>
> No functional changes introduced by this patch.
>
> Signed-off-by: Prashant Malani <[email protected]>

Reviewed-by: Heikki Krogerus <[email protected]>

> ---
> drivers/usb/typec/bus.c | 11 +++++++++--
> 1 file changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/usb/typec/bus.c b/drivers/usb/typec/bus.c
> index 31c2a3130cad..9f1bbd26ca47 100644
> --- a/drivers/usb/typec/bus.c
> +++ b/drivers/usb/typec/bus.c
> @@ -27,6 +27,13 @@ typec_altmode_set_mux(struct altmode *alt, unsigned long conf, void *data)
> return typec_mux_set(alt->mux, &state);
> }
>
> +/* Wrapper to set various Type-C port switches together. */
> +static inline int
> +typec_altmode_set_switches(struct altmode *alt, unsigned long conf, void *data)
> +{
> + return typec_altmode_set_mux(alt, conf, data);
> +}
> +
> static int typec_altmode_set_state(struct typec_altmode *adev,
> unsigned long conf, void *data)
> {
> @@ -35,7 +42,7 @@ static int typec_altmode_set_state(struct typec_altmode *adev,
>
> port_altmode = is_port ? to_altmode(adev) : to_altmode(adev)->partner;
>
> - return typec_altmode_set_mux(port_altmode, conf, data);
> + return typec_altmode_set_switches(port_altmode, conf, data);
> }
>
> /* -------------------------------------------------------------------------- */
> @@ -73,7 +80,7 @@ int typec_altmode_notify(struct typec_altmode *adev,
> is_port = is_typec_port(adev->dev.parent);
> partner = altmode->partner;
>
> - ret = typec_altmode_set_mux(is_port ? altmode : partner, conf, data);
> + ret = typec_altmode_set_switches(is_port ? altmode : partner, conf, data);
> if (ret)
> return ret;
>
> --
> 2.39.0.314.g84b9a713c41-goog

--
heikki

2023-01-17 12:23:11

by Heikki Krogerus

[permalink] [raw]
Subject: Re: [PATCH 3/3] usb: typec: Make bus switch code retimer-aware

On Thu, Jan 12, 2023 at 10:16:08PM +0000, Prashant Malani wrote:
> Since ports can have retimers associated with them, update the Type-C
> alternate mode bus code to also set retimer state when the switch state
> is updated.
>
> While we are here, make the typec_retimer_dev_type declaration in the
> retimer.h file as extern, so that the header file can be successfully
> included in the bus code without redeclaration compilation errors.
>
> Signed-off-by: Prashant Malani <[email protected]>

Reviewed-by: Heikki Krogerus <[email protected]>

> ---
> drivers/usb/typec/bus.c | 22 ++++++++++++++++++++++
> drivers/usb/typec/retimer.h | 2 +-
> 2 files changed, 23 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/usb/typec/bus.c b/drivers/usb/typec/bus.c
> index 9f1bbd26ca47..0c8d554240be 100644
> --- a/drivers/usb/typec/bus.c
> +++ b/drivers/usb/typec/bus.c
> @@ -11,6 +11,22 @@
> #include "bus.h"
> #include "class.h"
> #include "mux.h"
> +#include "retimer.h"
> +
> +static inline int
> +typec_altmode_set_retimer(struct altmode *alt, unsigned long conf, void *data)
> +{
> + struct typec_retimer_state state;
> +
> + if (!alt->retimer)
> + return 0;
> +
> + state.alt = &alt->adev;
> + state.mode = conf;
> + state.data = data;
> +
> + return typec_retimer_set(alt->retimer, &state);
> +}
>
> static inline int
> typec_altmode_set_mux(struct altmode *alt, unsigned long conf, void *data)
> @@ -31,6 +47,12 @@ typec_altmode_set_mux(struct altmode *alt, unsigned long conf, void *data)
> static inline int
> typec_altmode_set_switches(struct altmode *alt, unsigned long conf, void *data)
> {
> + int ret;
> +
> + ret = typec_altmode_set_retimer(alt, conf, data);
> + if (ret)
> + return ret;
> +
> return typec_altmode_set_mux(alt, conf, data);
> }
>
> diff --git a/drivers/usb/typec/retimer.h b/drivers/usb/typec/retimer.h
> index e34bd23323be..d6a5ef9881e1 100644
> --- a/drivers/usb/typec/retimer.h
> +++ b/drivers/usb/typec/retimer.h
> @@ -12,7 +12,7 @@ struct typec_retimer {
>
> #define to_typec_retimer(_dev_) container_of(_dev_, struct typec_retimer, dev)
>
> -const struct device_type typec_retimer_dev_type;
> +extern const struct device_type typec_retimer_dev_type;
>
> #define is_typec_retimer(dev) ((dev)->type == &typec_retimer_dev_type)
>
> --
> 2.39.0.314.g84b9a713c41-goog

--
heikki