Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp5886130rwl; Wed, 22 Mar 2023 03:50:46 -0700 (PDT) X-Google-Smtp-Source: AK7set9mQ01q0g5hflNu8YKTdynOKezl+96fQMynEHPYxgecDBDumyrJ23d/pJvRPtAn6NAs8Yu2 X-Received: by 2002:a17:906:370b:b0:8b1:30da:b586 with SMTP id d11-20020a170906370b00b008b130dab586mr5884466ejc.49.1679482246115; Wed, 22 Mar 2023 03:50:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679482246; cv=none; d=google.com; s=arc-20160816; b=ZLylABjOY1NSaY8ua+6EDk+4URcBwzZETHPDa/0OnrQtctLDyiXmnkpiy5ZZ/lcofA 0OlRCY6UzbeSUTY6QrvOlGO1DNlQZypUiyvz+C5RFPdXC9czT3Rh7uMjIrg6o+zxWSOy NYd6UgVIQUh7atbAqZiYOSR21x9jnmqp7ydP+EYHWsc02DQ6aBPIJWt24haiGcdsvuVm xoL8wnNWrRVObTWzp9wqAI8ZlogXh6GuA6vvU2yMY6skTAYUXi6RWhWvjClnA569x0lq G3gV0oPJIzrN1Xqa2j8Dh14uLhHf9gCEeec3wtl8/mX1XlgCP9My/UlnfdweOzfHwc3e Fodg== 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=+FF2ESmiiihfdUKZ1gHccbyU50W0AbxwPHMUw+YJ4po=; b=g3efVu53NAEZNqX5p5uBYGZb5ah4gLBscwc1c59x95q9lkE8DmLbfAL/3WPah5lp+6 fOCpCKVC1CblI35fLSQFey65CpSzP+eL14Kt9BrF8FLp3Qn295orSV7+8cooqs4+qNjh dgFtCRxFlPvhizz3QrVML1OhGtr0iILbKjdyi/v/dJuBd5EPFRWOQezgmJ4hb+t92uBI ZEXD6mxd/OA2Yj3PxTQ6zXWWS+5v8mu6NitDAnq03wGjoD3oJe2jsyidYVc8YO6RMkYK N90Z3wxzgKLSOneCyvHeWXmaAzWEhTYQ/UmWN3Vn/GKGoS2gpFNHpyIWBxAWrGzzCNid LWNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=nfLgwSYC; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k20-20020a1709063fd400b00930cad91803si16248451ejj.473.2023.03.22.03.50.22; Wed, 22 Mar 2023 03:50:46 -0700 (PDT) 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=nfLgwSYC; 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 S230515AbjCVKtX (ORCPT + 99 others); Wed, 22 Mar 2023 06:49:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230464AbjCVKs1 (ORCPT ); Wed, 22 Mar 2023 06:48:27 -0400 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84F3662B60 for ; Wed, 22 Mar 2023 03:47:46 -0700 (PDT) Received: by mail-pj1-x102f.google.com with SMTP id mp3-20020a17090b190300b0023fcc8ce113so8223361pjb.4 for ; Wed, 22 Mar 2023 03:47:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1679482063; 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=+FF2ESmiiihfdUKZ1gHccbyU50W0AbxwPHMUw+YJ4po=; b=nfLgwSYCb6c8k79DrPtmV/SnDKXeLf/SKbimoDCIuB9DzO+ChXJZp0SJ7NLDZL6aes PCUUEnUHd8ePkTydG75UrPkpsXgNp9vW5OAGozkn4XjPWoyrEhEaqx+nnscCaLNN2q/O nMZMU0IOrkSM7450xgpev38xoUB/TVGcA1HeE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679482063; 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=+FF2ESmiiihfdUKZ1gHccbyU50W0AbxwPHMUw+YJ4po=; b=Ll+nSw7/f4cEEEXaHPkGRht+TFS47vGXYAsUHk8brQo7BB2BRMFTp6+y+gO4BB9Ctv mylmvvbeWCwbAZLcHEApGSQLxhDpDYIBv3ysUc2/FCdRK8iYG97T2fN5Hs2QNzqmRyy2 ubCsbwDP3f1mx/UDwg6dkhIuWFHvebw6/yLXl6/VgbvX9rZwhGCS2cNqpeKJmpwLs9M9 dwhlO4XFQON6Le5GjB7ilnSmmUwxrJmNYCVHQc3fqFHTiE2ytowRATiNjqbIdEwSKGy1 scVIirrn4eIRqX/AiNAmhCqovFdoEdimN72jxq+J4CgCJJFe9tkIYEH4OzrAowxyJBQ/ l6xQ== X-Gm-Message-State: AO0yUKUs5sDqV4Ro4SkbguwTnq1k4DMB7QvY3VsBMomtyRHPaIQovPHM 26OhS6/CdyIyvUQ/78u/pqYYDg== X-Received: by 2002:a17:90a:56:b0:23f:ebf2:d3de with SMTP id 22-20020a17090a005600b0023febf2d3demr3016122pjb.27.1679482063289; Wed, 22 Mar 2023 03:47:43 -0700 (PDT) Received: from treapking.tpe.corp.google.com ([2401:fa00:1:10:343d:79bf:55f9:1da5]) by smtp.gmail.com with ESMTPSA id hg4-20020a17090b300400b002340d317f3esm9508742pjb.52.2023.03.22.03.47.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Mar 2023 03:47:43 -0700 (PDT) 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: Xin Ji , linux-kernel@vger.kernel.org, Thomas Zimmermann , linux-acpi@vger.kernel.org, Javier Martinez Canillas , AngeloGioacchino Del Regno , Pin-yen Lin , Hsin-Yi Wang , Lyude Paul , =?UTF-8?q?N=C3=ADcolas=20F=20=2E=20R=20=2E=20A=20=2E=20Prado?= , Allen Chen , dri-devel@lists.freedesktop.org, Marek Vasut , Stephen Boyd , chrome-platform@lists.linux.dev, devicetree@vger.kernel.org Subject: [PATCH v14 10/10] drm/bridge: it6505: Register Type C mode switches Date: Wed, 22 Mar 2023 18:46:39 +0800 Message-Id: <20230322104639.221402-11-treapking@chromium.org> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog In-Reply-To: <20230322104639.221402-1-treapking@chromium.org> References: <20230322104639.221402-1-treapking@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable 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 Register USB Type-C mode switches when the "mode-switch" property and relevant port are available in Device Tree. Configure the "lane_swap" state based on the entered alternate mode for a specific Type-C connector, which ends up updating the lane swap registers of the it6505 chip. Signed-off-by: Pin-yen Lin --- Changes in v14: - Fix style issues Changes in v13: - Fix style issues Changes in v12: - Fixes style issues in it6505 driver - Replaced &it6505->client->dev with it6505->dev - Updated the error logs when parsing data-lanes property Changes in v11: - Added back "data-lanes" parsing logics - Removed Kconfig dependency - Updated the usage of the private data Changes in v7: - Fixed style issues in it6505 driver - Removed the redundant sleep in it6505 driver - Removed DT property validation in it6505 driver - Rebased to drm-misc-next - Extracted common codes to another commit Changes in v6: - Changed it6505_typec_mux_set callback function to accommodate with the latest drm-misc patches - Changed the driver implementation to accommodate with the new binding - Squashed to a single patch drivers/gpu/drm/bridge/ite-it6505.c | 190 +++++++++++++++++++++++++++- 1 file changed, 183 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/bridge/ite-it6505.c b/drivers/gpu/drm/bridge/ite-it6505.c index d4bc388b68ac..28d07fd7486b 100644 --- a/drivers/gpu/drm/bridge/ite-it6505.c +++ b/drivers/gpu/drm/bridge/ite-it6505.c @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include #include @@ -27,6 +29,7 @@ #include #include #include +#include #include #include @@ -401,6 +404,11 @@ struct debugfs_entries { const struct file_operations *fops; }; +struct it6505_typec_port_data { + bool dp_connected; + bool lane_swap; +}; + struct it6505 { struct drm_dp_aux aux; struct drm_bridge bridge; @@ -454,6 +462,9 @@ struct it6505 { struct delayed_work delayed_audio; struct it6505_audio_data audio; struct dentry *debugfs; + struct completion mux_register; + struct drm_dp_typec_switch_desc switch_desc; + struct it6505_typec_port_data *port_data; /* it6505 driver hold option */ bool enable_drv_hold; @@ -3345,12 +3356,162 @@ static void it6505_shutdown(struct i2c_client *client) it6505_lane_off(it6505); } +static void it6505_typec_ports_update(struct it6505 *it6505) +{ + unsigned int i; + + /* Check if both ports available and do nothing to retain the current one */ + if (it6505->port_data[0].dp_connected && it6505->port_data[1].dp_connected) + return; + + for (i = 0; i < 2; i++) { + if (it6505->port_data[i].dp_connected) + it6505->lane_swap = it6505->port_data[i].lane_swap; + } +} + +static int it6505_typec_mux_set(struct typec_mux_dev *mux, + struct typec_mux_state *state) +{ + struct drm_dp_typec_port_data *port = typec_mux_get_drvdata(mux); + struct it6505 *it6505 = port->data; + struct device *dev = it6505->dev; + struct drm_dp_typec_switch_desc switch_desc = it6505->switch_desc; + bool old_dp_connected, new_dp_connected; + + if (switch_desc.num_typec_switches == 1) + return 0; + + mutex_lock(&it6505->extcon_lock); + wait_for_completion(&it6505->mux_register); + + old_dp_connected = it6505->port_data[0].dp_connected || + it6505->port_data[1].dp_connected; + + it6505->port_data[port->port_num].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", + it6505->port_data[0].dp_connected, it6505->port_data[1].dp_connected); + + new_dp_connected = it6505->port_data[0].dp_connected || + it6505->port_data[1].dp_connected; + + if (it6505->enable_drv_hold) { + dev_dbg(dev, "enable driver hold\n"); + goto unlock; + } + + it6505_typec_ports_update(it6505); + + if (!old_dp_connected && new_dp_connected) { + int ret = pm_runtime_get_sync(dev); + + /* + * pm_runtime_force_suspend() disables runtime PM when the + * system enters suspend state. But on system resume, mux_set + * can be triggered before pm_runtime_force_resume() re-enables + * runtime PM. This makes the bridge stay powered off if the + * downstream display is connected when the system is suspended. + * Handling the error here to make sure the bridge is powered + * on, and leave the PM runtime usage count incremented so + * the future runtime PM calls is balanced. + */ + if (ret < 0) + it6505_poweron(it6505); + + complete_all(&it6505->extcon_completion); + } + + if (old_dp_connected && !new_dp_connected) { + reinit_completion(&it6505->extcon_completion); + pm_runtime_put_sync(dev); + if (it6505->bridge.dev) + drm_helper_hpd_irq_event(it6505->bridge.dev); + memset(it6505->dpcd, 0, sizeof(it6505->dpcd)); + } + +unlock: + mutex_unlock(&it6505->extcon_lock); + return 0; +} + +static void it6505_unregister_typec_switches(struct it6505 *it6505) +{ + drm_dp_unregister_typec_switches(&it6505->switch_desc); +} + +static int it6505_register_typec_switches(struct device *dev, struct it6505 *it6505) +{ + struct device_node *port_node = of_graph_get_port_by_id(dev->of_node, 1); + struct drm_dp_typec_switch_desc *switch_desc = &it6505->switch_desc; + int ret; + u32 dp_lanes[4]; + unsigned int i, num_lanes; + + ret = drm_dp_register_typec_switches(dev, &port_node->fwnode, + &it6505->switch_desc, it6505, + it6505_typec_mux_set); + if (ret) + return ret; + + it6505->port_data = devm_kcalloc(dev, switch_desc->num_typec_switches, + sizeof(struct it6505_typec_port_data), + GFP_KERNEL); + if (!it6505->port_data) { + ret = -ENOMEM; + goto unregister_mux; + } + + for (i = 0; i < switch_desc->num_typec_switches; i++) { + struct drm_dp_typec_port_data *port = &switch_desc->typec_ports[i]; + struct fwnode_handle *fwnode = port->fwnode; + + ret = fwnode_property_count_u32(fwnode, "data-lanes"); + if (ret < 0) { + dev_err(dev, + "Error on getting data lanes count from %pfwP: %d\n", + fwnode, ret); + goto unregister_mux; + } + if (ret > 2) { + dev_err(dev, + "Invalid data lanes count for mode switches from %pfwP: %d\n", + fwnode, ret); + ret = -EINVAL; + goto unregister_mux; + } + num_lanes = ret; + + ret = fwnode_property_read_u32_array(fwnode, "data-lanes", + dp_lanes, num_lanes); + if (ret) { + dev_err(dev, + "Failed to read the data-lanes variable: %d\n", + ret); + goto unregister_mux; + } + + it6505->port_data[i].lane_swap = (dp_lanes[0] >= 2); + } + complete_all(&it6505->mux_register); + + return 0; + +unregister_mux: + complete_all(&it6505->mux_register); + it6505_unregister_typec_switches(it6505); + return ret; +} + static int it6505_i2c_probe(struct i2c_client *client) { struct it6505 *it6505; struct device *dev = &client->dev; struct extcon_dev *extcon; - int err, intp_irq; + int err, intp_irq, ret; it6505 = devm_kzalloc(&client->dev, sizeof(*it6505), GFP_KERNEL); if (!it6505) @@ -3367,14 +3528,28 @@ static int it6505_i2c_probe(struct i2c_client *client) /* get extcon device from DTS */ extcon = extcon_get_edev_by_phandle(dev, 0); - if (PTR_ERR(extcon) == -EPROBE_DEFER) - return -EPROBE_DEFER; - if (IS_ERR(extcon)) { - dev_err(dev, "can not get extcon device!"); - return PTR_ERR(extcon); + ret = PTR_ERR_OR_ZERO(extcon); + if (ret == -EPROBE_DEFER) + return ret; + + if (ret) { + if (ret != -ENODEV) + dev_warn(dev, "Cannot get extcon device: %d\n", ret); + + it6505->extcon = NULL; + } else { + it6505->extcon = extcon; } - it6505->extcon = extcon; + init_completion(&it6505->mux_register); + ret = it6505_register_typec_switches(dev, it6505); + if (ret != -ENODEV) + dev_warn(dev, "Didn't register Type-C switches, err: %d\n", ret); + + if (ret && !it6505->extcon) { + dev_err(dev, "Both extcon and Type-C switch are not registered.\n"); + return -EINVAL; + } it6505->regmap = devm_regmap_init_i2c(client, &it6505_regmap_config); if (IS_ERR(it6505->regmap)) { @@ -3446,6 +3621,7 @@ static void it6505_i2c_remove(struct i2c_client *client) it6505_debugfs_remove(it6505); it6505_poweroff(it6505); it6505_remove_edid(it6505); + it6505_unregister_typec_switches(it6505); } static const struct i2c_device_id it6505_id[] = { -- 2.40.0.rc1.284.g88254d51c5-goog