Merge tag 'tag-chrome-platform-for-v5.11' of git://git.kernel.org/pub/scm/linux/kerne...
[sfrench/cifs-2.6.git] / drivers / platform / chrome / cros_ec_typec.c
index 8111ed1fc5743bb598a35170cf4caa310174dd63..c4386861579040a8ec624c0a4aca3d1c8667efea 100644 (file)
@@ -7,6 +7,7 @@
  */
 
 #include <linux/acpi.h>
+#include <linux/list.h>
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/platform_data/cros_ec_commands.h>
@@ -14,6 +15,7 @@
 #include <linux/platform_data/cros_usbpd_notify.h>
 #include <linux/platform_device.h>
 #include <linux/usb/pd.h>
+#include <linux/usb/pd_vdo.h>
 #include <linux/usb/typec.h>
 #include <linux/usb/typec_altmode.h>
 #include <linux/usb/typec_dp.h>
@@ -30,6 +32,12 @@ enum {
        CROS_EC_ALTMODE_MAX,
 };
 
+/* Container for altmode pointer nodes. */
+struct cros_typec_altmode_node {
+       struct typec_altmode *amode;
+       struct list_head list;
+};
+
 /* Per port data. */
 struct cros_typec_port {
        struct typec_port *port;
@@ -48,6 +56,11 @@ struct cros_typec_port {
 
        /* Port alt modes. */
        struct typec_altmode p_altmode[CROS_EC_ALTMODE_MAX];
+
+       /* Flag indicating that PD discovery data parsing is completed. */
+       bool disc_done;
+       struct ec_response_typec_discovery *sop_disc;
+       struct list_head partner_mode_list;
 };
 
 /* Platform-specific data for the Chrome OS EC Type C controller. */
@@ -60,6 +73,7 @@ struct cros_typec_data {
        struct cros_typec_port *ports[EC_USB_PD_MAX_PORTS];
        struct notifier_block nb;
        struct work_struct port_work;
+       bool typec_cmd_supported;
 };
 
 static int cros_typec_parse_port_props(struct typec_capability *cap,
@@ -166,11 +180,25 @@ static int cros_typec_add_partner(struct cros_typec_data *typec, int port_num,
        return ret;
 }
 
+static void cros_typec_unregister_altmodes(struct cros_typec_data *typec, int port_num)
+{
+       struct cros_typec_port *port = typec->ports[port_num];
+       struct cros_typec_altmode_node *node, *tmp;
+
+       list_for_each_entry_safe(node, tmp, &port->partner_mode_list, list) {
+               list_del(&node->list);
+               typec_unregister_altmode(node->amode);
+               devm_kfree(typec->dev, node);
+       }
+}
+
 static void cros_typec_remove_partner(struct cros_typec_data *typec,
                                     int port_num)
 {
        struct cros_typec_port *port = typec->ports[port_num];
 
+       cros_typec_unregister_altmodes(typec, port_num);
+
        port->state.alt = NULL;
        port->state.mode = TYPEC_STATE_USB;
        port->state.data = NULL;
@@ -181,6 +209,8 @@ static void cros_typec_remove_partner(struct cros_typec_data *typec,
 
        typec_unregister_partner(port->partner);
        port->partner = NULL;
+       memset(&port->p_identity, 0, sizeof(port->p_identity));
+       port->disc_done = false;
 }
 
 static void cros_unregister_ports(struct cros_typec_data *typec)
@@ -190,7 +220,10 @@ static void cros_unregister_ports(struct cros_typec_data *typec)
        for (i = 0; i < typec->num_ports; i++) {
                if (!typec->ports[i])
                        continue;
-               cros_typec_remove_partner(typec, i);
+
+               if (typec->ports[i]->partner)
+                       cros_typec_remove_partner(typec, i);
+
                usb_role_switch_put(typec->ports[i]->role_sw);
                typec_switch_put(typec->ports[i]->ori_sw);
                typec_mux_put(typec->ports[i]->mux);
@@ -289,6 +322,14 @@ static int cros_typec_init_ports(struct cros_typec_data *typec)
                                port_num);
 
                cros_typec_register_port_altmodes(typec, port_num);
+
+               cros_port->sop_disc = devm_kzalloc(dev, EC_PROTO2_MAX_RESPONSE_SIZE, GFP_KERNEL);
+               if (!cros_port->sop_disc) {
+                       ret = -ENOMEM;
+                       goto unregister_ports;
+               }
+
+               INIT_LIST_HEAD(&cros_port->partner_mode_list);
        }
 
        return 0;
@@ -329,74 +370,6 @@ static int cros_typec_ec_command(struct cros_typec_data *typec,
        return ret;
 }
 
-static void cros_typec_set_port_params_v0(struct cros_typec_data *typec,
-               int port_num, struct ec_response_usb_pd_control *resp)
-{
-       struct typec_port *port = typec->ports[port_num]->port;
-       enum typec_orientation polarity;
-
-       if (!resp->enabled)
-               polarity = TYPEC_ORIENTATION_NONE;
-       else if (!resp->polarity)
-               polarity = TYPEC_ORIENTATION_NORMAL;
-       else
-               polarity = TYPEC_ORIENTATION_REVERSE;
-
-       typec_set_pwr_role(port, resp->role ? TYPEC_SOURCE : TYPEC_SINK);
-       typec_set_orientation(port, polarity);
-}
-
-static void cros_typec_set_port_params_v1(struct cros_typec_data *typec,
-               int port_num, struct ec_response_usb_pd_control_v1 *resp)
-{
-       struct typec_port *port = typec->ports[port_num]->port;
-       enum typec_orientation polarity;
-       bool pd_en;
-       int ret;
-
-       if (!(resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED))
-               polarity = TYPEC_ORIENTATION_NONE;
-       else if (!resp->polarity)
-               polarity = TYPEC_ORIENTATION_NORMAL;
-       else
-               polarity = TYPEC_ORIENTATION_REVERSE;
-       typec_set_orientation(port, polarity);
-       typec_set_data_role(port, resp->role & PD_CTRL_RESP_ROLE_DATA ?
-                       TYPEC_HOST : TYPEC_DEVICE);
-       typec_set_pwr_role(port, resp->role & PD_CTRL_RESP_ROLE_POWER ?
-                       TYPEC_SOURCE : TYPEC_SINK);
-       typec_set_vconn_role(port, resp->role & PD_CTRL_RESP_ROLE_VCONN ?
-                       TYPEC_SOURCE : TYPEC_SINK);
-
-       /* Register/remove partners when a connect/disconnect occurs. */
-       if (resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED) {
-               if (typec->ports[port_num]->partner)
-                       return;
-
-               pd_en = resp->enabled & PD_CTRL_RESP_ENABLED_PD_CAPABLE;
-               ret = cros_typec_add_partner(typec, port_num, pd_en);
-               if (ret)
-                       dev_warn(typec->dev,
-                                "Failed to register partner on port: %d\n",
-                                port_num);
-       } else {
-               if (!typec->ports[port_num]->partner)
-                       return;
-               cros_typec_remove_partner(typec, port_num);
-       }
-}
-
-static int cros_typec_get_mux_info(struct cros_typec_data *typec, int port_num,
-                                  struct ec_response_usb_pd_mux_info *resp)
-{
-       struct ec_params_usb_pd_mux_info req = {
-               .port = port_num,
-       };
-
-       return cros_typec_ec_command(typec, 0, EC_CMD_USB_PD_MUX_INFO, &req,
-                                    sizeof(req), resp, sizeof(*resp));
-}
-
 static int cros_typec_usb_safe_state(struct cros_typec_port *port)
 {
        port->state.mode = TYPEC_STATE_SAFE;
@@ -563,15 +536,210 @@ static int cros_typec_configure_mux(struct cros_typec_data *typec, int port_num,
                port->state.mode = TYPEC_STATE_USB;
                ret = typec_mux_set(port->mux, &port->state);
        } else {
-               dev_info(typec->dev,
-                        "Unsupported mode requested, mux flags: %x\n",
-                        mux_flags);
-               ret = -ENOTSUPP;
+               dev_dbg(typec->dev,
+                       "Unrecognized mode requested, mux flags: %x\n",
+                       mux_flags);
+       }
+
+       return ret;
+}
+
+static void cros_typec_set_port_params_v0(struct cros_typec_data *typec,
+               int port_num, struct ec_response_usb_pd_control *resp)
+{
+       struct typec_port *port = typec->ports[port_num]->port;
+       enum typec_orientation polarity;
+
+       if (!resp->enabled)
+               polarity = TYPEC_ORIENTATION_NONE;
+       else if (!resp->polarity)
+               polarity = TYPEC_ORIENTATION_NORMAL;
+       else
+               polarity = TYPEC_ORIENTATION_REVERSE;
+
+       typec_set_pwr_role(port, resp->role ? TYPEC_SOURCE : TYPEC_SINK);
+       typec_set_orientation(port, polarity);
+}
+
+static void cros_typec_set_port_params_v1(struct cros_typec_data *typec,
+               int port_num, struct ec_response_usb_pd_control_v1 *resp)
+{
+       struct typec_port *port = typec->ports[port_num]->port;
+       enum typec_orientation polarity;
+       bool pd_en;
+       int ret;
+
+       if (!(resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED))
+               polarity = TYPEC_ORIENTATION_NONE;
+       else if (!resp->polarity)
+               polarity = TYPEC_ORIENTATION_NORMAL;
+       else
+               polarity = TYPEC_ORIENTATION_REVERSE;
+       typec_set_orientation(port, polarity);
+       typec_set_data_role(port, resp->role & PD_CTRL_RESP_ROLE_DATA ?
+                       TYPEC_HOST : TYPEC_DEVICE);
+       typec_set_pwr_role(port, resp->role & PD_CTRL_RESP_ROLE_POWER ?
+                       TYPEC_SOURCE : TYPEC_SINK);
+       typec_set_vconn_role(port, resp->role & PD_CTRL_RESP_ROLE_VCONN ?
+                       TYPEC_SOURCE : TYPEC_SINK);
+
+       /* Register/remove partners when a connect/disconnect occurs. */
+       if (resp->enabled & PD_CTRL_RESP_ENABLED_CONNECTED) {
+               if (typec->ports[port_num]->partner)
+                       return;
+
+               pd_en = resp->enabled & PD_CTRL_RESP_ENABLED_PD_CAPABLE;
+               ret = cros_typec_add_partner(typec, port_num, pd_en);
+               if (ret)
+                       dev_warn(typec->dev,
+                                "Failed to register partner on port: %d\n",
+                                port_num);
+       } else {
+               if (!typec->ports[port_num]->partner)
+                       return;
+               cros_typec_remove_partner(typec, port_num);
        }
+}
+
+static int cros_typec_get_mux_info(struct cros_typec_data *typec, int port_num,
+                                  struct ec_response_usb_pd_mux_info *resp)
+{
+       struct ec_params_usb_pd_mux_info req = {
+               .port = port_num,
+       };
+
+       return cros_typec_ec_command(typec, 0, EC_CMD_USB_PD_MUX_INFO, &req,
+                                    sizeof(req), resp, sizeof(*resp));
+}
+
+static int cros_typec_register_altmodes(struct cros_typec_data *typec, int port_num)
+{
+       struct cros_typec_port *port = typec->ports[port_num];
+       struct ec_response_typec_discovery *sop_disc = port->sop_disc;
+       struct cros_typec_altmode_node *node;
+       struct typec_altmode_desc desc;
+       struct typec_altmode *amode;
+       int ret = 0;
+       int i, j;
+
+       for (i = 0; i < sop_disc->svid_count; i++) {
+               for (j = 0; j < sop_disc->svids[i].mode_count; j++) {
+                       memset(&desc, 0, sizeof(desc));
+                       desc.svid = sop_disc->svids[i].svid;
+                       desc.mode = j;
+                       desc.vdo = sop_disc->svids[i].mode_vdo[j];
+
+                       amode = typec_partner_register_altmode(port->partner, &desc);
+                       if (IS_ERR(amode)) {
+                               ret = PTR_ERR(amode);
+                               goto err_cleanup;
+                       }
+
+                       /* If no memory is available we should unregister and exit. */
+                       node = devm_kzalloc(typec->dev, sizeof(*node), GFP_KERNEL);
+                       if (!node) {
+                               ret = -ENOMEM;
+                               typec_unregister_altmode(amode);
+                               goto err_cleanup;
+                       }
+
+                       node->amode = amode;
+                       list_add_tail(&node->list, &port->partner_mode_list);
+               }
+       }
+
+       return 0;
 
+err_cleanup:
+       cros_typec_unregister_altmodes(typec, port_num);
        return ret;
 }
 
+static int cros_typec_handle_sop_disc(struct cros_typec_data *typec, int port_num)
+{
+       struct cros_typec_port *port = typec->ports[port_num];
+       struct ec_response_typec_discovery *sop_disc = port->sop_disc;
+       struct ec_params_typec_discovery req = {
+               .port = port_num,
+               .partner_type = TYPEC_PARTNER_SOP,
+       };
+       int ret = 0;
+       int i;
+
+       if (!port->partner) {
+               dev_err(typec->dev,
+                       "SOP Discovery received without partner registered, port: %d\n",
+                       port_num);
+               ret = -EINVAL;
+               goto disc_exit;
+       }
+
+       memset(sop_disc, 0, EC_PROTO2_MAX_RESPONSE_SIZE);
+       ret = cros_typec_ec_command(typec, 0, EC_CMD_TYPEC_DISCOVERY, &req, sizeof(req),
+                                   sop_disc, EC_PROTO2_MAX_RESPONSE_SIZE);
+       if (ret < 0) {
+               dev_err(typec->dev, "Failed to get SOP discovery data for port: %d\n", port_num);
+               goto disc_exit;
+       }
+
+       /* First, update the PD identity VDOs for the partner. */
+       if (sop_disc->identity_count > 0)
+               port->p_identity.id_header = sop_disc->discovery_vdo[0];
+       if (sop_disc->identity_count > 1)
+               port->p_identity.cert_stat = sop_disc->discovery_vdo[1];
+       if (sop_disc->identity_count > 2)
+               port->p_identity.product = sop_disc->discovery_vdo[2];
+
+       /* Copy the remaining identity VDOs till a maximum of 6. */
+       for (i = 3; i < sop_disc->identity_count && i < VDO_MAX_OBJECTS; i++)
+               port->p_identity.vdo[i - 3] = sop_disc->discovery_vdo[i];
+
+       ret = typec_partner_set_identity(port->partner);
+       if (ret < 0) {
+               dev_err(typec->dev, "Failed to update partner PD identity, port: %d\n", port_num);
+               goto disc_exit;
+       }
+
+       ret = cros_typec_register_altmodes(typec, port_num);
+       if (ret < 0) {
+               dev_err(typec->dev, "Failed to register partner altmodes, port: %d\n", port_num);
+               goto disc_exit;
+       }
+
+disc_exit:
+       return ret;
+}
+
+static void cros_typec_handle_status(struct cros_typec_data *typec, int port_num)
+{
+       struct ec_response_typec_status resp;
+       struct ec_params_typec_status req = {
+               .port = port_num,
+       };
+       int ret;
+
+       ret = cros_typec_ec_command(typec, 0, EC_CMD_TYPEC_STATUS, &req, sizeof(req),
+                                   &resp, sizeof(resp));
+       if (ret < 0) {
+               dev_warn(typec->dev, "EC_CMD_TYPEC_STATUS failed for port: %d\n", port_num);
+               return;
+       }
+
+       if (typec->ports[port_num]->disc_done)
+               return;
+
+       /* Handle any events appropriately. */
+       if (resp.events & PD_STATUS_EVENT_SOP_DISC_DONE) {
+               ret = cros_typec_handle_sop_disc(typec, port_num);
+               if (ret < 0) {
+                       dev_err(typec->dev, "Couldn't parse SOP Disc data, port: %d\n", port_num);
+                       return;
+               }
+
+               typec->ports[port_num]->disc_done = true;
+       }
+}
+
 static int cros_typec_port_update(struct cros_typec_data *typec, int port_num)
 {
        struct ec_params_usb_pd_control req;
@@ -608,6 +776,9 @@ static int cros_typec_port_update(struct cros_typec_data *typec, int port_num)
                cros_typec_set_port_params_v0(typec, port_num,
                        (struct ec_response_usb_pd_control *) &resp);
 
+       if (typec->typec_cmd_supported)
+               cros_typec_handle_status(typec, port_num);
+
        /* Update the switches if they exist, according to requested state */
        ret = cros_typec_get_mux_info(typec, port_num, &mux_resp);
        if (ret < 0) {
@@ -656,6 +827,23 @@ static int cros_typec_get_cmd_version(struct cros_typec_data *typec)
        return 0;
 }
 
+/* Check the EC feature flags to see if TYPEC_* commands are supported. */
+static int cros_typec_cmds_supported(struct cros_typec_data *typec)
+{
+       struct ec_response_get_features resp = {};
+       int ret;
+
+       ret = cros_typec_ec_command(typec, 0, EC_CMD_GET_FEATURES, NULL, 0,
+                                   &resp, sizeof(resp));
+       if (ret < 0) {
+               dev_warn(typec->dev,
+                        "Failed to get features, assuming typec commands unsupported.\n");
+               return 0;
+       }
+
+       return resp.flags[EC_FEATURE_TYPEC_CMD / 32] & EC_FEATURE_MASK_1(EC_FEATURE_TYPEC_CMD);
+}
+
 static void cros_typec_port_work(struct work_struct *work)
 {
        struct cros_typec_data *typec = container_of(work, struct cros_typec_data, port_work);
@@ -715,6 +903,8 @@ static int cros_typec_probe(struct platform_device *pdev)
                return ret;
        }
 
+       typec->typec_cmd_supported = !!cros_typec_cmds_supported(typec);
+
        ret = cros_typec_ec_command(typec, 0, EC_CMD_USB_PD_PORTS, NULL, 0,
                                    &resp, sizeof(resp));
        if (ret < 0)