From patchwork Wed Oct 2 23:16:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 11171901 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1AF8516B1 for ; Wed, 2 Oct 2019 23:16:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id ED35A222C6 for ; Wed, 2 Oct 2019 23:16:37 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="yUio+wwG" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728656AbfJBXQh (ORCPT ); Wed, 2 Oct 2019 19:16:37 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:41863 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728616AbfJBXQZ (ORCPT ); Wed, 2 Oct 2019 19:16:25 -0400 Received: by mail-pl1-f196.google.com with SMTP id t10so560466plr.8 for ; Wed, 02 Oct 2019 16:16:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=rU3ZIe1wEyPAlyBuDaaRw79vpye8Qn/RpKU+olhtNWc=; b=yUio+wwG5BWebxxhvpw7haRUyEyivo6OPVbEi3XIn0sz8A+fsfgZW/IZSGk9ec0w4f k4ZlnzlypckTUxWu2hXxCWjxs8vw7UXlr95Mwpk0gklCavpWrE2yLme/muin40Hf1x9J ZtzTL/CsWMQoKLjUj4m/nIAlZgEjN2F9K2zALdSWKf4ca4eIshDHFjyScWISkS9ezCL6 xakFfbbDqt6ZTYKTEyt6iPYey0xWjdaWccEK8xBkX2tPDAq9m0/MdlVA+9kVeTC+Vabd 9hM4hLA32mrfL5Y1epXeavibzLHX300cNBtFli9v8J5PuuZWcPHBHLt/wi68gP7/DDEz DmXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=rU3ZIe1wEyPAlyBuDaaRw79vpye8Qn/RpKU+olhtNWc=; b=KOQaFDWM0skK2jCfJIOYM/IQ3PhzmyV9pUsi7yIzr69kx+fRoB9TutN1v6SenvdACl Z114fziSJUMzdSe9QjO4vezMUPerqSkGuIrJNIW5Gy91Tu2JvHynJL0cFss0ZFkVh+pY UcKjBcDisLiC/aEm4PcOVo2F4/0mZ9hloC8b3U4soko3AeURm+OhUWZ2gRvq8zwIRUnX 4uBRcwj/YlwB4iqE3LAJoOs/y4EziEfvO+u6jA6N/PE/4prfAiG8wBi3HGSyQ8Iyio8Y 7uY/POQnMRDGgJ426hzldFPSXf++HcVVVr92qsEgTar8hkS/YD9I2zDaPhoi/yuXCejS qhoA== X-Gm-Message-State: APjAAAVGIdzu7PifuH/c2/JZZwVU7EujQRdjgZnFhMYLloVXtMOB9wGW Bq4J0VrGjwoPdv8RQ+TSJODJ8w== X-Google-Smtp-Source: APXvYqwcoB4Ky8kF2Dkr6DmmT3lpgIWscDqSSRfTpsmolukj/DFBnWE3VjYEeGe1NHexI2kCdvgjsw== X-Received: by 2002:a17:902:ff08:: with SMTP id f8mr6340368plj.309.1570058183692; Wed, 02 Oct 2019 16:16:23 -0700 (PDT) Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id l7sm288791pjy.12.2019.10.02.16.16.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2019 16:16:22 -0700 (PDT) From: John Stultz To: lkml Cc: John Stultz , Greg Kroah-Hartman , Rob Herring , Mark Rutland , Heikki Krogerus , Suzuki K Poulose , Chunfeng Yun , Yu Chen , Felipe Balbi , Hans de Goede , Andy Shevchenko , Jun Li , Valentin Schneider , linux-usb@vger.kernel.org, devicetree@vger.kernel.org Subject: [RFC][PATCH 1/3] dt-bindings: usb: generic: Add role-switch-default-host binding Date: Wed, 2 Oct 2019 23:16:15 +0000 Message-Id: <20191002231617.3670-2-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191002231617.3670-1-john.stultz@linaro.org> References: <20191002231617.3670-1-john.stultz@linaro.org> Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org Add binding to configure the default role the controller assumes is host mode when the usb role is USB_ROLE_NONE. Cc: Greg Kroah-Hartman Cc: Rob Herring Cc: Mark Rutland Cc: Heikki Krogerus Cc: Suzuki K Poulose Cc: Chunfeng Yun Cc: Yu Chen Cc: Felipe Balbi Cc: Hans de Goede Cc: Andy Shevchenko Cc: Jun Li Cc: Valentin Schneider Cc: linux-usb@vger.kernel.org Cc: devicetree@vger.kernel.org Signed-off-by: John Stultz --- Documentation/devicetree/bindings/usb/generic.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Documentation/devicetree/bindings/usb/generic.txt b/Documentation/devicetree/bindings/usb/generic.txt index cf5a1ad456e6..013782fde293 100644 --- a/Documentation/devicetree/bindings/usb/generic.txt +++ b/Documentation/devicetree/bindings/usb/generic.txt @@ -34,6 +34,11 @@ Optional properties: the USB data role (USB host or USB device) for a given USB connector, such as Type-C, Type-B(micro). see connector/usb-connector.txt. + - role-switch-default-host: boolean, indicating if usb-role-switch is enabled + the device default operation mode of controller while + usb role is USB_ROLE_NONE is host mode. If this is not + set or false, it will be assumed the default is device + mode. This is an attribute to a USB controller such as: From patchwork Wed Oct 2 23:16:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 11171899 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CCB07139A for ; Wed, 2 Oct 2019 23:16:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9FEEC222C2 for ; Wed, 2 Oct 2019 23:16:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="ZexQjMb8" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728753AbfJBXQf (ORCPT ); Wed, 2 Oct 2019 19:16:35 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:42422 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728654AbfJBXQ0 (ORCPT ); Wed, 2 Oct 2019 19:16:26 -0400 Received: by mail-pg1-f194.google.com with SMTP id z12so521257pgp.9 for ; Wed, 02 Oct 2019 16:16:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=uG2nqozuwo9BwCrw+RwCa9b6zn63vZVGMltNLgbwu2E=; b=ZexQjMb8Ay+ScAsdYaEJSxUduZsElYFnqjVAM6juLiP6Fsox2O9LsZ49VUNprld79k yBP6K7pziYnfii5w8mzqklI5RKVYedOPVgM7xovliKQz1keF3nTrVi+GR+05FrApjMAs 9Uw1lPGgfpOdXKbVbu3CLelwgo+zZwuc9wBlA/ui6pFplAY9dX3GuXPymrl2AwaR4Eax yCsfoIiPsy6pkcJRids9x6uVw9zrmTg1TPcEjoAfnz87xXY1xEVxHhTxLGUcb1Xjvv57 v4j0b7HFPL7fag3I61PBGiitShfvcvYx9DeQrv5AMIzbI/SPFMAj5K86qHipeZ6E4wPi naMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=uG2nqozuwo9BwCrw+RwCa9b6zn63vZVGMltNLgbwu2E=; b=B1ooZty1ZbAzDb1n2fGtFieVs7hiagdIx6rUCXUoRisobj/Fpkbw1nd1OAYPXuwqBK DCxAtOYFWRqj0SYwkNWmKpWaogKUa9LPkld1d3JMMO3j72xCupFJNzfmAUzfJEE4kw6T Dd8jBgSb8zVfp/SWOWCZGppczf7s3mCb0BGk/ceY3r0bOMfI1wfj8mfSNpQQIXvVza1I DgRt8XdyT0l3Km93GuWb6tQ0pbN7ty4ff7e1JggWYYbf3iQ4EW62xce3TGonInqBiv1u UJg2dKixKIAaKLDdN7x4Hc5AChHKcYXpETzwGRsTAKbdSUNgSbVQde9LOVrOWBnKYBLk pauA== X-Gm-Message-State: APjAAAWKUQ6ARvAHD8v17KPIknX17XP49Y1+BxkiEtkSa8DjyYw/edx7 yoLoiB2jyBiy1To9mvJSZfVc1g== X-Google-Smtp-Source: APXvYqxu2u/IQP/LPwB0sAmV4cVg8e5OqNfignEooLRI2THErLoK7DUvR6SLzQPli3BYyhMwbmSigA== X-Received: by 2002:a63:225f:: with SMTP id t31mr6320256pgm.50.1570058185308; Wed, 02 Oct 2019 16:16:25 -0700 (PDT) Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id l7sm288791pjy.12.2019.10.02.16.16.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2019 16:16:24 -0700 (PDT) From: John Stultz To: lkml Cc: Yu Chen , Greg Kroah-Hartman , Rob Herring , Mark Rutland , Heikki Krogerus , Suzuki K Poulose , Chunfeng Yun , Felipe Balbi , Hans de Goede , Andy Shevchenko , Jun Li , Valentin Schneider , linux-usb@vger.kernel.org, devicetree@vger.kernel.org, John Stultz Subject: [RFC][PATCH 2/3] usb: roles: Add usb role switch notifier. Date: Wed, 2 Oct 2019 23:16:16 +0000 Message-Id: <20191002231617.3670-3-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191002231617.3670-1-john.stultz@linaro.org> References: <20191002231617.3670-1-john.stultz@linaro.org> Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org From: Yu Chen This patch adds notifier for drivers want to be informed of the usb role switch. Cc: Greg Kroah-Hartman Cc: Rob Herring Cc: Mark Rutland Cc: Heikki Krogerus Cc: Suzuki K Poulose Cc: Chunfeng Yun Cc: Yu Chen Cc: Felipe Balbi Cc: Hans de Goede Cc: Andy Shevchenko Cc: Jun Li Cc: Valentin Schneider Cc: linux-usb@vger.kernel.org Cc: devicetree@vger.kernel.org Suggested-by: Heikki Krogerus Signed-off-by: Yu Chen Signed-off-by: John Stultz --- drivers/usb/roles/class.c | 35 ++++++++++++++++++++++++++++++++++- include/linux/usb/role.h | 16 ++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c index 94b4e7db2b94..418e762d5d72 100644 --- a/drivers/usb/roles/class.c +++ b/drivers/usb/roles/class.c @@ -20,6 +20,7 @@ struct usb_role_switch { struct device dev; struct mutex lock; /* device lock*/ enum usb_role role; + struct blocking_notifier_head nh; /* From descriptor */ struct device *usb2_port; @@ -49,8 +50,10 @@ int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role) mutex_lock(&sw->lock); ret = sw->set(sw->dev.parent, role); - if (!ret) + if (!ret) { sw->role = role; + blocking_notifier_call_chain(&sw->nh, role, NULL); + } mutex_unlock(&sw->lock); @@ -58,6 +61,35 @@ int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role) } EXPORT_SYMBOL_GPL(usb_role_switch_set_role); +int usb_role_switch_register_notifier(struct usb_role_switch *sw, + struct notifier_block *nb) +{ + int ret = blocking_notifier_chain_register(&sw->nh, nb); + enum usb_role role; + + if (ret) + return ret; + + /* Initialize the notifier that was just registered */ + mutex_lock(&sw->lock); + if (sw->get) + role = sw->get(sw->dev.parent); + else + role = sw->role; + blocking_notifier_call_chain(&sw->nh, role, NULL); + mutex_unlock(&sw->lock); + + return 0; +} +EXPORT_SYMBOL_GPL(usb_role_switch_register_notifier); + +int usb_role_switch_unregister_notifier(struct usb_role_switch *sw, + struct notifier_block *nb) +{ + return blocking_notifier_chain_unregister(&sw->nh, nb); +} +EXPORT_SYMBOL_GPL(usb_role_switch_unregister_notifier); + /** * usb_role_switch_get_role - Get the USB role for a switch * @sw: USB role switch @@ -296,6 +328,7 @@ usb_role_switch_register(struct device *parent, return ERR_PTR(-ENOMEM); mutex_init(&sw->lock); + BLOCKING_INIT_NOTIFIER_HEAD(&sw->nh); sw->allow_userspace_control = desc->allow_userspace_control; sw->usb2_port = desc->usb2_port; diff --git a/include/linux/usb/role.h b/include/linux/usb/role.h index 2d77f97df72d..8dbf7940b7da 100644 --- a/include/linux/usb/role.h +++ b/include/linux/usb/role.h @@ -54,6 +54,10 @@ struct usb_role_switch * usb_role_switch_register(struct device *parent, const struct usb_role_switch_desc *desc); void usb_role_switch_unregister(struct usb_role_switch *sw); +int usb_role_switch_register_notifier(struct usb_role_switch *sw, + struct notifier_block *nb); +int usb_role_switch_unregister_notifier(struct usb_role_switch *sw, + struct notifier_block *nb); #else static inline int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role) @@ -87,6 +91,18 @@ usb_role_switch_register(struct device *parent, } static inline void usb_role_switch_unregister(struct usb_role_switch *sw) { } + +static int usb_role_switch_register_notifier(struct usb_role_switch *sw, + struct notifier_block *nb) +{ + return -ENODEV; +} + +static int usb_role_switch_unregister_notifier(struct usb_role_switch *sw, + struct notifier_block *nb) +{ + return -ENODEV; +} #endif #endif /* __LINUX_USB_ROLE_H */ From patchwork Wed Oct 2 23:16:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 11171897 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7E8B716B1 for ; Wed, 2 Oct 2019 23:16:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 515B8222C0 for ; Wed, 2 Oct 2019 23:16:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="SEs5WOuM" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728710AbfJBXQ3 (ORCPT ); Wed, 2 Oct 2019 19:16:29 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:39658 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728691AbfJBXQ2 (ORCPT ); Wed, 2 Oct 2019 19:16:28 -0400 Received: by mail-pg1-f194.google.com with SMTP id e1so537707pgj.6 for ; Wed, 02 Oct 2019 16:16:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=qxUelUZoTzRMyxNxjTZuYP1rs0YqSW4KUYVQurWum4s=; b=SEs5WOuMQ9i46KZODDHA2eT1753KTFIhfQtIBC0vgfTge55jTvENz2ehdd4XniMNpV FpJtfOyUH9JpzGyCOHvO1r+3/4sI8HYLS3DFBp3LIw7XcbjreXvWPVUDbhBEbirCSzEt wCsSGakoroSPK+wb+rVZnLReSg/0yKh333p+YgxAXAeLZ3NtWIG3VPJjTPRBe1rwYhWp qyWb4CTc6qpxj7lv4JpBBjdfFJtbV2Mf93hhOTNY5A/rA/yu+xv5sG0AIv2yzsWMkakG EcVoZFuOtv5RpH1aJLeWXuYiVHwxSUpcaIRXZ6I/TTaJfCj0uDQfK3AzpaJkExFZIExc JmaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=qxUelUZoTzRMyxNxjTZuYP1rs0YqSW4KUYVQurWum4s=; b=Qdd5NemC4ZidNTmjOcTK2teCZsyBQ9T3yK3PxUHsl0ye2jMxhcywX1ao00lnNFGwoy SmVh5CpEhed7vqf3a+OnZxw9uN2/QpRZdzYck2gfhwfjZ8QY8tdP1iZbG42vNxMCGg1H mBDC0aBC0knHF61bcHu1HdeiZ3MXr2JKYQaV08MrZG1zndrM/p4wM2ewtAc+giI8DobO 1dJpmJ78MrnU65Cx9H2F+hQl3zN40pd8q8EAOwVk0Vz4CmJKalLcyKMxI3FM98TXVf8W f/CdxXT1NjS4zmpVkjJxbX6OMtEKDN0KmIgHNez9Td9oPjptZ5b5YtlbTfOvGEPqcn08 9luQ== X-Gm-Message-State: APjAAAXHj9kpJqLqziDKxDQ9SaPI3CTwvWm2jlehPO478bIg3lmSDIpS QFFWGR5FuwZYG9igFAmkgYQk6A== X-Google-Smtp-Source: APXvYqziu9jZ7DI+pIb89nbIQdGRbeE9ytAfHCKWE3r1VSYxVgY8EoqtYas3YrMcVkRMQo60mB2rDA== X-Received: by 2002:a17:90a:8087:: with SMTP id c7mr7238620pjn.56.1570058187290; Wed, 02 Oct 2019 16:16:27 -0700 (PDT) Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id l7sm288791pjy.12.2019.10.02.16.16.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2019 16:16:26 -0700 (PDT) From: John Stultz To: lkml Cc: Yu Chen , Greg Kroah-Hartman , Rob Herring , Mark Rutland , Heikki Krogerus , Suzuki K Poulose , Chunfeng Yun , Felipe Balbi , Hans de Goede , Andy Shevchenko , Jun Li , Valentin Schneider , linux-usb@vger.kernel.org, devicetree@vger.kernel.org, John Stultz Subject: [RFC][PATCH 3/3] usb: dwc3: Registering a role switch in the DRD code. Date: Wed, 2 Oct 2019 23:16:17 +0000 Message-Id: <20191002231617.3670-4-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191002231617.3670-1-john.stultz@linaro.org> References: <20191002231617.3670-1-john.stultz@linaro.org> Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org From: Yu Chen The Type-C drivers use USB role switch API to inform the system about the negotiated data role, so registering a role switch in the DRD code in order to support platforms with USB Type-C connectors. Cc: Greg Kroah-Hartman Cc: Rob Herring Cc: Mark Rutland Cc: Heikki Krogerus Cc: Suzuki K Poulose Cc: Chunfeng Yun Cc: Yu Chen Cc: Felipe Balbi Cc: Hans de Goede Cc: Andy Shevchenko Cc: Jun Li Cc: Valentin Schneider Cc: linux-usb@vger.kernel.org Cc: devicetree@vger.kernel.org Suggested-by: Heikki Krogerus Signed-off-by: Yu Chen Signed-off-by: John Stultz --- drivers/usb/dwc3/Kconfig | 1 + drivers/usb/dwc3/core.h | 6 ++++ drivers/usb/dwc3/drd.c | 78 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 84 insertions(+), 1 deletion(-) diff --git a/drivers/usb/dwc3/Kconfig b/drivers/usb/dwc3/Kconfig index 89abc6078703..1104745c41a9 100644 --- a/drivers/usb/dwc3/Kconfig +++ b/drivers/usb/dwc3/Kconfig @@ -44,6 +44,7 @@ config USB_DWC3_DUAL_ROLE bool "Dual Role mode" depends on ((USB=y || USB=USB_DWC3) && (USB_GADGET=y || USB_GADGET=USB_DWC3)) depends on (EXTCON=y || EXTCON=USB_DWC3) + select USB_ROLE_SWITCH help This is the default mode of working of DWC3 controller where both host and gadget features are enabled. diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h index b3cb6eec3f8f..83728157b3e9 100644 --- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -951,6 +952,9 @@ struct dwc3_scratchpad_array { * @hsphy_mode: UTMI phy mode, one of following: * - USBPHY_INTERFACE_MODE_UTMI * - USBPHY_INTERFACE_MODE_UTMIW + * role_sw: usb_role_switch handle + * role_switch_default_mode: default operation mode of controller while + * usb role is USB_ROLE_NONE. * @usb2_phy: pointer to USB2 PHY * @usb3_phy: pointer to USB3 PHY * @usb2_generic_phy: pointer to USB2 PHY @@ -1085,6 +1089,8 @@ struct dwc3 { struct extcon_dev *edev; struct notifier_block edev_nb; enum usb_phy_interface hsphy_mode; + struct usb_role_switch *role_sw; + enum usb_dr_mode role_switch_default_mode; u32 fladj; u32 irq_gadget; diff --git a/drivers/usb/dwc3/drd.c b/drivers/usb/dwc3/drd.c index 726100d1ac0d..95b466a7faa0 100644 --- a/drivers/usb/dwc3/drd.c +++ b/drivers/usb/dwc3/drd.c @@ -479,6 +479,58 @@ static struct extcon_dev *dwc3_get_extcon(struct dwc3 *dwc) return edev; } +static int dwc3_usb_role_switch_set(struct device *dev, enum usb_role role) +{ + struct dwc3 *dwc = dev_get_drvdata(dev); + u32 mode; + + switch (role) { + case USB_ROLE_HOST: + mode = DWC3_GCTL_PRTCAP_HOST; + break; + case USB_ROLE_DEVICE: + mode = DWC3_GCTL_PRTCAP_DEVICE; + break; + default: + if (dwc->role_switch_default_mode == USB_DR_MODE_HOST) + mode = DWC3_GCTL_PRTCAP_HOST; + else + mode = DWC3_GCTL_PRTCAP_DEVICE; + break; + } + + dwc3_set_mode(dwc, mode); + return 0; +} + +static enum usb_role dwc3_usb_role_switch_get(struct device *dev) +{ + struct dwc3 *dwc = dev_get_drvdata(dev); + unsigned long flags; + enum usb_role role; + + spin_lock_irqsave(&dwc->lock, flags); + switch (dwc->current_dr_role) { + case DWC3_GCTL_PRTCAP_HOST: + role = USB_ROLE_HOST; + break; + case DWC3_GCTL_PRTCAP_DEVICE: + role = USB_ROLE_DEVICE; + break; + case DWC3_GCTL_PRTCAP_OTG: + role = dwc->current_otg_role; + break; + default: + if (dwc->role_switch_default_mode == USB_DR_MODE_HOST) + role = USB_ROLE_HOST; + else + role = USB_ROLE_DEVICE; + break; + } + spin_unlock_irqrestore(&dwc->lock, flags); + return role; +} + int dwc3_drd_init(struct dwc3 *dwc) { int ret, irq; @@ -487,7 +539,28 @@ int dwc3_drd_init(struct dwc3 *dwc) if (IS_ERR(dwc->edev)) return PTR_ERR(dwc->edev); - if (dwc->edev) { + if (device_property_read_bool(dwc->dev, "usb-role-switch")) { + struct usb_role_switch_desc dwc3_role_switch = {0}; + u32 mode; + + if (device_property_read_bool(dwc->dev, + "role-switch-default-host")) { + dwc->role_switch_default_mode = USB_DR_MODE_HOST; + mode = DWC3_GCTL_PRTCAP_HOST; + } else { + dwc->role_switch_default_mode = USB_DR_MODE_PERIPHERAL; + mode = DWC3_GCTL_PRTCAP_DEVICE; + } + dwc3_role_switch.fwnode = dev_fwnode(dwc->dev); + dwc3_role_switch.set = dwc3_usb_role_switch_set; + dwc3_role_switch.get = dwc3_usb_role_switch_get; + dwc->role_sw = usb_role_switch_register(dwc->dev, + &dwc3_role_switch); + if (IS_ERR(dwc->role_sw)) + return PTR_ERR(dwc->role_sw); + + dwc3_set_mode(dwc, mode); + } else if (dwc->edev) { dwc->edev_nb.notifier_call = dwc3_drd_notifier; ret = extcon_register_notifier(dwc->edev, EXTCON_USB_HOST, &dwc->edev_nb); @@ -534,6 +607,9 @@ void dwc3_drd_exit(struct dwc3 *dwc) { unsigned long flags; + if (dwc->role_sw) + usb_role_switch_unregister(dwc->role_sw); + if (dwc->edev) extcon_unregister_notifier(dwc->edev, EXTCON_USB_HOST, &dwc->edev_nb);