[09/10] platform/chrome: cros_typec_vdm: Add VDM reply support

Message ID 20221228004648.793339-10-pmalani@chromium.org
State New
Headers
Series platform/chrome: cros_ec_typec: VDM support |

Commit Message

Prashant Malani Dec. 28, 2022, 12:45 a.m. UTC
  Handle response VDMs which are sent by the partner (replying to VDMs
sent by the host system itself). These get forwarded to the altmode
driver.

Cc: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Prashant Malani <pmalani@chromium.org>
---
 drivers/platform/chrome/cros_ec_typec.c  |  7 +++++
 drivers/platform/chrome/cros_typec_vdm.c | 39 ++++++++++++++++++++++++
 drivers/platform/chrome/cros_typec_vdm.h |  2 ++
 3 files changed, 48 insertions(+)
  

Comments

Benson Leung Jan. 9, 2023, 7:50 p.m. UTC | #1
On Wed, Dec 28, 2022 at 12:45:12AM +0000, Prashant Malani wrote:
> Handle response VDMs which are sent by the partner (replying to VDMs
> sent by the host system itself). These get forwarded to the altmode
> driver.
> 
> Cc: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> Signed-off-by: Prashant Malani <pmalani@chromium.org>

Reviewed-by: Benson Leung <bleung@chromium.org>

> ---
>  drivers/platform/chrome/cros_ec_typec.c  |  7 +++++
>  drivers/platform/chrome/cros_typec_vdm.c | 39 ++++++++++++++++++++++++
>  drivers/platform/chrome/cros_typec_vdm.h |  2 ++
>  3 files changed, 48 insertions(+)
> 
> diff --git a/drivers/platform/chrome/cros_ec_typec.c b/drivers/platform/chrome/cros_ec_typec.c
> index 1e28d56b094d..e02107a6870a 100644
> --- a/drivers/platform/chrome/cros_ec_typec.c
> +++ b/drivers/platform/chrome/cros_ec_typec.c
> @@ -1000,6 +1000,13 @@ static void cros_typec_handle_status(struct cros_typec_data *typec, int port_num
>  					 "Failed SOP Disc event clear, port: %d\n", port_num);
>  		}
>  	}
> +
> +	if (resp.events & PD_STATUS_EVENT_VDM_REQ_REPLY) {
> +		cros_typec_handle_vdm_response(typec, port_num);
> +		ret = cros_typec_send_clear_event(typec, port_num, PD_STATUS_EVENT_VDM_REQ_REPLY);
> +		if (ret < 0)
> +			dev_warn(typec->dev, "Failed VDM Reply event clear, port: %d\n", port_num);
> +	}
>  }
>  
>  static int cros_typec_port_update(struct cros_typec_data *typec, int port_num)
> diff --git a/drivers/platform/chrome/cros_typec_vdm.c b/drivers/platform/chrome/cros_typec_vdm.c
> index df0102ca3a18..fc7b602ceb37 100644
> --- a/drivers/platform/chrome/cros_typec_vdm.c
> +++ b/drivers/platform/chrome/cros_typec_vdm.c
> @@ -13,6 +13,45 @@
>  #include "cros_ec_typec.h"
>  #include "cros_typec_vdm.h"
>  
> +/*
> + * Retrieves a VDM response from the EC and forwards it to the altmode driver based on SVID.
> + */
> +void cros_typec_handle_vdm_response(struct cros_typec_data *typec, int port_num)
> +{
> +	struct ec_response_typec_vdm_response resp;
> +	struct ec_params_typec_vdm_response req = {
> +		.port = port_num,
> +	};
> +	struct typec_altmode *amode;
> +	u16 svid;
> +	u32 hdr;
> +	int ret;
> +
> +	ret = cros_ec_cmd(typec->ec, 0, EC_CMD_TYPEC_VDM_RESPONSE, &req,
> +			  sizeof(req), &resp, sizeof(resp));
> +	if (ret < 0) {
> +		dev_warn(typec->dev, "Failed VDM response fetch, port: %d\n", port_num);
> +		return;
> +	}
> +
> +	hdr = resp.vdm_response[0];
> +	svid = PD_VDO_VID(hdr);
> +	dev_dbg(typec->dev, "Received VDM header: %x, port: %d\n", hdr, port_num);
> +
> +	amode = typec_match_altmode(typec->ports[port_num]->port_altmode, CROS_EC_ALTMODE_MAX,
> +				    svid, PD_VDO_OPOS(hdr));
> +	if (!amode) {
> +		dev_err(typec->dev, "Received VDM for unregistered altmode (SVID:%x), port: %d\n",
> +			svid, port_num);
> +		return;
> +	}
> +
> +	ret = typec_altmode_vdm(amode, hdr, &resp.vdm_response[1], resp.vdm_data_objects);
> +	if (ret)
> +		dev_err(typec->dev, "Failed to forward VDM to altmode (SVID:%x), port: %d\n",
> +			svid, port_num);
> +}
> +
>  static int cros_typec_port_amode_enter(struct typec_altmode *amode, u32 *vdo)
>  {
>  	struct cros_typec_port *port = typec_altmode_get_drvdata(amode);
> diff --git a/drivers/platform/chrome/cros_typec_vdm.h b/drivers/platform/chrome/cros_typec_vdm.h
> index 7e282d168a98..003587525554 100644
> --- a/drivers/platform/chrome/cros_typec_vdm.h
> +++ b/drivers/platform/chrome/cros_typec_vdm.h
> @@ -7,4 +7,6 @@
>  
>  extern struct typec_altmode_ops port_amode_ops;
>  
> +void cros_typec_handle_vdm_response(struct cros_typec_data *typec, int port_num);
> +
>  #endif /*  __CROS_TYPEC_VDM__ */
> -- 
> 2.39.0.314.g84b9a713c41-goog
> 
>
  

Patch

diff --git a/drivers/platform/chrome/cros_ec_typec.c b/drivers/platform/chrome/cros_ec_typec.c
index 1e28d56b094d..e02107a6870a 100644
--- a/drivers/platform/chrome/cros_ec_typec.c
+++ b/drivers/platform/chrome/cros_ec_typec.c
@@ -1000,6 +1000,13 @@  static void cros_typec_handle_status(struct cros_typec_data *typec, int port_num
 					 "Failed SOP Disc event clear, port: %d\n", port_num);
 		}
 	}
