From: Heikki Krogerus <heikki.krogerus@linux.intel.com>
To: Kyle Tso <kyletso@google.com>
Cc: linux@roeck-us.net, gregkh@linuxfoundation.org,
badhri@google.com, linux-usb@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] usb: typec: tcpm: Create legacy PDOs for PD2 connection
Date: Thu, 21 Jan 2021 10:41:01 +0200 [thread overview]
Message-ID: <20210121084101.GC423216@kuha.fi.intel.com> (raw)
In-Reply-To: <20210115163311.391332-1-kyletso@google.com>
Hi Kyle,
On Sat, Jan 16, 2021 at 12:33:11AM +0800, Kyle Tso wrote:
> If the port partner is PD2, the PDOs of the local port should follow the
> format defined in PD2 Spec. Dynamically modify the pre-defined PD3 PDOs
> and transform them into PD2 format before sending them to the PD2 port
> partner.
I guess it's not possible for the system to supply separate static
PDOs for each PD revision?
> Signed-off-by: Kyle Tso <kyletso@google.com>
> ---
> drivers/usb/typec/tcpm/tcpm.c | 62 +++++++++++++++++++++++++++++------
> include/linux/usb/pd.h | 1 +
> 2 files changed, 53 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index 22a85b396f69..1220ab1ed47d 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
> @@ -911,13 +911,47 @@ static int tcpm_set_pwr_role(struct tcpm_port *port, enum typec_role role)
> return 0;
> }
>
> +/*
> + * Transform the PDO to be compliant to PD rev2.0.
> + * Return 0 if the PDO type is not defined in PD rev2.0.
> + * Otherwise, return the converted PDO.
> + */
> +static u32 tcpm_forge_legacy_pdo(struct tcpm_port *port, u32 pdo, enum typec_role role)
> +{
> + switch (pdo_type(pdo)) {
> + case PDO_TYPE_FIXED:
> + if (role == TYPEC_SINK)
> + return pdo & ~PDO_FIXED_FRS_CURR_MASK;
> + else
> + return pdo & ~PDO_FIXED_UNCHUNK_EXT;
> + case PDO_TYPE_VAR:
> + case PDO_TYPE_BATT:
> + return pdo;
> + case PDO_TYPE_APDO:
> + default:
> + return 0;
> + }
> +}
> +
> static int tcpm_pd_send_source_caps(struct tcpm_port *port)
> {
> struct pd_message msg;
> - int i;
> + u32 pdo;
> + unsigned int i, nr_pdo = 0;
Side note. I think this driver uses the "reverse christmas tree"
style with the variables.
> memset(&msg, 0, sizeof(msg));
> - if (!port->nr_src_pdo) {
> +
> + for (i = 0; i < port->nr_src_pdo; i++) {
> + if (port->negotiated_rev >= PD_REV30) {
> + msg.payload[nr_pdo++] = cpu_to_le32(port->src_pdo[i]);
> + } else {
> + pdo = tcpm_forge_legacy_pdo(port, port->src_pdo[i], TYPEC_SOURCE);
> + if (pdo)
> + msg.payload[nr_pdo++] = cpu_to_le32(pdo);
> + }
> + }
Why not just generate the PD_REV20 PDOs during probe time and then
just use the ones that fit the negotiated_rev here?
> +
> + if (!nr_pdo) {
> /* No source capabilities defined, sink only */
> msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
> port->pwr_role,
> @@ -930,10 +964,8 @@ static int tcpm_pd_send_source_caps(struct tcpm_port *port)
> port->data_role,
> port->negotiated_rev,
> port->message_id,
> - port->nr_src_pdo);
> + nr_pdo);
> }
> - for (i = 0; i < port->nr_src_pdo; i++)
> - msg.payload[i] = cpu_to_le32(port->src_pdo[i]);
>
> return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
> }
> @@ -941,10 +973,22 @@ static int tcpm_pd_send_source_caps(struct tcpm_port *port)
> static int tcpm_pd_send_sink_caps(struct tcpm_port *port)
> {
> struct pd_message msg;
> - int i;
> + u32 pdo;
> + unsigned int i, nr_pdo = 0;
>
> memset(&msg, 0, sizeof(msg));
> - if (!port->nr_snk_pdo) {
> +
> + for (i = 0; i < port->nr_snk_pdo; i++) {
> + if (port->negotiated_rev >= PD_REV30) {
> + msg.payload[nr_pdo++] = cpu_to_le32(port->snk_pdo[i]);
> + } else {
> + pdo = tcpm_forge_legacy_pdo(port, port->snk_pdo[i], TYPEC_SINK);
> + if (pdo)
> + msg.payload[nr_pdo++] = cpu_to_le32(pdo);
> + }
> + }
ditto.
> + if (!nr_pdo) {
> /* No sink capabilities defined, source only */
> msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
> port->pwr_role,
> @@ -957,10 +1001,8 @@ static int tcpm_pd_send_sink_caps(struct tcpm_port *port)
> port->data_role,
> port->negotiated_rev,
> port->message_id,
> - port->nr_snk_pdo);
> + nr_pdo);
> }
> - for (i = 0; i < port->nr_snk_pdo; i++)
> - msg.payload[i] = cpu_to_le32(port->snk_pdo[i]);
>
> return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
> }
> diff --git a/include/linux/usb/pd.h b/include/linux/usb/pd.h
> index bb9a782e1411..88f64bce5dea 100644
> --- a/include/linux/usb/pd.h
> +++ b/include/linux/usb/pd.h
> @@ -225,6 +225,7 @@ enum pd_pdo_type {
> #define PDO_FIXED_EXTPOWER BIT(27) /* Externally powered */
> #define PDO_FIXED_USB_COMM BIT(26) /* USB communications capable */
> #define PDO_FIXED_DATA_SWAP BIT(25) /* Data role swap supported */
> +#define PDO_FIXED_UNCHUNK_EXT BIT(24) /* Unchunked Extended Message supported (Source) */
> #define PDO_FIXED_FRS_CURR_MASK (BIT(24) | BIT(23)) /* FR_Swap Current (Sink) */
> #define PDO_FIXED_FRS_CURR_SHIFT 23
> #define PDO_FIXED_VOLT_SHIFT 10 /* 50mV units */
thanks,
--
heikki
next prev parent reply other threads:[~2021-01-21 8:46 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-15 16:33 [PATCH] usb: typec: tcpm: Create legacy PDOs for PD2 connection Kyle Tso
2021-01-21 8:41 ` Heikki Krogerus [this message]
2021-01-21 9:48 ` Kyle Tso
2021-01-25 9:21 ` Heikki Krogerus
2021-01-23 20:01 ` Guenter Roeck
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210121084101.GC423216@kuha.fi.intel.com \
--to=heikki.krogerus@linux.intel.com \
--cc=badhri@google.com \
--cc=gregkh@linuxfoundation.org \
--cc=kyletso@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=linux@roeck-us.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox