From patchwork Thu Jul 7 22:20:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prashant Malani X-Patchwork-Id: 588487 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9082AC433EF for ; Thu, 7 Jul 2022 22:24:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236950AbiGGWYn (ORCPT ); Thu, 7 Jul 2022 18:24:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236930AbiGGWYm (ORCPT ); Thu, 7 Jul 2022 18:24:42 -0400 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1F3461D6E for ; Thu, 7 Jul 2022 15:24:41 -0700 (PDT) Received: by mail-pj1-x102b.google.com with SMTP id 89-20020a17090a09e200b001ef7638e536so155378pjo.3 for ; Thu, 07 Jul 2022 15:24:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=N9vAkojdh6qMe19EHyx5mVtuXIIPXXQjqcAFQnpD2/w=; b=adbszkRTqfHpbUOBrGo4yWWxnXbH3v4a8KsPa0jahMhmw3oO/dFNATvjEbVCEzUOwb PvD8tUn7JS3pKgM/BJUdlbjePyqqtJNTlc/CLuCw+fZH62R0poN3YZH2H+jSqpC74EYp aYaACHYagEpGgUwpLFGyDi4BufQ33mU4jXaUM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=N9vAkojdh6qMe19EHyx5mVtuXIIPXXQjqcAFQnpD2/w=; b=b13vUkkRuAjTaIfCrI1tYqtM3qWZ3VgiFzVFznecUxL2Wc4Mpd3oSlSlcvqb5WGQC+ CCvbT9uu58d01tDRIxC8MHZEbSbIzSix8UPCjFF0qgy/FYmm4wFLxARAjM8lJhe0l+R6 N3ZqDHu68T3wYPBCOd65AsPI12+j+ZJEU5DpuimG9SBs+jvT/wCI3y4oLzkbm6gOAMuO TofW8oRpkhgV617R7ohirsKivTAXdgCoTaTRSsL+leOfDZBqteA82wPPOoIUX7XoQFtu LyLFXntVG7WTP2nR8y+bxhpK4u36CCWjJGyXIMHW+0K1BEVbMxwMyEktT41FojjrmJM+ 2YRg== X-Gm-Message-State: AJIora+AVzXD3cOfS28GlvWR/3Z5uJo4+yzZk0+7nx2DSjgrSL9+Rahn t5WYwGJtP0GIK+aUyXa586AAcA== X-Google-Smtp-Source: AGRyM1sEExb5BbYrXctyg52GLzsAYr1U0f+5+G8TK57wfvKazzq5tMyp0zqOlqS7G6sv19pDrBvvVQ== X-Received: by 2002:a17:90b:2496:b0:1ef:a94:7048 with SMTP id nt22-20020a17090b249600b001ef0a947048mr114546pjb.244.1657232681298; Thu, 07 Jul 2022 15:24:41 -0700 (PDT) Received: from pmalani.c.googlers.com.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id 66-20020a620445000000b005289bfcee91sm5545657pfe.59.2022.07.07.15.24.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jul 2022 15:24:40 -0700 (PDT) From: Prashant Malani To: linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, chrome-platform@lists.linux.dev Cc: bleung@chromium.org, heikki.krogerus@linux.intel.com, Prashant Malani , Daisuke Nojiri , "Dustin L. Howett" , Greg Kroah-Hartman , Guenter Roeck , "Gustavo A. R. Silva" , Sebastian Reichel , Tzung-Bi Shih Subject: [PATCH v3 5/9] platform/chrome: cros_typec_switch: Set EC retimer Date: Thu, 7 Jul 2022 22:20:12 +0000 Message-Id: <20220707222045.1415417-6-pmalani@chromium.org> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog In-Reply-To: <20220707222045.1415417-1-pmalani@chromium.org> References: <20220707222045.1415417-1-pmalani@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org Invoke Chrome EC host commands to set EC-controlled retimer switches to the state the Type-C framework instructs. Signed-off-by: Prashant Malani --- Changes since v2: - No changes. Changes since v1: - No changes. drivers/platform/chrome/cros_typec_switch.c | 56 ++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/drivers/platform/chrome/cros_typec_switch.c b/drivers/platform/chrome/cros_typec_switch.c index 0d319e315d57..b50ecedce662 100644 --- a/drivers/platform/chrome/cros_typec_switch.c +++ b/drivers/platform/chrome/cros_typec_switch.c @@ -9,7 +9,10 @@ #include #include #include +#include #include +#include +#include #include #define DRV_NAME "cros-typec-switch" @@ -28,9 +31,60 @@ struct cros_typec_switch_data { struct cros_typec_port *ports[EC_USB_PD_MAX_PORTS]; }; +static int cros_typec_cmd_mux_set(struct cros_typec_switch_data *sdata, int port_num, u8 index, + u8 state) +{ + struct typec_usb_mux_set params = { + .mux_index = index, + .mux_flags = state, + }; + + struct ec_params_typec_control req = { + .port = port_num, + .command = TYPEC_CONTROL_COMMAND_USB_MUX_SET, + .mux_params = params, + }; + + return cros_ec_command(sdata->ec, 0, EC_CMD_TYPEC_CONTROL, &req, + sizeof(req), NULL, 0); +} + +static int cros_typec_get_mux_state(unsigned long mode, struct typec_altmode *alt) +{ + int ret = -EOPNOTSUPP; + + if (mode == TYPEC_STATE_SAFE) + ret = USB_PD_MUX_SAFE_MODE; + else if (mode == TYPEC_STATE_USB) + ret = USB_PD_MUX_USB_ENABLED; + else if (alt && alt->svid == USB_TYPEC_DP_SID) + ret = USB_PD_MUX_DP_ENABLED; + + return ret; +} + +/* + * The Chrome EC treats both mode-switches and retimers as "muxes" for the purposes of the + * host command API. This common function configures and verifies the retimer/mode-switch + * according to the provided setting. + */ +static int cros_typec_configure_mux(struct cros_typec_switch_data *sdata, int port_num, int index, + unsigned long mode, struct typec_altmode *alt) +{ + int ret = cros_typec_get_mux_state(mode, alt); + + if (ret < 0) + return ret; + + return cros_typec_cmd_mux_set(sdata, port_num, index, (u8)ret); +} + static int cros_typec_retimer_set(struct typec_retimer *retimer, struct typec_retimer_state *state) { - return 0; + struct cros_typec_port *port = typec_retimer_get_drvdata(retimer); + + /* Retimers have index 1. */ + return cros_typec_configure_mux(port->sdata, port->port_num, 1, state->mode, state->alt); } static void cros_typec_unregister_switches(struct cros_typec_switch_data *sdata)