From patchwork Wed Sep 2 14:24:16 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Quadros X-Patchwork-Id: 52981 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-la0-f70.google.com (mail-la0-f70.google.com [209.85.215.70]) by patches.linaro.org (Postfix) with ESMTPS id 318112300B for ; Wed, 2 Sep 2015 14:27:42 +0000 (UTC) Received: by lamp12 with SMTP id p12sf4437318lam.2 for ; Wed, 02 Sep 2015 07:27:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-type:sender:precedence :list-id:x-original-sender:x-original-authentication-results :mailing-list:list-post:list-help:list-archive:list-unsubscribe; bh=bPszJmUw1qyEWRdF8PY5TmNMekgYtqnwqSMZwqmRGfE=; b=fDNOvR/DTcqiQy0VSkRWGMhh6wr5gwT4bMFPyT29Qevx5GkqfeDLPfiCpR2VvCb8Uy 2ZpW+2JRBK5MEOnhntLg6GeraOOUK3iLGEYEXRrEvXH0kA6a3CQ7szM6McdqqBhciZB0 WmMFyrG/Ahr5HnJx8H7bSCWGB8RzUz9Risq4XStYk/i9n4cbtv9RmB/JPN/qamCPjf/B ifMzuos4x396PTBrDkRK5QpbLtxi46e7jHy533sI0PtUUh2ffpgY/miXdGteeIjFIhNF UYm7KTO31U+xU5YCEbM68jNa3erZz3QmB7eUSQDC5ieKTNLv7xtEiyEeML5J8HsrtDnj qpOQ== X-Gm-Message-State: ALoCoQmQ3Nl1LnRTke+oo0q8ruczbfQEfJfrckih/5A45yxQUOSNwwTyQTWFhN4iXQVuIj7hCc+M X-Received: by 10.180.82.101 with SMTP id h5mr1110435wiy.1.1441204061180; Wed, 02 Sep 2015 07:27:41 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.153.4.163 with SMTP id cf3ls11068lad.89.gmail; Wed, 02 Sep 2015 07:27:40 -0700 (PDT) X-Received: by 10.112.209.106 with SMTP id ml10mr15775022lbc.112.1441204060847; Wed, 02 Sep 2015 07:27:40 -0700 (PDT) Received: from mail-lb0-f176.google.com (mail-lb0-f176.google.com. [209.85.217.176]) by mx.google.com with ESMTPS id b1si19780467lbd.146.2015.09.02.07.27.40 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 02 Sep 2015 07:27:40 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.176 as permitted sender) client-ip=209.85.217.176; Received: by lbbmp1 with SMTP id mp1so7170982lbb.1 for ; Wed, 02 Sep 2015 07:27:40 -0700 (PDT) X-Received: by 10.112.131.98 with SMTP id ol2mr16055139lbb.56.1441204060710; Wed, 02 Sep 2015 07:27:40 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.164.42 with SMTP id yn10csp616356lbb; Wed, 2 Sep 2015 07:27:39 -0700 (PDT) X-Received: by 10.68.162.99 with SMTP id xz3mr56783389pbb.134.1441204059592; Wed, 02 Sep 2015 07:27:39 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ny13si35719062pab.128.2015.09.02.07.27.38; Wed, 02 Sep 2015 07:27:39 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932076AbbIBO1g (ORCPT + 28 others); Wed, 2 Sep 2015 10:27:36 -0400 Received: from bear.ext.ti.com ([192.94.94.41]:56848 "EHLO bear.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753553AbbIBOYi (ORCPT ); Wed, 2 Sep 2015 10:24:38 -0400 Received: from dlelxv90.itg.ti.com ([172.17.2.17]) by bear.ext.ti.com (8.13.7/8.13.7) with ESMTP id t82EOWoU014480; Wed, 2 Sep 2015 09:24:32 -0500 Received: from DFLE73.ent.ti.com (dfle73.ent.ti.com [128.247.5.110]) by dlelxv90.itg.ti.com (8.14.3/8.13.8) with ESMTP id t82EOWW9004923; Wed, 2 Sep 2015 09:24:32 -0500 Received: from dflp32.itg.ti.com (10.64.6.15) by DFLE73.ent.ti.com (128.247.5.110) with Microsoft SMTP Server id 14.3.224.2; Wed, 2 Sep 2015 09:24:32 -0500 Received: from rockdesk.ti.com (ileax41-snat.itg.ti.com [10.172.224.153]) by dflp32.itg.ti.com (8.14.3/8.13.8) with ESMTP id t82EOPFO024150; Wed, 2 Sep 2015 09:24:29 -0500 From: Roger Quadros To: CC: , , , , , , , , Roger Quadros Subject: [PATCH v4 1/9] usb: dwc3: add dual-role support Date: Wed, 2 Sep 2015 17:24:16 +0300 Message-ID: <1441203864-15786-2-git-send-email-rogerq@ti.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1441203864-15786-1-git-send-email-rogerq@ti.com> References: <1441203864-15786-1-git-send-email-rogerq@ti.com> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: rogerq@ti.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.176 as permitted sender) smtp.mailfrom=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Register with the USB OTG core. Since we don't support OTG yet we just work as a dual-role device even if device tree says "otg". Use extcon framework to get VBUS/ID cable events and kick the OTG state machine. Signed-off-by: Roger Quadros Signed-off-by: Roger Quadros --- drivers/usb/dwc3/core.c | 174 ++++++++++++++++++++++++++++++++++++++- drivers/usb/dwc3/core.h | 7 ++ drivers/usb/dwc3/platform_data.h | 1 + 3 files changed, 181 insertions(+), 1 deletion(-) diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c index 064123e..2e36a9b 100644 --- a/drivers/usb/dwc3/core.c +++ b/drivers/usb/dwc3/core.c @@ -704,6 +704,152 @@ static int dwc3_core_get_phy(struct dwc3 *dwc) return 0; } +/* --------------------- Dual-Role management ------------------------------- */ + +static void dwc3_drd_fsm_sync(struct dwc3 *dwc) +{ + int id, vbus; + + /* get ID */ + id = extcon_get_cable_state(dwc->edev, "USB-HOST"); + /* Host means ID == 0 */ + id = !id; + + /* get VBUS */ + vbus = extcon_get_cable_state(dwc->edev, "USB"); + dev_dbg(dwc->dev, "id %d vbus %d\n", id, vbus); + + dwc->fsm->id = id; + dwc->fsm->b_sess_vld = vbus; + usb_otg_sync_inputs(dwc->fsm); +} + +static int dwc3_drd_start_host(struct otg_fsm *fsm, int on) +{ + struct device *dev = usb_otg_fsm_to_dev(fsm); + struct dwc3 *dwc = dev_get_drvdata(dev); + + dev_dbg(dwc->dev, "%s: %d\n", __func__, on); + if (on) { + dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST); + /* start the HCD */ + usb_otg_start_host(fsm, true); + } else { + /* stop the HCD */ + usb_otg_start_host(fsm, false); + } + + return 0; +} + +static int dwc3_drd_start_gadget(struct otg_fsm *fsm, int on) +{ + struct device *dev = usb_otg_fsm_to_dev(fsm); + struct dwc3 *dwc = dev_get_drvdata(dev); + + dev_dbg(dwc->dev, "%s: %d\n", __func__, on); + if (on) { + dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE); + dwc3_event_buffers_setup(dwc); + + /* start the UDC */ + usb_otg_start_gadget(fsm, true); + } else { + /* stop the UDC */ + usb_otg_start_gadget(fsm, false); + } + + return 0; +} + +static struct otg_fsm_ops dwc3_drd_ops = { + .start_host = dwc3_drd_start_host, + .start_gadget = dwc3_drd_start_gadget, +}; + +static int dwc3_drd_notifier(struct notifier_block *nb, + unsigned long event, void *ptr) +{ + struct dwc3 *dwc = container_of(nb, struct dwc3, otg_nb); + + dwc3_drd_fsm_sync(dwc); + + return NOTIFY_DONE; +} + +static int dwc3_drd_init(struct dwc3 *dwc) +{ + int ret, id, vbus; + struct usb_otg_caps *otgcaps = &dwc->otg_config.otg_caps; + + otgcaps->otg_rev = 0; + otgcaps->hnp_support = false; + otgcaps->srp_support = false; + otgcaps->adp_support = false; + dwc->otg_config.fsm_ops = &dwc3_drd_ops; + + if (!dwc->edev) { + dev_err(dwc->dev, "No extcon device found for OTG mode\n"); + return -ENODEV; + } + + dwc->otg_nb.notifier_call = dwc3_drd_notifier; + ret = extcon_register_notifier(dwc->edev, EXTCON_USB, &dwc->otg_nb); + if (ret < 0) { + dev_err(dwc->dev, "Couldn't register USB cable notifier\n"); + return -ENODEV; + } + + ret = extcon_register_notifier(dwc->edev, EXTCON_USB_HOST, + &dwc->otg_nb); + if (ret < 0) { + dev_err(dwc->dev, "Couldn't register USB-HOST cable notifier\n"); + ret = -ENODEV; + goto extcon_fail; + } + + /* sanity check id & vbus states */ + id = extcon_get_cable_state(dwc->edev, "USB-HOST"); + vbus = extcon_get_cable_state(dwc->edev, "USB"); + if (id < 0 || vbus < 0) { + dev_err(dwc->dev, "Invalid USB cable state. id %d, vbus %d\n", + id, vbus); + ret = -ENODEV; + goto fail; + } + + /* register as DRD device with OTG core */ + dwc->fsm = usb_otg_register(dwc->dev, &dwc->otg_config); + if (IS_ERR(dwc->fsm)) { + ret = PTR_ERR(dwc->fsm); + if (ret == -ENOTSUPP) + dev_err(dwc->dev, "CONFIG_USB_OTG needed for dual-role\n"); + else + dev_err(dwc->dev, "Failed to register with OTG core\n"); + + goto fail; + } + + dwc3_drd_fsm_sync(dwc); + + return 0; +fail: + extcon_unregister_notifier(dwc->edev, EXTCON_USB_HOST, &dwc->otg_nb); +extcon_fail: + extcon_unregister_notifier(dwc->edev, EXTCON_USB, &dwc->otg_nb); + + return ret; +} + +static void dwc3_drd_exit(struct dwc3 *dwc) +{ + usb_otg_unregister(dwc->dev); + extcon_unregister_notifier(dwc->edev, EXTCON_USB_HOST, &dwc->otg_nb); + extcon_unregister_notifier(dwc->edev, EXTCON_USB, &dwc->otg_nb); +} + +/* -------------------------------------------------------------------------- */ + static int dwc3_core_init_mode(struct dwc3 *dwc) { struct device *dev = dwc->dev; @@ -727,13 +873,21 @@ static int dwc3_core_init_mode(struct dwc3 *dwc) } break; case USB_DR_MODE_OTG: - dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG); + ret = dwc3_drd_init(dwc); + if (ret) { + dev_err(dev, "limiting to peripheral only\n"); + dwc->dr_mode = USB_DR_MODE_PERIPHERAL; + dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE); + goto gadget_init; + } + ret = dwc3_host_init(dwc); if (ret) { dev_err(dev, "failed to initialize host\n"); return ret; } +gadget_init: ret = dwc3_gadget_init(dwc); if (ret) { dev_err(dev, "failed to initialize gadget\n"); @@ -760,6 +914,7 @@ static void dwc3_core_exit_mode(struct dwc3 *dwc) case USB_DR_MODE_OTG: dwc3_host_exit(dwc); dwc3_gadget_exit(dwc); + dwc3_drd_exit(dwc); break; default: /* do nothing */ @@ -843,6 +998,16 @@ static int dwc3_probe(struct platform_device *pdev) hird_threshold = 12; if (node) { + if (of_property_read_bool(node, "extcon")) + dwc->edev = extcon_get_edev_by_phandle(dev, 0); + else if (of_property_read_bool(dev->parent->of_node, "extcon")) + dwc->edev = extcon_get_edev_by_phandle(dev->parent, 0); + + if (IS_ERR(dwc->edev)) { + dev_vdbg(dev, "couldn't get extcon device\n"); + return -EPROBE_DEFER; + } + dwc->maximum_speed = of_usb_get_maximum_speed(node); dwc->has_lpm_erratum = of_property_read_bool(node, "snps,has-lpm-erratum"); @@ -887,6 +1052,13 @@ static int dwc3_probe(struct platform_device *pdev) of_property_read_string(node, "snps,hsphy_interface", &dwc->hsphy_interface); } else if (pdata) { + if (pdata->extcon) { + dwc->edev = extcon_get_extcon_dev(pdata->extcon); + if (!dwc->edev) { + dev_vdbg(dev, "couldn't get extcon device\n"); + return -EPROBE_DEFER; + } + } dwc->maximum_speed = pdata->maximum_speed; dwc->has_lpm_erratum = pdata->has_lpm_erratum; if (pdata->lpm_nyet_threshold) diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h index 0447788..5ca2b25 100644 --- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -31,8 +31,10 @@ #include #include #include +#include #include +#include #define DWC3_MSG_MAX 500 @@ -753,6 +755,11 @@ struct dwc3 { struct ulpi *ulpi; + struct extcon_dev *edev; /* USB cable events ID & VBUS */ + struct notifier_block otg_nb; /* notifier for USB cable events */ + struct otg_fsm *fsm; + struct usb_otg_config otg_config; + void __iomem *regs; size_t regs_size; diff --git a/drivers/usb/dwc3/platform_data.h b/drivers/usb/dwc3/platform_data.h index d3614ec..b3b245c 100644 --- a/drivers/usb/dwc3/platform_data.h +++ b/drivers/usb/dwc3/platform_data.h @@ -47,4 +47,5 @@ struct dwc3_platform_data { unsigned tx_de_emphasis:2; const char *hsphy_interface; + const char *extcon; /* extcon name for USB cable events ID/VBUS */ };