aece52c6a4fbe8d2df8eb7bc5ca53c0b0dcb49a6
[oweals/openwrt.git] /
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
5  role swap
6
7 Register a usb_role_switch for dual role swap if the property
8 "usb-role-switch" is present.
9
10 Signed-off-by: Li Jun <jun.li@nxp.com>
11 ---
12  drivers/usb/dwc3/core.h |  2 ++
13  drivers/usb/dwc3/drd.c  | 47 ++++++++++++++++++++++++++++++++++++++++++++++-
14  2 files changed, 48 insertions(+), 1 deletion(-)
15
16 --- a/drivers/usb/dwc3/core.h
17 +++ b/drivers/usb/dwc3/core.h
18 @@ -25,6 +25,7 @@
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>
24  
25  #include <linux/phy/phy.h>
26 @@ -1082,6 +1083,7 @@ struct dwc3 {
27         void __iomem            *regs;
28         size_t                  regs_size;
29  
30 +       struct usb_role_switch  *role_switch;
31         enum usb_dr_mode        dr_mode;
32         u32                     current_dr_role;
33         u32                     desired_dr_role;
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
37         return edev;
38  }
39  
40 +static int dwc3_usb_role_switch_set(struct device *dev, enum usb_role role)
41 +{
42 +       u32 mode;
43 +
44 +       switch (role) {
45 +       case USB_ROLE_HOST:
46 +               mode = DWC3_GCTL_PRTCAP_HOST;
47 +               break;
48 +       case USB_ROLE_DEVICE:
49 +               mode = DWC3_GCTL_PRTCAP_DEVICE;
50 +               break;
51 +       default:
52 +               return 0;
53 +       };
54 +
55 +       dwc3_set_mode(dev_get_drvdata(dev), mode);
56 +       return 0;
57 +}
58 +
59 +static enum usb_role dwc3_usb_role_switch_get(struct device *dev)
60 +{
61 +       struct dwc3 *dwc = dev_get_drvdata(dev);
62 +       unsigned long flags;
63 +       enum usb_role role;
64 +
65 +       spin_lock_irqsave(&dwc->lock, flags);
66 +       role = dwc->current_dr_role;
67 +       spin_unlock_irqrestore(&dwc->lock, flags);
68 +
69 +       return role;
70 +}
71 +
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,
75 +};
76 +
77  int dwc3_drd_init(struct dwc3 *dwc)
78  {
79         int ret, irq;
80 @@ -484,7 +521,12 @@ int dwc3_drd_init(struct dwc3 *dwc)
81         if (IS_ERR(dwc->edev))
82                 return PTR_ERR(dwc->edev);
83  
84 -       if (dwc->edev) {
85 +       if (device_property_read_bool(dwc->dev, "usb-role-switch")) {
86 +               dwc->role_switch = usb_role_switch_register(dwc->dev,
87 +                                                           &dwc3_role_switch);
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,
93                                                &dwc->edev_nb);
94 @@ -531,6 +573,9 @@ void dwc3_drd_exit(struct dwc3 *dwc)
95  {
96         unsigned long flags;
97  
98 +       if (dwc->role_switch)
99 +               usb_role_switch_unregister(dwc->role_switch);
100 +
101         if (dwc->edev)
102                 extcon_unregister_notifier(dwc->edev, EXTCON_USB_HOST,
103                                            &dwc->edev_nb);