From patchwork Thu Nov 24 10:20:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pin-yen Lin X-Patchwork-Id: 25431 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3305584wrr; Thu, 24 Nov 2022 02:23:25 -0800 (PST) X-Google-Smtp-Source: AA0mqf4NkQO6czauOAvCAtQ/ZHpYQcvhiQulYocyamSJUkbuZlosbXzLLM9bgs4PfECQHbVxH6+I X-Received: by 2002:a62:ee05:0:b0:56b:b253:36da with SMTP id e5-20020a62ee05000000b0056bb25336damr19458449pfi.59.1669285404975; Thu, 24 Nov 2022 02:23:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669285404; cv=none; d=google.com; s=arc-20160816; b=PMmrn898+0MVogAQMiEiMw6bJeaaNbYOpTizmAaMpjn3L2g0/IxNzNcfD16jC8Anrn sEP7BqtfQmDhIiqDYxjE6jCLeMBtpXcbFjP6PbqNzSyR264w8klzNGuN+wjjzVMbs/pK F8YAv8ZzadoIn9yN6fqqLphXsaxxJrx9WhYLRV2mfpNQ4/PfPridEIi7d47/blO0tDx7 +5mnLsVegFt/RsJVPPsq+H95XR3fYScvbZhyagf2uIKI0l6oAR+lvJ68cfr2ci4Ja6tQ +hcBnsM7Kbvq2OhpVQV+Mf1DtXq+MZb8X+z1m5vy9E/ctSXUmO60++lm6NIfpNmHE72U jLsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=/AMzuZTvtULhDaNSb1CGR7wc9XfQGANRhchKL9iMWpc=; b=Fwf4pvZvW1tGeawCVtcHnA4YWdfYgocnAA6D6ZI7jvGt8ZKax5ri64Gfc9o2Kc6mL8 gbzxIj1zKJ1Ne6c2aQW7Uq0xsvi7QDExvzoWz3Yb9Ka+GzJKNudkkfvUNtLHLfMHf9a7 WNRm7lMKOzSqfmYgBfIkpXBw0krYpRFxPToXH/i+zjgiXQFUYVHhcx/rVQQFH2NsXPMR ORouY5bVPGYJA6EodJqWw3v1lRW8pcKFa1yZG5eDPGx+9yU278cDNFdZMslxzeCzsIsV prMsrQ1LFKpOeHtz8r10LIN1DHWROusBPcQSdWxWOPavRNlMho+0EUW5b23j5hnwx+zi fYaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=jYETd7vR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mw13-20020a17090b4d0d00b002134f593f88si4743673pjb.38.2022.11.24.02.23.12; Thu, 24 Nov 2022 02:23:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=jYETd7vR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229842AbiKXKWP (ORCPT + 99 others); Thu, 24 Nov 2022 05:22:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229852AbiKXKVk (ORCPT ); Thu, 24 Nov 2022 05:21:40 -0500 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A56A114F9DE for ; Thu, 24 Nov 2022 02:21:39 -0800 (PST) Received: by mail-pg1-x532.google.com with SMTP id h193so1198234pgc.10 for ; Thu, 24 Nov 2022 02:21:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/AMzuZTvtULhDaNSb1CGR7wc9XfQGANRhchKL9iMWpc=; b=jYETd7vRF39IppIhmII7tbsyaUQpByJvGYddMez3t0wvAsKDcjqYisry/Hjr/8vxPD PyTMM/9y3cCcuPrjYSHLe6bs16y4EJQ9+eKf54Er6N5NNrlWQwPMIOsLaIC5iUzunsD5 23bQsA+xq3PaWRlVLGnKgTLHiWS+ns7D3LtY4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/AMzuZTvtULhDaNSb1CGR7wc9XfQGANRhchKL9iMWpc=; b=m8fO8VjwnZg5tBbYEG1rQ6jm88HHioWscw+OEZZLmDLoWy6r5uvHG5FspwrKOYBb2h NB0JAEfoUaY1J6YOAccgxM/vvuGcn7VfF2PsBLwVK2+Dmw7YyIVrC4F9X5tU3KSwJ8/x zFTlwf+gMv3xvaIidM5FgQ02L7m+J4rCmj37iXchzwM7QDx6meCihv8KKNLXu49paKO1 Bl58AqFZ9ohKxNGYK2riDKG/FJDaIHJQFgnJCJRbq3S+NYch5Rj62Sk13x/bMkQHHyK5 /lE/26Ew9bWQmE1SGoQOIZVFJ6ynqI+5W+Yy/yFUUO7033KJvM1wPFBIKO66PvuXUq8p aACw== X-Gm-Message-State: ANoB5plVTKpzgS7rtOoF19S5+nM9R7DV9O0JQsFg61OCIr8ADEDiyWdM c7yCASJSozeWOln8R+9YRGkUUg== X-Received: by 2002:a62:148f:0:b0:560:ee47:81e1 with SMTP id 137-20020a62148f000000b00560ee4781e1mr17482882pfu.63.1669285299158; Thu, 24 Nov 2022 02:21:39 -0800 (PST) Received: from treapking.tpe.corp.google.com ([2401:fa00:1:10:d446:708:8556:dfee]) by smtp.gmail.com with ESMTPSA id t123-20020a625f81000000b005747b59fc54sm854584pfb.172.2022.11.24.02.21.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Nov 2022 02:21:38 -0800 (PST) From: Pin-yen Lin To: Andrzej Hajda , Neil Armstrong , Robert Foss , Laurent Pinchart , Jonas Karlman , Jernej Skrabec , David Airlie , Daniel Vetter , Rob Herring , Krzysztof Kozlowski , Andy Shevchenko , Daniel Scally , Heikki Krogerus , Sakari Ailus , Greg Kroah-Hartman , "Rafael J . Wysocki" , Prashant Malani , Benson Leung , Guenter Roeck Cc: Javier Martinez Canillas , Stephen Boyd , dri-devel@lists.freedesktop.org, Hsin-Yi Wang , Thomas Zimmermann , devicetree@vger.kernel.org, Pin-yen Lin , chrome-platform@lists.linux.dev, linux-acpi@vger.kernel.org, Marek Vasut , Xin Ji , Lyude Paul , =?utf-8?q?N=C3=ADcolas_F_=2E_R_=2E_A_=2E_Pra?= =?utf-8?q?do?= , AngeloGioacchino Del Regno , linux-kernel@vger.kernel.org, Allen Chen Subject: [PATCH v6 5/7] drm/bridge: anx7625: Register Type C mode switches Date: Thu, 24 Nov 2022 18:20:54 +0800 Message-Id: <20221124102056.393220-6-treapking@chromium.org> X-Mailer: git-send-email 2.38.1.584.g0f3c55d4c2-goog In-Reply-To: <20221124102056.393220-1-treapking@chromium.org> References: <20221124102056.393220-1-treapking@chromium.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750372612927063719?= X-GMAIL-MSGID: =?utf-8?q?1750372612927063719?= Register USB Type-C mode switches when the "mode-switch" property and relevant port are available in Device Tree. Configure the crosspoint switch based on the entered alternate mode for a specific Type-C connector. Signed-off-by: Pin-yen Lin --- Changes in v6: - Squashed to a single patch drivers/gpu/drm/bridge/analogix/Kconfig | 1 + drivers/gpu/drm/bridge/analogix/anx7625.c | 169 ++++++++++++++++++++++ drivers/gpu/drm/bridge/analogix/anx7625.h | 20 +++ 3 files changed, 190 insertions(+) diff --git a/drivers/gpu/drm/bridge/analogix/Kconfig b/drivers/gpu/drm/bridge/analogix/Kconfig index 173dada218ec..992b43ed1dd7 100644 --- a/drivers/gpu/drm/bridge/analogix/Kconfig +++ b/drivers/gpu/drm/bridge/analogix/Kconfig @@ -34,6 +34,7 @@ config DRM_ANALOGIX_ANX7625 tristate "Analogix Anx7625 MIPI to DP interface support" depends on DRM depends on OF + depends on TYPEC || TYPEC=n select DRM_DISPLAY_DP_HELPER select DRM_DISPLAY_HDCP_HELPER select DRM_DISPLAY_HELPER diff --git a/drivers/gpu/drm/bridge/analogix/anx7625.c b/drivers/gpu/drm/bridge/analogix/anx7625.c index b9e3d6ad8846..2485e61f5cab 100644 --- a/drivers/gpu/drm/bridge/analogix/anx7625.c +++ b/drivers/gpu/drm/bridge/analogix/anx7625.c @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include #include @@ -2573,6 +2575,167 @@ static void anx7625_runtime_disable(void *data) pm_runtime_disable(data); } +static void anx7625_set_crosspoint_switch(struct anx7625_data *ctx, + enum typec_orientation orientation) +{ + if (orientation == TYPEC_ORIENTATION_NORMAL) { + anx7625_reg_write(ctx, ctx->i2c.tcpc_client, TCPC_SWITCH_0, + SW_SEL1_SSRX_RX1 | SW_SEL1_DPTX0_RX2); + anx7625_reg_write(ctx, ctx->i2c.tcpc_client, TCPC_SWITCH_1, + SW_SEL2_SSTX_TX1 | SW_SEL2_DPTX1_TX2); + } else if (orientation == TYPEC_ORIENTATION_REVERSE) { + anx7625_reg_write(ctx, ctx->i2c.tcpc_client, TCPC_SWITCH_0, + SW_SEL1_SSRX_RX2 | SW_SEL1_DPTX0_RX1); + anx7625_reg_write(ctx, ctx->i2c.tcpc_client, TCPC_SWITCH_1, + SW_SEL2_SSTX_TX2 | SW_SEL2_DPTX1_TX1); + } +} + +static void anx7625_typec_two_ports_update(struct anx7625_data *ctx) +{ + if (ctx->typec_ports[0].dp_connected && ctx->typec_ports[1].dp_connected) + /* Both ports available, do nothing to retain the current one. */ + return; + else if (ctx->typec_ports[0].dp_connected) + anx7625_set_crosspoint_switch(ctx, TYPEC_ORIENTATION_NORMAL); + else if (ctx->typec_ports[1].dp_connected) + anx7625_set_crosspoint_switch(ctx, TYPEC_ORIENTATION_REVERSE); +} + +static int anx7625_typec_mux_set(struct typec_mux_dev *mux, + struct typec_mux_state *state) +{ + struct anx7625_port_data *data = typec_mux_get_drvdata(mux); + struct anx7625_data *ctx = data->ctx; + struct device *dev = &ctx->client->dev; + bool new_dp_connected, old_dp_connected; + + if (ctx->num_typec_switches == 1) + return 0; + + old_dp_connected = ctx->typec_ports[0].dp_connected || ctx->typec_ports[1].dp_connected; + + data->dp_connected = (state->alt && state->alt->svid == USB_TYPEC_DP_SID && + state->alt->mode == USB_TYPEC_DP_MODE); + + dev_dbg(dev, "mux_set dp_connected: c0=%d, c1=%d\n", + ctx->typec_ports[0].dp_connected, ctx->typec_ports[1].dp_connected); + + new_dp_connected = ctx->typec_ports[0].dp_connected || ctx->typec_ports[1].dp_connected; + + /* dp on, power on first */ + if (!old_dp_connected && new_dp_connected) + pm_runtime_get_sync(dev); + + anx7625_typec_two_ports_update(ctx); + + /* dp off, power off last */ + if (old_dp_connected && !new_dp_connected) + pm_runtime_put_sync(dev); + + return 0; +} + +static int anx7625_register_mode_switch(struct device *dev, struct device_node *node, + struct anx7625_data *ctx) +{ + struct anx7625_port_data *port_data; + struct typec_mux_desc mux_desc = {}; + char name[32]; + u32 dp_lanes[2]; + int ret, num_lanes, i, port_num = -1; + + num_lanes = drm_of_get_data_lanes_count(node, 0, 2); + if (num_lanes < 0) { + dev_err(dev, "Error on getting data lanes count: %d\n", num_lanes); + return num_lanes; + } + + ret = of_property_read_u32_array(node, "data-lanes", dp_lanes, num_lanes); + if (ret) { + dev_err(dev, "Failed to read the data-lanes variable: %d\n", + ret); + return ret; + } + + /* + * <0 1> refers to SSRX1/SSTX1, and <2 3> refers to SSRX2/SSTX2. + */ + for (i = 0; i < num_lanes; i++) { + if (port_num != -1 && port_num != dp_lanes[i] / 2) { + dev_err(dev, "Invalid data lane numbers\n"); + return -EINVAL; + } + port_num = dp_lanes[i] / 2; + } + + port_data = &ctx->typec_ports[port_num]; + port_data->ctx = ctx; + mux_desc.fwnode = &node->fwnode; + mux_desc.drvdata = port_data; + snprintf(name, sizeof(name), "%s-%u", node->name, port_num); + mux_desc.name = name; + mux_desc.set = anx7625_typec_mux_set; + + port_data->typec_mux = typec_mux_register(dev, &mux_desc); + if (IS_ERR(port_data->typec_mux)) { + ret = PTR_ERR(port_data->typec_mux); + dev_err(dev, "Mode switch register for port %d failed: %d\n", + port_num, ret); + } + + return ret; +} + +static void anx7625_unregister_typec_switches(struct anx7625_data *ctx) +{ + int i; + + for (i = 0; i < ctx->num_typec_switches; i++) + typec_mux_unregister(ctx->typec_ports[i].typec_mux); +} + +static int anx7625_register_typec_switches(struct device *dev, struct anx7625_data *ctx) +{ + struct device_node *port, *sw; + int ret = 0; + + port = of_graph_get_port_by_id(dev->of_node, 1); + + for_each_child_of_node(port, sw) { + if (of_property_read_bool(sw, "mode-switch")) + ctx->num_typec_switches++; + } + + if (!ctx->num_typec_switches) { + dev_warn(dev, "No Type-C switches node found\n"); + return ret; + } + + ctx->typec_ports = devm_kcalloc( + dev, ctx->num_typec_switches, sizeof(struct anx7625_port_data), + GFP_KERNEL); + if (!ctx->typec_ports) + return -ENOMEM; + + /* Register switches for each connector. */ + for_each_child_of_node(port, sw) { + if (!of_property_read_bool(sw, "mode-switch")) + continue; + ret = anx7625_register_mode_switch(dev, sw, ctx); + if (ret) { + dev_err(dev, "Failed to register mode switch: %d\n", ret); + of_node_put(sw); + break; + } + } + + if (ret) + anx7625_unregister_typec_switches(ctx); + + return ret; +} + static int anx7625_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id) { @@ -2681,6 +2844,10 @@ static int anx7625_i2c_probe(struct i2c_client *client, if (platform->pdata.intp_irq) queue_work(platform->workqueue, &platform->work); + ret = anx7625_register_typec_switches(dev, platform); + if (ret && ret != -ENODEV) + dev_warn(dev, "Didn't register Type-C switches, err: %d\n", ret); + platform->bridge.funcs = &anx7625_bridge_funcs; platform->bridge.of_node = client->dev.of_node; if (!anx7625_of_panel_on_aux_bus(&client->dev)) @@ -2732,6 +2899,8 @@ static void anx7625_i2c_remove(struct i2c_client *client) drm_bridge_remove(&platform->bridge); + anx7625_unregister_typec_switches(platform); + if (platform->pdata.intp_irq) destroy_workqueue(platform->workqueue); diff --git a/drivers/gpu/drm/bridge/analogix/anx7625.h b/drivers/gpu/drm/bridge/analogix/anx7625.h index 14f33d6be289..b8634766b04e 100644 --- a/drivers/gpu/drm/bridge/analogix/anx7625.h +++ b/drivers/gpu/drm/bridge/analogix/anx7625.h @@ -55,6 +55,18 @@ #define HPD_STATUS_CHANGE 0x80 #define HPD_STATUS 0x80 +#define TCPC_SWITCH_0 0xB4 +#define SW_SEL1_DPTX0_RX2 BIT(0) +#define SW_SEL1_DPTX0_RX1 BIT(1) +#define SW_SEL1_SSRX_RX2 BIT(4) +#define SW_SEL1_SSRX_RX1 BIT(5) + +#define TCPC_SWITCH_1 0xB5 +#define SW_SEL2_DPTX1_TX2 BIT(0) +#define SW_SEL2_DPTX1_TX1 BIT(1) +#define SW_SEL2_SSTX_TX2 BIT(4) +#define SW_SEL2_SSTX_TX1 BIT(5) + /******** END of I2C Address 0x58 ********/ /***************************************************************/ @@ -449,6 +461,12 @@ struct anx7625_i2c_client { struct i2c_client *tcpc_client; }; +struct anx7625_port_data { + bool dp_connected; + struct typec_mux_dev *typec_mux; + struct anx7625_data *ctx; +}; + struct anx7625_data { struct anx7625_platform_data pdata; struct platform_device *audio_pdev; @@ -479,6 +497,8 @@ struct anx7625_data { struct drm_connector *connector; struct mipi_dsi_device *dsi; struct drm_dp_aux aux; + int num_typec_switches; + struct anx7625_port_data *typec_ports; }; #endif /* __ANX7625_H__ */