+
+	if (resp.events & PD_STATUS_EVENT_VDM_REQ_REPLY) {
+		cros_typec_handle_vdm_response(typec, port_num);
+		ret = cros_typec_send_clear_event(typec, port_num, PD_STATUS_EVENT_VDM_REQ_REPLY);
+		if (ret < 0)
+			dev_warn(typec->dev, "Failed VDM Reply event clear, port: %d\n", port_num);
+	}
 }
 
 static int cros_typec_port_update(struct cros_typec_data *typec, int port_num)
diff --git a/drivers/platform/chrome/cros_typec_vdm.c b/drivers/platform/chrome/cros_typec_vdm.c
index df0102ca3a18..fc7b602ceb37 100644
--- a/drivers/platform/chrome/cros_typec_vdm.c
+++ b/drivers/platform/chrome/cros_typec_vdm.c
@@ -13,6 +13,45 @@ 
 #include "cros_ec_typec.h"
 #include "cros_typec_vdm.h"
 
+/*
+ * Retrieves a VDM response from the EC and forwards it to the altmode driver based on SVID.
+ */
+void cros_typec_handle_vdm_response(struct cros_typec_data *typec, int port_num)
+{
+	struct ec_response_typec_vdm_response resp;
+	struct ec_params_typec_vdm_response req = {
+		.port = port_num,
+	};
+	struct typec_altmode *amode;
+	u16 svid;
+	u32 hdr;
+	int ret;
+
+	ret = cros_ec_cmd(typec->ec, 0, EC_CMD_TYPEC_VDM_RESPONSE, &req,
+			  sizeof(req), &resp, sizeof(resp));
+	if (ret < 0) {
+		dev_warn(typec->dev, "Failed VDM response fetch, port: %d\n", port_num);
+		return;
+	}
+
+	hdr = resp.vdm_response[0];
+	svid = PD_VDO_VID(hdr);
+	dev_dbg(typec->dev, "Received VDM header: %x, port: %d\n", hdr, port_num);
+
+	amode = typec_match_altmode(typec->ports[port_num]->port_altmode, CROS_EC_ALTMODE_MAX,
+				    svid, PD_VDO_OPOS(hdr));
+	if (!amode) {
+		dev_err(typec->dev, "Received VDM for unregistered altmode (SVID:%x), port: %d\n",
+			svid, port_num);
+		return;
+	}
+
+	ret = typec_altmode_vdm(amode, hdr, &resp.vdm_response[1], resp.vdm_data_objects);
+	if (ret)
+		dev_err(typec->dev, "Failed to forward VDM to altmode (SVID:%x), port: %d\n",
+			svid, port_num);
+}
+
 static int cros_typec_port_amode_enter(struct typec_altmode *amode, u32 *vdo)
 {
 	struct cros_typec_port *port = typec_altmode_get_drvdata(amode);
diff --git a/drivers/platform/chrome/cros_typec_vdm.h b/drivers/platform/chrome/cros_typec_vdm.h
index 7e282d168a98..003587525554 100644
--- a/drivers/platform/chrome/cros_typec_vdm.h
+++ b/drivers/platform/chrome/cros_typec_vdm.h
@@ -7,4 +7,6 @@ 
 
 extern struct typec_altmode_ops port_amode_ops;
 
+void cros_typec_handle_vdm_response(struct cros_typec_data *typec, int port_num);
+
 #endif /*  __CROS_TYPEC_VDM__ */