[PATCH 2/2] usb: cdns3: fix type-c lane swap
From: Roger Quadros
Date: Thu Aug 15 2019 - 04:39:38 EST
We need to have a discrete IDLE state so that
we know type-C cable is disconnected.
On exting IDLE state we can program lane swap
via a PHY reset.
Signed-off-by: Roger Quadros <rogerq@xxxxxx>
---
drivers/usb/cdns3/core.c | 49 +++++++++++++++++++++++++++-----------
drivers/usb/cdns3/gadget.c | 2 ++
drivers/usb/cdns3/host.c | 3 +++
3 files changed, 40 insertions(+), 14 deletions(-)
diff --git a/drivers/usb/cdns3/core.c b/drivers/usb/cdns3/core.c
index 07f8d8f23930..3c42ee347836 100644
--- a/drivers/usb/cdns3/core.c
+++ b/drivers/usb/cdns3/core.c
@@ -44,9 +44,6 @@ static int cdns3_role_start(struct cdns3 *cdns, enum usb_role role)
cdns->role = role;
mutex_unlock(&cdns->mutex);
- if (role == USB_ROLE_NONE)
- return 0;
-
if (!cdns->roles[role])
return -ENXIO;
@@ -54,10 +51,6 @@ static int cdns3_role_start(struct cdns3 *cdns, enum usb_role role)
return 0;
mutex_lock(&cdns->mutex);
- if (role == USB_ROLE_HOST)
- cdns3_drd_switch_host(cdns, 1);
- else
- cdns3_drd_switch_gadget(cdns, 1);
ret = cdns->roles[role]->start(cdns);
if (!ret)
@@ -74,18 +67,11 @@ static void cdns3_role_stop(struct cdns3 *cdns)
if (WARN_ON(role > USB_ROLE_DEVICE))
return;
- if (role == USB_ROLE_NONE)
- return;
-
if (cdns->roles[role]->state == CDNS3_ROLE_STATE_INACTIVE)
return;
mutex_lock(&cdns->mutex);
cdns->roles[role]->stop(cdns);
- if (role == USB_ROLE_HOST)
- cdns3_drd_switch_host(cdns, 0);
- else
- cdns3_drd_switch_gadget(cdns, 0);
cdns->roles[role]->state = CDNS3_ROLE_STATE_INACTIVE;
mutex_unlock(&cdns->mutex);
@@ -99,6 +85,37 @@ static void cdns3_exit_roles(struct cdns3 *cdns)
static enum usb_role cdsn3_hw_role_state_machine(struct cdns3 *cdns);
+static int cdns3_idle_role_start(struct cdns3 *cdns)
+{
+ return 0;
+}
+
+static void cdns3_idle_role_stop(struct cdns3 *cdns)
+{
+ /* Program Lane swap and bring PHY out of RESET */
+ phy_reset(cdns->usb3_phy);
+}
+
+static int cdns3_idle_init(struct cdns3 *cdns)
+{
+ struct cdns3_role_driver *rdrv;
+
+ rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
+ if (!rdrv)
+ return -ENOMEM;
+
+ rdrv->start = cdns3_idle_role_start;
+ rdrv->stop = cdns3_idle_role_stop;
+ rdrv->state = CDNS3_ROLE_STATE_INACTIVE;
+ rdrv->suspend = NULL;
+ rdrv->resume = NULL;
+ rdrv->name = "idle";
+
+ cdns->roles[USB_ROLE_NONE] = rdrv;
+
+ return 0;
+}
+
/**
* cdns3_core_init_role - initialize role of operation
* @cdns: Pointer to cdns3 structure
@@ -136,6 +153,10 @@ static int cdns3_core_init_role(struct cdns3 *cdns)
*/
best_dr_mode = cdns->dr_mode;
+ ret = cdns3_idle_init(cdns);
+ if (ret)
+ return ret;
+
if (dr_mode == USB_DR_MODE_OTG) {
best_dr_mode = cdns->dr_mode;
} else if (cdns->dr_mode == USB_DR_MODE_OTG) {
diff --git a/drivers/usb/cdns3/gadget.c b/drivers/usb/cdns3/gadget.c
index a42e832b3c6a..2ac2eba284c7 100644
--- a/drivers/usb/cdns3/gadget.c
+++ b/drivers/usb/cdns3/gadget.c
@@ -2475,6 +2475,7 @@ void cdns3_gadget_exit(struct cdns3 *cdns)
kfree(priv_dev->zlp_buf);
kfree(priv_dev);
cdns->gadget_dev = NULL;
+ cdns3_drd_switch_gadget(cdns, 0);
}
static int cdns3_gadget_start(struct cdns3 *cdns)
@@ -2594,6 +2595,7 @@ static int __cdns3_gadget_init(struct cdns3 *cdns)
struct cdns3_device *priv_dev;
int ret = 0;
+ cdns3_drd_switch_gadget(cdns, 1);
pm_runtime_get_sync(cdns->dev);
ret = cdns3_gadget_start(cdns);
diff --git a/drivers/usb/cdns3/host.c b/drivers/usb/cdns3/host.c
index a7629ab2c20c..2733a8f71fcd 100644
--- a/drivers/usb/cdns3/host.c
+++ b/drivers/usb/cdns3/host.c
@@ -18,6 +18,8 @@ static int __cdns3_host_init(struct cdns3 *cdns)
struct platform_device *xhci;
int ret;
+ cdns3_drd_switch_host(cdns, 1);
+
xhci = platform_device_alloc("xhci-hcd", PLATFORM_DEVID_AUTO);
if (!xhci) {
dev_err(cdns->dev, "couldn't allocate xHCI device\n");
@@ -50,6 +52,7 @@ static void cdns3_host_exit(struct cdns3 *cdns)
{
platform_device_unregister(cdns->host_dev);
cdns->host_dev = NULL;
+ cdns3_drd_switch_host(cdns, 0);
}
int cdns3_host_init(struct cdns3 *cdns)
--
Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki.
Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki