From patchwork Wed Mar 11 17:21:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 184404 Delivered-To: patches@linaro.org Received: by 2002:a92:1f12:0:0:0:0:0 with SMTP id i18csp869799ile; Wed, 11 Mar 2020 10:21:19 -0700 (PDT) X-Received: by 2002:a63:5206:: with SMTP id g6mr3934561pgb.419.1583947279232; Wed, 11 Mar 2020 10:21:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583947279; cv=none; d=google.com; s=arc-20160816; b=ctp1D3ds7FVw80hltAd+OoF589GOqk3OCrgYHpZmJhrEKZ3QG1s6K2IrZjRRRgX1dT u0wkWXI5uHJwHZs6msa62cZ4GPra+84JiBrM/KqjMFNbJgKIStORfYFr6uAx7NA0ExVe Qdq69/LKI8O2WroT2M0KOOCjdoJBjQbN3W1GNG7S1HT7ya3K4fAMTGt1jLY87K5FUgJQ FJkqLIfiVE5tZJRz6oxZ7qVI1xCgphhGEU74rAyydDqAXxCIRqoP1hu1ROKd96HOmF9S 0twcRUA8J6qcISuaIDSU2iK7J9iyNve/qYRJy5cFQF49LckSAiX44aZ2g1jMwJ0dDI8e Cn0Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=WAV7i9DpkYyptymNiUj/uooXSddOTDPzEmF8/rLdzNA=; b=K//opsxwTfeMbvcqEqnI+RKhDbytZq8WNHUODFO6VL1QisuFtmuGxcV9sxpa5jMdgy 6DsSF+Njj2DyAPaH9cou8sR3kk1mE5DGWT8to0mbQ0D/L9tkpurw84qsCz5+5oPCVtQK TeuQZ0JqEMKOLI31+3SjjiXhjmeQvaMaNowzQydcdQLOt9wL8+50kxqNTK/O8cljPn67 qwetmWCnPFd2aN1EJCmf3g0YiaIugmuJxVRuMx8z3gbQSf2rJsG3WpZtZztWt27byIFD O87/NN6c36WS/8G04S0Gbpd3BDDX1x95Ik8eqq4etv6LMMTaJjOH8t/exeXXKIXPuk5h P9qg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=v90sNn9X; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id x5sor3213614pgb.44.2020.03.11.10.21.19 for (Google Transport Security); Wed, 11 Mar 2020 10:21:19 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=v90sNn9X; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org 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=WAV7i9DpkYyptymNiUj/uooXSddOTDPzEmF8/rLdzNA=; b=v90sNn9X8BRpEKbFTfSlIt7FhpVLPNq1rrz4UZ7BYSmcgkeS70mPrFQdBay6B2aYbc Wc24d+bXbldbfKuYcztn/Sxpn/Vorbs7SKjHroXEeGHiW4jgp+7sliYnr9hqgXo+kZxS VmoKRdgQRScp0c6BfVlLCYO9+St3c+3uV40jsxs9HETJxMMJYk3nVIo/wAR6L/mcK54c 1wBFH0MKtHl/7lhS8EhQBry7zOu7W0vX4ImD6kR1Xm0pTBNPnHskBiB4KNgW3NfaeSE5 LTlrHOKFur2u2ndk8PQWPoCWPC2GJmZlpVdsLOwaRLgHQ0BsWtcjNL9dIYdf1pjMfqd+ i+iw== 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=WAV7i9DpkYyptymNiUj/uooXSddOTDPzEmF8/rLdzNA=; b=RUHbPhQLSmTlB3Kzevc9KGjNFYBbJWXYgPH2pDrOWm0obCa+ujjMykQqSmu9mrsTMT x/yOadjhjF9FvGKoaMpY24QfoffOJpLjm5BEIUgAZ3sSuecb+ugE0NIhafeTyjQrBfxc N0aUs6JZa3/PQ+c35M75+h6Od4DvEl6/lhXg7Tob/WR2fOGkUhcE9WjMj7XVcGuFGiJ0 05fxeROuVwn+ubyquz81ywlcmRkM9okF2g9gTLezsclIs0xCNbmikuD3lkT2/LvHN1qb wRO5l5w9TIaBlsUiMAEPNOCqWa8Tlaf792uCOD5ZGXmipt1l5kvU/0MrcxEQlKYNxHQP 8nlg== X-Gm-Message-State: ANhLgQ095fSyw6DhBOMod2v+u18qIuQtwntq+714f2GkSoIHpVaP+u1w IxqapCm5nhq4c3KdQ0RWWyjr8nq8 X-Google-Smtp-Source: ADFU+vtzBoZFcIDWNjK9MIBKHCBmyqHTO+sxRnhI5ba4B3NJdzaRPyIUlCz+gPylTkgIih79KI+8Yg== X-Received: by 2002:a63:a06e:: with SMTP id u46mr3790733pgn.140.1583947278772; Wed, 11 Mar 2020 10:21:18 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id b24sm14914053pfi.52.2020.03.11.10.21.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Mar 2020 10:21:18 -0700 (PDT) From: John Stultz To: lkml Cc: Yu Chen , Greg Kroah-Hartman , Rob Herring , Mark Rutland , ShuFan Lee , Heikki Krogerus , Suzuki K Poulose , Chunfeng Yun , Felipe Balbi , Hans de Goede , Andy Shevchenko , Jun Li , Valentin Schneider , Guillaume Gardet , Jack Pham , Bryan O'Donoghue , linux-usb@vger.kernel.org, devicetree@vger.kernel.org, John Stultz Subject: [RESEND][PATCH v8 1/6] usb: dwc3: Registering a role switch in the DRD code. Date: Wed, 11 Mar 2020 17:21:04 +0000 Message-Id: <20200311172109.45134-2-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200311172109.45134-1-john.stultz@linaro.org> References: <20200311172109.45134-1-john.stultz@linaro.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: ShuFan Lee 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: Guillaume Gardet Cc: Jack Pham Cc: Bryan O'Donoghue Cc: linux-usb@vger.kernel.org Cc: devicetree@vger.kernel.org Suggested-by: Heikki Krogerus Tested-by: Bryan O'Donoghue Signed-off-by: Yu Chen Signed-off-by: John Stultz --- v2: Fix role_sw and role_switch_default_mode descriptions as reported by kbuild test robot v3: Split out the role-switch-default-host logic into its own patch v5: Drop selecting CONFIG_USB_ROLE_SWITCH & ifdef dependent code v6: Fix build issue Reported-by: kbuild test robot v7: Minor fix for CONFIG_USB_ROLE_SWITCH=m case not building the role switch handling code, reported by Guillaume Gardet --- drivers/usb/dwc3/core.h | 3 ++ drivers/usb/dwc3/drd.c | 77 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 1 deletion(-) -- 2.17.1 diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h index 77c4a9abe365..a99e57636172 100644 --- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -953,6 +954,7 @@ 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 * @usb2_phy: pointer to USB2 PHY * @usb3_phy: pointer to USB3 PHY * @usb2_generic_phy: pointer to USB2 PHY @@ -1086,6 +1088,7 @@ struct dwc3 { struct extcon_dev *edev; struct notifier_block edev_nb; enum usb_phy_interface hsphy_mode; + struct usb_role_switch *role_sw; u32 fladj; u32 irq_gadget; diff --git a/drivers/usb/dwc3/drd.c b/drivers/usb/dwc3/drd.c index c946d64142ad..331c6e997f0c 100644 --- a/drivers/usb/dwc3/drd.c +++ b/drivers/usb/dwc3/drd.c @@ -476,6 +476,73 @@ static struct extcon_dev *dwc3_get_extcon(struct dwc3 *dwc) return edev; } +#if IS_ENABLED(CONFIG_USB_ROLE_SWITCH) +#define ROLE_SWITCH 1 +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: + 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: + role = USB_ROLE_DEVICE; + break; + } + spin_unlock_irqrestore(&dwc->lock, flags); + return role; +} + +static int dwc3_setup_role_switch(struct dwc3 *dwc) +{ + struct usb_role_switch_desc dwc3_role_switch = {NULL}; + + 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, DWC3_GCTL_PRTCAP_DEVICE); + return 0; +} +#else +#define ROLE_SWITCH 0 +#define dwc3_setup_role_switch(x) 0 +#endif + int dwc3_drd_init(struct dwc3 *dwc) { int ret, irq; @@ -484,7 +551,12 @@ int dwc3_drd_init(struct dwc3 *dwc) if (IS_ERR(dwc->edev)) return PTR_ERR(dwc->edev); - if (dwc->edev) { + if (ROLE_SWITCH && + device_property_read_bool(dwc->dev, "usb-role-switch")) { + ret = dwc3_setup_role_switch(dwc); + if (ret < 0) + return ret; + } else if (dwc->edev) { dwc->edev_nb.notifier_call = dwc3_drd_notifier; ret = extcon_register_notifier(dwc->edev, EXTCON_USB_HOST, &dwc->edev_nb); @@ -531,6 +603,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);