[PATCH v2] usb: typec: tcpm: detect orientation in debug acc mode

Michael Grzeschik posted 1 patch 7 months, 1 week ago
drivers/usb/typec/tcpm/tcpm.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
[PATCH v2] usb: typec: tcpm: detect orientation in debug acc mode
Posted by Michael Grzeschik 7 months, 1 week ago
For the debug accessory case, the orientation can be detected by reading
the cc resistor values. The will be TYPEC_CC_RP_DEF and TYPEC_CC_RP_1_5
in sink mode and TYPEC_CC_RA TYPEC_CC_RD in src mode.

Fixes: 64843d0ba96 ('usb: typec: tcpm: allow to use sink in accessory mode')
Signed-off-by: Michael Grzeschik <m.grzeschik@pengutronix.de>
---
Changes in v2:
- Added fixes tag as suggested by gregkh
- Link to v1: https://lore.kernel.org/r/20250505-tcpm-v1-1-e6142985a012@pengutronix.de
---
 drivers/usb/typec/tcpm/tcpm.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index 784fa23102f90..478e9c80fc8c2 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -611,6 +611,12 @@ static const char * const pd_rev[] = {
 #define tcpm_port_is_sink(port) \
 	(tcpm_cc_is_sink((port)->cc1) || tcpm_cc_is_sink((port)->cc2))
 
+#define tcpm_port_is_debug_pol_cc1(port) \
+	((tcpm_port_is_sink(port) && \
+	 (port->cc2 == TYPEC_CC_RP_DEF && port->cc1 == TYPEC_CC_RP_1_5)) || \
+	 (!tcpm_port_is_sink(port) && \
+	 (port->cc2 == TYPEC_CC_RA && port->cc1 == TYPEC_CC_RD)))
+
 #define tcpm_cc_is_source(cc) ((cc) == TYPEC_CC_RD)
 #define tcpm_cc_is_audio(cc) ((cc) == TYPEC_CC_RA)
 #define tcpm_cc_is_open(cc) ((cc) == TYPEC_CC_OPEN)
@@ -4569,8 +4575,11 @@ static int tcpm_acc_attach(struct tcpm_port *port)
 	if (tcpm_port_is_audio(port))
 		state = TYPEC_MODE_AUDIO;
 
-	if (tcpm_port_is_debug(port))
+	if (tcpm_port_is_debug(port)) {
+		port->polarity = tcpm_port_is_debug_pol_cc1(port) ?
+					TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
 		state = TYPEC_MODE_DEBUG;
+	}
 
 	ret = tcpm_set_roles(port, true, state, role, data);
 	if (ret < 0)

---
base-commit: 588d032e9e566997db3213dee145dbe3bda297b6
change-id: 20250505-tcpm-41b4ba7ea0ec

Best regards,
-- 
Michael Grzeschik <m.grzeschik@pengutronix.de>
Re: [PATCH v2] usb: typec: tcpm: detect orientation in debug acc mode
Posted by Kyle Tso 7 months, 1 week ago
On Tue, May 6, 2025 at 1:33 AM Michael Grzeschik
<m.grzeschik@pengutronix.de> wrote:
>
> For the debug accessory case, the orientation can be detected by reading
> the cc resistor values. The will be TYPEC_CC_RP_DEF and TYPEC_CC_RP_1_5
> in sink mode and TYPEC_CC_RA TYPEC_CC_RD in src mode.
>
> Fixes: 64843d0ba96 ('usb: typec: tcpm: allow to use sink in accessory mode')
> Signed-off-by: Michael Grzeschik <m.grzeschik@pengutronix.de>
> ---
> Changes in v2:
> - Added fixes tag as suggested by gregkh
> - Link to v1: https://lore.kernel.org/r/20250505-tcpm-v1-1-e6142985a012@pengutronix.de
> ---
>  drivers/usb/typec/tcpm/tcpm.c | 11 ++++++++++-
>  1 file changed, 10 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index 784fa23102f90..478e9c80fc8c2 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
> @@ -611,6 +611,12 @@ static const char * const pd_rev[] = {
>  #define tcpm_port_is_sink(port) \
>         (tcpm_cc_is_sink((port)->cc1) || tcpm_cc_is_sink((port)->cc2))
>
> +#define tcpm_port_is_debug_pol_cc1(port) \
> +       ((tcpm_port_is_sink(port) && \
> +        (port->cc2 == TYPEC_CC_RP_DEF && port->cc1 == TYPEC_CC_RP_1_5)) || \
> +        (!tcpm_port_is_sink(port) && \
> +        (port->cc2 == TYPEC_CC_RA && port->cc1 == TYPEC_CC_RD)))
> +

(oops I accidentally sent the mail in HTML format. Let me resend it in
Plain Text)
---

Thanks for the patch. I have a few comments.

My understanding was that TCPM in Debug Accessory Mode only supports
the port as a TS Sink. This patch seems to add orientation detection,
implying more than just a TS Sink. I thought TS Source/DRP and DTS
Sink/Source/DRP were not in the current Debug Accessory Mode
implementation in TCPM.

While full Debug Accessory Mode functionality would be great, perhaps
that should be in separate patches.

Also, for TS Sink orientation, please ensure it aligns with the Debug
Accessory Mode of Operation in the Type-C Spec R2.4 (Table B-2). It's
not solely based on Rp-def / Rp-1.5.

---
Kyle

>  #define tcpm_cc_is_source(cc) ((cc) == TYPEC_CC_RD)
>  #define tcpm_cc_is_audio(cc) ((cc) == TYPEC_CC_RA)
>  #define tcpm_cc_is_open(cc) ((cc) == TYPEC_CC_OPEN)
> @@ -4569,8 +4575,11 @@ static int tcpm_acc_attach(struct tcpm_port *port)
>         if (tcpm_port_is_audio(port))
>                 state = TYPEC_MODE_AUDIO;
>
> -       if (tcpm_port_is_debug(port))
> +       if (tcpm_port_is_debug(port)) {
> +               port->polarity = tcpm_port_is_debug_pol_cc1(port) ?
> +                                       TYPEC_POLARITY_CC1 : TYPEC_POLARITY_CC2;
>                 state = TYPEC_MODE_DEBUG;
> +       }
>
>         ret = tcpm_set_roles(port, true, state, role, data);
>         if (ret < 0)
>
> ---
> base-commit: 588d032e9e566997db3213dee145dbe3bda297b6
> change-id: 20250505-tcpm-41b4ba7ea0ec
>
> Best regards,
> --
> Michael Grzeschik <m.grzeschik@pengutronix.de>
>
>