Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp4716339rwb; Tue, 17 Jan 2023 04:44:59 -0800 (PST) X-Google-Smtp-Source: AMrXdXs8kM6XkYu62MHtG0lcMUYPVfYmtBlK6+VtWykr1LBIfv/aq0pNkFvWfmXfk+vk3G0rrE5H X-Received: by 2002:a17:90b:3784:b0:229:1887:1dde with SMTP id mz4-20020a17090b378400b0022918871ddemr3266932pjb.34.1673959498835; Tue, 17 Jan 2023 04:44:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673959498; cv=none; d=google.com; s=arc-20160816; b=y2rmXeJhGa9qQwB/t0rYxcbzdYxg4xms1j3B5mQKnhIrwiQsZleGx5RaaGxbLx1M7/ gDDeQOYqWEjgNKftGECoXhTa+4OxPXmbcNUJpkltbYxvRm7ahJWLxUILvSKI3Zs5AFSW GQwM+dwmJpJWSmbq72ftkJkxGyb/h9MpKiiuY15NKOcyDORL192OZFMi6IBsCobnOOm2 0wsAI94WObloWZxv6GkjUirKYFUbSZPm5UnjpDavlXRjayVx2fumHuRdA4xSBJeXwJaE ld2I30rSNUbShtIaIWYp1drlALA1bdQ6wFmV8Msd//clwtBfahVWt7T/PA70KvSs/jbY nPcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=soUvhNZdKYp2qR1YfA32uBH/GNi0Fg8sbqxVLtsZI0s=; b=OJuWsfFG7YsW51p89HklC89PXh0+zOUiLTeccnYFylPzN92LGK0HWm1WmlNMo4fF5y bxq8EnT9GsK2BIoxut1WLFeVhaHN9K4UPvOoILinWDqn3DH5Bfp3INCc7O4AWjyhdvWh K7QpZ65ZYADj5sGuSvOZWsvoh5bf83Q+wl4kcrVxFnBUwtWKfDT7aPHRA+m0D8KrocNc 0roWpLcd4YIRgAdwfeVNwX3EY74Cmzyuysrf9UXj7YF8Yq5q88RlCcjUyeiSkjff36uC hKRLwy8j4LtIilUTW1Mt/YD+q0lC5RzVA4uo0PPmDkuaSMZFJLYQ5n7q4tHawy3BmFGf c4/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=cL2avsgC; 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=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w1-20020a63d741000000b004a2e257462fsi32516457pgi.494.2023.01.17.04.44.53; Tue, 17 Jan 2023 04:44:58 -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=@intel.com header.s=Intel header.b=cL2avsgC; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236719AbjAQL4c (ORCPT + 48 others); Tue, 17 Jan 2023 06:56:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236588AbjAQL41 (ORCPT ); Tue, 17 Jan 2023 06:56:27 -0500 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 529BD166CB; Tue, 17 Jan 2023 03:56:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673956586; x=1705492586; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=gcnv7FtGXifpk5G8wjofQlyYGroL3+TkMBNsDwKKrQI=; b=cL2avsgCpLWpUC7J2oA1lqUoWb27YJ0hPJh0p5233slMizyecsntduzQ rErdTuODzbSRAc7Iux5LX5FCZ5YeNFKinm30msrsjM3ygHBp/PqYZYYoo PXbaOAmsTMxW2/3fBI96Fzf8smi3KdidCxnJ9bvlUFvWB9gSYsxt52ew6 9vxB0vyRex68YxQLmkgximog58B1TcyrE5kx+/s6xsh+2btbF9H1Urc6G xeWHnJYcJxBR1BfAwC2tIEcNpAxq/wO97+L6+xpjGSxDMrqpFCYeMMEqq E13Fi4PoInGeciRUD1I33OzMdPzSLs8SVHN8DP0mxGDpxQSL14lcrNgPi Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10592"; a="387026941" X-IronPort-AV: E=Sophos;i="5.97,222,1669104000"; d="scan'208";a="387026941" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jan 2023 03:56:23 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10592"; a="801720509" X-IronPort-AV: E=Sophos;i="5.97,222,1669104000"; d="scan'208";a="801720509" Received: from kuha.fi.intel.com ([10.237.72.185]) by fmsmga001.fm.intel.com with SMTP; 17 Jan 2023 03:56:19 -0800 Received: by kuha.fi.intel.com (sSMTP sendmail emulation); Tue, 17 Jan 2023 13:56:18 +0200 Date: Tue, 17 Jan 2023 13:56:18 +0200 From: Heikki Krogerus To: Bjorn Andersson Cc: Greg Kroah-Hartman , Rob Herring , Krzysztof Kozlowski , Bjorn Andersson , linux-usb@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 2/2] usb: typec: mux: Introduce GPIO-based SBU mux Message-ID: References: <20230113041115.4189210-1-quic_bjorande@quicinc.com> <20230113041115.4189210-2-quic_bjorande@quicinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230113041115.4189210-2-quic_bjorande@quicinc.com> X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE 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 On Thu, Jan 12, 2023 at 08:11:15PM -0800, Bjorn Andersson wrote: > From: Bjorn Andersson > > A design found in various Qualcomm-based boards is to use a USB switch, > controlled through a pair of GPIO lines to connect, disconnect and > switch the orientation of the SBU lines in USB Type-C applications. > > This introduces a generic driver, which implements the typec_switch and > typec_mux interfaces to perform these operations. > > Signed-off-by: Bjorn Andersson > Signed-off-by: Bjorn Andersson Reviewed-by: Heikki Krogerus > --- > > Changes since v1: > - Fixed "swapped" being uninitialized for TYPEC_ORIENTATION_NONE in switch_set > > drivers/usb/typec/mux/Kconfig | 6 + > drivers/usb/typec/mux/Makefile | 1 + > drivers/usb/typec/mux/gpio-sbu-mux.c | 172 +++++++++++++++++++++++++++ > 3 files changed, 179 insertions(+) > create mode 100644 drivers/usb/typec/mux/gpio-sbu-mux.c > > diff --git a/drivers/usb/typec/mux/Kconfig b/drivers/usb/typec/mux/Kconfig > index 5eb2c17d72c1..c46fa4f9d3df 100644 > --- a/drivers/usb/typec/mux/Kconfig > +++ b/drivers/usb/typec/mux/Kconfig > @@ -12,6 +12,12 @@ config TYPEC_MUX_FSA4480 > common USB Type-C connector. > If compiled as a module, the module will be named fsa4480. > > +config TYPEC_MUX_GPIO_SBU > + tristate "Generic GPIO based SBU mux for USB Type-C applications" > + help > + Say Y or M if your system uses a GPIO based mux for managing the > + connected state and the swapping of the SBU lines in a Type-C port. > + > config TYPEC_MUX_PI3USB30532 > tristate "Pericom PI3USB30532 Type-C cross switch driver" > depends on I2C > diff --git a/drivers/usb/typec/mux/Makefile b/drivers/usb/typec/mux/Makefile > index e52a56c16bfb..dda67e19b58b 100644 > --- a/drivers/usb/typec/mux/Makefile > +++ b/drivers/usb/typec/mux/Makefile > @@ -1,5 +1,6 @@ > # SPDX-License-Identifier: GPL-2.0 > > obj-$(CONFIG_TYPEC_MUX_FSA4480) += fsa4480.o > +obj-$(CONFIG_TYPEC_MUX_GPIO_SBU) += gpio-sbu-mux.o > obj-$(CONFIG_TYPEC_MUX_PI3USB30532) += pi3usb30532.o > obj-$(CONFIG_TYPEC_MUX_INTEL_PMC) += intel_pmc_mux.o > diff --git a/drivers/usb/typec/mux/gpio-sbu-mux.c b/drivers/usb/typec/mux/gpio-sbu-mux.c > new file mode 100644 > index 000000000000..f62516dafe8f > --- /dev/null > +++ b/drivers/usb/typec/mux/gpio-sbu-mux.c > @@ -0,0 +1,172 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Copyright (C) 2022 Linaro Ltd. > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +struct gpio_sbu_mux { > + struct gpio_desc *enable_gpio; > + struct gpio_desc *select_gpio; > + > + struct typec_switch_dev *sw; > + struct typec_mux_dev *mux; > + > + struct mutex lock; /* protect enabled and swapped */ > + bool enabled; > + bool swapped; > +}; > + > +static int gpio_sbu_switch_set(struct typec_switch_dev *sw, > + enum typec_orientation orientation) > +{ > + struct gpio_sbu_mux *sbu_mux = typec_switch_get_drvdata(sw); > + bool enabled; > + bool swapped; > + > + mutex_lock(&sbu_mux->lock); > + > + enabled = sbu_mux->enabled; > + swapped = sbu_mux->swapped; > + > + switch (orientation) { > + case TYPEC_ORIENTATION_NONE: > + enabled = false; > + break; > + case TYPEC_ORIENTATION_NORMAL: > + swapped = false; > + break; > + case TYPEC_ORIENTATION_REVERSE: > + swapped = true; > + break; > + } > + > + if (enabled != sbu_mux->enabled) > + gpiod_set_value(sbu_mux->enable_gpio, enabled); > + > + if (swapped != sbu_mux->swapped) > + gpiod_set_value(sbu_mux->select_gpio, swapped); > + > + sbu_mux->enabled = enabled; > + sbu_mux->swapped = swapped; > + > + mutex_unlock(&sbu_mux->lock); > + > + return 0; > +} > + > +static int gpio_sbu_mux_set(struct typec_mux_dev *mux, > + struct typec_mux_state *state) > +{ > + struct gpio_sbu_mux *sbu_mux = typec_mux_get_drvdata(mux); > + > + mutex_lock(&sbu_mux->lock); > + > + switch (state->mode) { > + case TYPEC_STATE_SAFE: > + case TYPEC_STATE_USB: > + sbu_mux->enabled = false; > + break; > + case TYPEC_DP_STATE_C: > + case TYPEC_DP_STATE_D: > + case TYPEC_DP_STATE_E: > + sbu_mux->enabled = true; > + break; > + default: > + break; > + } > + > + gpiod_set_value(sbu_mux->enable_gpio, sbu_mux->enabled); > + > + mutex_unlock(&sbu_mux->lock); > + > + return 0; > +} > + > +static int gpio_sbu_mux_probe(struct platform_device *pdev) > +{ > + struct typec_switch_desc sw_desc = { }; > + struct typec_mux_desc mux_desc = { }; > + struct device *dev = &pdev->dev; > + struct gpio_sbu_mux *sbu_mux; > + > + sbu_mux = devm_kzalloc(dev, sizeof(*sbu_mux), GFP_KERNEL); > + if (!sbu_mux) > + return -ENOMEM; > + > + mutex_init(&sbu_mux->lock); > + > + sbu_mux->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW); > + if (IS_ERR(sbu_mux->enable_gpio)) > + return dev_err_probe(dev, PTR_ERR(sbu_mux->enable_gpio), > + "unable to acquire enable gpio\n"); > + > + sbu_mux->select_gpio = devm_gpiod_get(dev, "select", GPIOD_OUT_LOW); > + if (IS_ERR(sbu_mux->select_gpio)) > + return dev_err_probe(dev, PTR_ERR(sbu_mux->select_gpio), > + "unable to acquire select gpio\n"); > + > + sw_desc.drvdata = sbu_mux; > + sw_desc.fwnode = dev_fwnode(dev); > + sw_desc.set = gpio_sbu_switch_set; > + > + sbu_mux->sw = typec_switch_register(dev, &sw_desc); > + if (IS_ERR(sbu_mux->sw)) > + return dev_err_probe(dev, PTR_ERR(sbu_mux->sw), > + "failed to register typec switch\n"); > + > + mux_desc.drvdata = sbu_mux; > + mux_desc.fwnode = dev_fwnode(dev); > + mux_desc.set = gpio_sbu_mux_set; > + > + sbu_mux->mux = typec_mux_register(dev, &mux_desc); > + if (IS_ERR(sbu_mux->mux)) { > + typec_switch_unregister(sbu_mux->sw); > + return dev_err_probe(dev, PTR_ERR(sbu_mux->mux), > + "failed to register typec mux\n"); > + } > + > + platform_set_drvdata(pdev, sbu_mux); > + > + return 0; > +} > + > +static int gpio_sbu_mux_remove(struct platform_device *pdev) > +{ > + struct gpio_sbu_mux *sbu_mux = platform_get_drvdata(pdev); > + > + gpiod_set_value(sbu_mux->enable_gpio, 0); > + > + typec_mux_unregister(sbu_mux->mux); > + typec_switch_unregister(sbu_mux->sw); > + > + return 0; > +} > + > +static const struct of_device_id gpio_sbu_mux_match[] = { > + { .compatible = "gpio-sbu-mux", }, > + {} > +}; > +MODULE_DEVICE_TABLE(of, gpio_sbu_mux_match); > + > +static struct platform_driver gpio_sbu_mux_driver = { > + .probe = gpio_sbu_mux_probe, > + .remove = gpio_sbu_mux_remove, > + .driver = { > + .name = "gpio_sbu_mux", > + .of_match_table = gpio_sbu_mux_match, > + }, > +}; > +module_platform_driver(gpio_sbu_mux_driver); > + > +MODULE_DESCRIPTION("GPIO based SBU mux driver"); > +MODULE_LICENSE("GPL"); > -- > 2.37.3 -- heikki