1 From 8595748e58885439c7e18a11a94702378bff3b02 Mon Sep 17 00:00:00 2001
2 From: Li Jun <jun.li@nxp.com>
3 Date: Sun, 27 Jan 2019 19:40:03 +0800
4 Subject: [PATCH] usb: dwc3: drd: add usb role switch class support for dual
7 Register a usb_role_switch for dual role swap if the property
8 "usb-role-switch" is present.
10 Signed-off-by: Li Jun <jun.li@nxp.com>
12 drivers/usb/dwc3/core.h | 2 ++
13 drivers/usb/dwc3/drd.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++-
14 2 files changed, 48 insertions(+), 1 deletion(-)
16 --- a/drivers/usb/dwc3/core.h
17 +++ b/drivers/usb/dwc3/core.h
19 #include <linux/usb/ch9.h>
20 #include <linux/usb/gadget.h>
21 #include <linux/usb/otg.h>
22 +#include <linux/usb/role.h>
23 #include <linux/ulpi/interface.h>
25 #include <linux/phy/phy.h>
26 @@ -1082,6 +1083,7 @@ struct dwc3 {
30 + struct usb_role_switch *role_switch;
31 enum usb_dr_mode dr_mode;
34 --- a/drivers/usb/dwc3/drd.c
35 +++ b/drivers/usb/dwc3/drd.c
36 @@ -476,6 +476,43 @@ static struct extcon_dev *dwc3_get_extco
40 +static int dwc3_usb_role_switch_set(struct device *dev, enum usb_role role)
46 + mode = DWC3_GCTL_PRTCAP_HOST;
48 + case USB_ROLE_DEVICE:
49 + mode = DWC3_GCTL_PRTCAP_DEVICE;
55 + dwc3_set_mode(dev_get_drvdata(dev), mode);
59 +static enum usb_role dwc3_usb_role_switch_get(struct device *dev)
61 + struct dwc3 *dwc = dev_get_drvdata(dev);
62 + unsigned long flags;
65 + spin_lock_irqsave(&dwc->lock, flags);
66 + role = dwc->current_dr_role;
67 + spin_unlock_irqrestore(&dwc->lock, flags);
72 +static const struct usb_role_switch_desc dwc3_role_switch = {
73 + .set = dwc3_usb_role_switch_set,
74 + .get = dwc3_usb_role_switch_get,
77 int dwc3_drd_init(struct dwc3 *dwc)
80 @@ -484,7 +521,12 @@ int dwc3_drd_init(struct dwc3 *dwc)
81 if (IS_ERR(dwc->edev))
82 return PTR_ERR(dwc->edev);
85 + if (device_property_read_bool(dwc->dev, "usb-role-switch")) {
86 + dwc->role_switch = usb_role_switch_register(dwc->dev,
88 + if (IS_ERR(dwc->role_switch))
89 + return PTR_ERR(dwc->role_switch);
90 + } else if (dwc->edev) {
91 dwc->edev_nb.notifier_call = dwc3_drd_notifier;
92 ret = extcon_register_notifier(dwc->edev, EXTCON_USB_HOST,
94 @@ -531,6 +573,9 @@ void dwc3_drd_exit(struct dwc3 *dwc)
98 + if (dwc->role_switch)
99 + usb_role_switch_unregister(dwc->role_switch);
102 extcon_unregister_notifier(dwc->edev, EXTCON_USB_HOST,