]> Git Repo - linux.git/blob - drivers/usb/mtu3/mtu3_dr.c
usb: mtu3: move vbus and mode debugfs interfaces into mtu3_debugfs.c
[linux.git] / drivers / usb / mtu3 / mtu3_dr.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * mtu3_dr.c - dual role switch and host glue layer
4  *
5  * Copyright (C) 2016 MediaTek Inc.
6  *
7  * Author: Chunfeng Yun <[email protected]>
8  */
9
10 #include "mtu3.h"
11 #include "mtu3_dr.h"
12 #include "mtu3_debug.h"
13
14 #define USB2_PORT 2
15 #define USB3_PORT 3
16
17 enum mtu3_vbus_id_state {
18         MTU3_ID_FLOAT = 1,
19         MTU3_ID_GROUND,
20         MTU3_VBUS_OFF,
21         MTU3_VBUS_VALID,
22 };
23
24 static void toggle_opstate(struct ssusb_mtk *ssusb)
25 {
26         if (!ssusb->otg_switch.is_u3_drd) {
27                 mtu3_setbits(ssusb->mac_base, U3D_DEVICE_CONTROL, DC_SESSION);
28                 mtu3_setbits(ssusb->mac_base, U3D_POWER_MANAGEMENT, SOFT_CONN);
29         }
30 }
31
32 /* only port0 supports dual-role mode */
33 static int ssusb_port0_switch(struct ssusb_mtk *ssusb,
34         int version, bool tohost)
35 {
36         void __iomem *ibase = ssusb->ippc_base;
37         u32 value;
38
39         dev_dbg(ssusb->dev, "%s (switch u%d port0 to %s)\n", __func__,
40                 version, tohost ? "host" : "device");
41
42         if (version == USB2_PORT) {
43                 /* 1. power off and disable u2 port0 */
44                 value = mtu3_readl(ibase, SSUSB_U2_CTRL(0));
45                 value |= SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS;
46                 mtu3_writel(ibase, SSUSB_U2_CTRL(0), value);
47
48                 /* 2. power on, enable u2 port0 and select its mode */
49                 value = mtu3_readl(ibase, SSUSB_U2_CTRL(0));
50                 value &= ~(SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS);
51                 value = tohost ? (value | SSUSB_U2_PORT_HOST_SEL) :
52                         (value & (~SSUSB_U2_PORT_HOST_SEL));
53                 mtu3_writel(ibase, SSUSB_U2_CTRL(0), value);
54         } else {
55                 /* 1. power off and disable u3 port0 */
56                 value = mtu3_readl(ibase, SSUSB_U3_CTRL(0));
57                 value |= SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS;
58                 mtu3_writel(ibase, SSUSB_U3_CTRL(0), value);
59
60                 /* 2. power on, enable u3 port0 and select its mode */
61                 value = mtu3_readl(ibase, SSUSB_U3_CTRL(0));
62                 value &= ~(SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS);
63                 value = tohost ? (value | SSUSB_U3_PORT_HOST_SEL) :
64                         (value & (~SSUSB_U3_PORT_HOST_SEL));
65                 mtu3_writel(ibase, SSUSB_U3_CTRL(0), value);
66         }
67
68         return 0;
69 }
70
71 static void switch_port_to_host(struct ssusb_mtk *ssusb)
72 {
73         u32 check_clk = 0;
74
75         dev_dbg(ssusb->dev, "%s\n", __func__);
76
77         ssusb_port0_switch(ssusb, USB2_PORT, true);
78
79         if (ssusb->otg_switch.is_u3_drd) {
80                 ssusb_port0_switch(ssusb, USB3_PORT, true);
81                 check_clk = SSUSB_U3_MAC_RST_B_STS;
82         }
83
84         ssusb_check_clocks(ssusb, check_clk);
85
86         /* after all clocks are stable */
87         toggle_opstate(ssusb);
88 }
89
90 static void switch_port_to_device(struct ssusb_mtk *ssusb)
91 {
92         u32 check_clk = 0;
93
94         dev_dbg(ssusb->dev, "%s\n", __func__);
95
96         ssusb_port0_switch(ssusb, USB2_PORT, false);
97
98         if (ssusb->otg_switch.is_u3_drd) {
99                 ssusb_port0_switch(ssusb, USB3_PORT, false);
100                 check_clk = SSUSB_U3_MAC_RST_B_STS;
101         }
102
103         ssusb_check_clocks(ssusb, check_clk);
104 }
105
106 int ssusb_set_vbus(struct otg_switch_mtk *otg_sx, int is_on)
107 {
108         struct ssusb_mtk *ssusb =
109                 container_of(otg_sx, struct ssusb_mtk, otg_switch);
110         struct regulator *vbus = otg_sx->vbus;
111         int ret;
112
113         /* vbus is optional */
114         if (!vbus)
115                 return 0;
116
117         dev_dbg(ssusb->dev, "%s: turn %s\n", __func__, is_on ? "on" : "off");
118
119         if (is_on) {
120                 ret = regulator_enable(vbus);
121                 if (ret) {
122                         dev_err(ssusb->dev, "vbus regulator enable failed\n");
123                         return ret;
124                 }
125         } else {
126                 regulator_disable(vbus);
127         }
128
129         return 0;
130 }
131
132 /*
133  * switch to host: -> MTU3_VBUS_OFF --> MTU3_ID_GROUND
134  * switch to device: -> MTU3_ID_FLOAT --> MTU3_VBUS_VALID
135  */
136 static void ssusb_set_mailbox(struct otg_switch_mtk *otg_sx,
137         enum mtu3_vbus_id_state status)
138 {
139         struct ssusb_mtk *ssusb =
140                 container_of(otg_sx, struct ssusb_mtk, otg_switch);
141         struct mtu3 *mtu = ssusb->u3d;
142
143         dev_dbg(ssusb->dev, "mailbox state(%d)\n", status);
144
145         switch (status) {
146         case MTU3_ID_GROUND:
147                 switch_port_to_host(ssusb);
148                 ssusb_set_vbus(otg_sx, 1);
149                 ssusb->is_host = true;
150                 break;
151         case MTU3_ID_FLOAT:
152                 ssusb->is_host = false;
153                 ssusb_set_vbus(otg_sx, 0);
154                 switch_port_to_device(ssusb);
155                 break;
156         case MTU3_VBUS_OFF:
157                 mtu3_stop(mtu);
158                 pm_relax(ssusb->dev);
159                 break;
160         case MTU3_VBUS_VALID:
161                 /* avoid suspend when works as device */
162                 pm_stay_awake(ssusb->dev);
163                 mtu3_start(mtu);
164                 break;
165         default:
166                 dev_err(ssusb->dev, "invalid state\n");
167         }
168 }
169
170 static void ssusb_id_work(struct work_struct *work)
171 {
172         struct otg_switch_mtk *otg_sx =
173                 container_of(work, struct otg_switch_mtk, id_work);
174
175         if (otg_sx->id_event)
176                 ssusb_set_mailbox(otg_sx, MTU3_ID_GROUND);
177         else
178                 ssusb_set_mailbox(otg_sx, MTU3_ID_FLOAT);
179 }
180
181 static void ssusb_vbus_work(struct work_struct *work)
182 {
183         struct otg_switch_mtk *otg_sx =
184                 container_of(work, struct otg_switch_mtk, vbus_work);
185
186         if (otg_sx->vbus_event)
187                 ssusb_set_mailbox(otg_sx, MTU3_VBUS_VALID);
188         else
189                 ssusb_set_mailbox(otg_sx, MTU3_VBUS_OFF);
190 }
191
192 /*
193  * @ssusb_id_notifier is called in atomic context, but @ssusb_set_mailbox
194  * may sleep, so use work queue here
195  */
196 static int ssusb_id_notifier(struct notifier_block *nb,
197         unsigned long event, void *ptr)
198 {
199         struct otg_switch_mtk *otg_sx =
200                 container_of(nb, struct otg_switch_mtk, id_nb);
201
202         otg_sx->id_event = event;
203         schedule_work(&otg_sx->id_work);
204
205         return NOTIFY_DONE;
206 }
207
208 static int ssusb_vbus_notifier(struct notifier_block *nb,
209         unsigned long event, void *ptr)
210 {
211         struct otg_switch_mtk *otg_sx =
212                 container_of(nb, struct otg_switch_mtk, vbus_nb);
213
214         otg_sx->vbus_event = event;
215         schedule_work(&otg_sx->vbus_work);
216
217         return NOTIFY_DONE;
218 }
219
220 static int ssusb_extcon_register(struct otg_switch_mtk *otg_sx)
221 {
222         struct ssusb_mtk *ssusb =
223                 container_of(otg_sx, struct ssusb_mtk, otg_switch);
224         struct extcon_dev *edev = otg_sx->edev;
225         int ret;
226
227         /* extcon is optional */
228         if (!edev)
229                 return 0;
230
231         otg_sx->vbus_nb.notifier_call = ssusb_vbus_notifier;
232         ret = devm_extcon_register_notifier(ssusb->dev, edev, EXTCON_USB,
233                                         &otg_sx->vbus_nb);
234         if (ret < 0) {
235                 dev_err(ssusb->dev, "failed to register notifier for USB\n");
236                 return ret;
237         }
238
239         otg_sx->id_nb.notifier_call = ssusb_id_notifier;
240         ret = devm_extcon_register_notifier(ssusb->dev, edev, EXTCON_USB_HOST,
241                                         &otg_sx->id_nb);
242         if (ret < 0) {
243                 dev_err(ssusb->dev, "failed to register notifier for USB-HOST\n");
244                 return ret;
245         }
246
247         dev_dbg(ssusb->dev, "EXTCON_USB: %d, EXTCON_USB_HOST: %d\n",
248                 extcon_get_state(edev, EXTCON_USB),
249                 extcon_get_state(edev, EXTCON_USB_HOST));
250
251         /* default as host, switch to device mode if needed */
252         if (extcon_get_state(edev, EXTCON_USB_HOST) == false)
253                 ssusb_set_mailbox(otg_sx, MTU3_ID_FLOAT);
254         if (extcon_get_state(edev, EXTCON_USB) == true)
255                 ssusb_set_mailbox(otg_sx, MTU3_VBUS_VALID);
256
257         return 0;
258 }
259
260 /*
261  * We provide an interface via debugfs to switch between host and device modes
262  * depending on user input.
263  * This is useful in special cases, such as uses TYPE-A receptacle but also
264  * wants to support dual-role mode.
265  */
266 void ssusb_mode_manual_switch(struct ssusb_mtk *ssusb, int to_host)
267 {
268         struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
269
270         if (to_host) {
271                 ssusb_set_force_mode(ssusb, MTU3_DR_FORCE_HOST);
272                 ssusb_set_mailbox(otg_sx, MTU3_VBUS_OFF);
273                 ssusb_set_mailbox(otg_sx, MTU3_ID_GROUND);
274         } else {
275                 ssusb_set_force_mode(ssusb, MTU3_DR_FORCE_DEVICE);
276                 ssusb_set_mailbox(otg_sx, MTU3_ID_FLOAT);
277                 ssusb_set_mailbox(otg_sx, MTU3_VBUS_VALID);
278         }
279 }
280
281 void ssusb_set_force_mode(struct ssusb_mtk *ssusb,
282                           enum mtu3_dr_force_mode mode)
283 {
284         u32 value;
285
286         value = mtu3_readl(ssusb->ippc_base, SSUSB_U2_CTRL(0));
287         switch (mode) {
288         case MTU3_DR_FORCE_DEVICE:
289                 value |= SSUSB_U2_PORT_FORCE_IDDIG | SSUSB_U2_PORT_RG_IDDIG;
290                 break;
291         case MTU3_DR_FORCE_HOST:
292                 value |= SSUSB_U2_PORT_FORCE_IDDIG;
293                 value &= ~SSUSB_U2_PORT_RG_IDDIG;
294                 break;
295         case MTU3_DR_FORCE_NONE:
296                 value &= ~(SSUSB_U2_PORT_FORCE_IDDIG | SSUSB_U2_PORT_RG_IDDIG);
297                 break;
298         default:
299                 return;
300         }
301         mtu3_writel(ssusb->ippc_base, SSUSB_U2_CTRL(0), value);
302 }
303
304 int ssusb_otg_switch_init(struct ssusb_mtk *ssusb)
305 {
306         struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
307         int ret = 0;
308
309         INIT_WORK(&otg_sx->id_work, ssusb_id_work);
310         INIT_WORK(&otg_sx->vbus_work, ssusb_vbus_work);
311
312         if (otg_sx->manual_drd_enabled)
313                 ssusb_dr_debugfs_init(ssusb);
314         else
315                 ret = ssusb_extcon_register(otg_sx);
316
317         return ret;
318 }
319
320 void ssusb_otg_switch_exit(struct ssusb_mtk *ssusb)
321 {
322         struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
323
324         cancel_work_sync(&otg_sx->id_work);
325         cancel_work_sync(&otg_sx->vbus_work);
326 }
This page took 0.051682 seconds and 4 git commands to generate.