1 // SPDX-License-Identifier: GPL-2.0
3 * mtu3_dr.c - dual role switch and host glue layer
5 * Copyright (C) 2016 MediaTek Inc.
7 * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
10 #include <linux/usb/role.h>
14 #include "mtu3_debug.h"
19 enum mtu3_vbus_id_state
{
26 static char *mailbox_state_string(enum mtu3_vbus_id_state state
)
42 static void toggle_opstate(struct ssusb_mtk
*ssusb
)
44 if (!ssusb
->otg_switch
.is_u3_drd
) {
45 mtu3_setbits(ssusb
->mac_base
, U3D_DEVICE_CONTROL
, DC_SESSION
);
46 mtu3_setbits(ssusb
->mac_base
, U3D_POWER_MANAGEMENT
, SOFT_CONN
);
50 /* only port0 supports dual-role mode */
51 static int ssusb_port0_switch(struct ssusb_mtk
*ssusb
,
52 int version
, bool tohost
)
54 void __iomem
*ibase
= ssusb
->ippc_base
;
57 dev_dbg(ssusb
->dev
, "%s (switch u%d port0 to %s)\n", __func__
,
58 version
, tohost
? "host" : "device");
60 if (version
== USB2_PORT
) {
61 /* 1. power off and disable u2 port0 */
62 value
= mtu3_readl(ibase
, SSUSB_U2_CTRL(0));
63 value
|= SSUSB_U2_PORT_PDN
| SSUSB_U2_PORT_DIS
;
64 mtu3_writel(ibase
, SSUSB_U2_CTRL(0), value
);
66 /* 2. power on, enable u2 port0 and select its mode */
67 value
= mtu3_readl(ibase
, SSUSB_U2_CTRL(0));
68 value
&= ~(SSUSB_U2_PORT_PDN
| SSUSB_U2_PORT_DIS
);
69 value
= tohost
? (value
| SSUSB_U2_PORT_HOST_SEL
) :
70 (value
& (~SSUSB_U2_PORT_HOST_SEL
));
71 mtu3_writel(ibase
, SSUSB_U2_CTRL(0), value
);
73 /* 1. power off and disable u3 port0 */
74 value
= mtu3_readl(ibase
, SSUSB_U3_CTRL(0));
75 value
|= SSUSB_U3_PORT_PDN
| SSUSB_U3_PORT_DIS
;
76 mtu3_writel(ibase
, SSUSB_U3_CTRL(0), value
);
78 /* 2. power on, enable u3 port0 and select its mode */
79 value
= mtu3_readl(ibase
, SSUSB_U3_CTRL(0));
80 value
&= ~(SSUSB_U3_PORT_PDN
| SSUSB_U3_PORT_DIS
);
81 value
= tohost
? (value
| SSUSB_U3_PORT_HOST_SEL
) :
82 (value
& (~SSUSB_U3_PORT_HOST_SEL
));
83 mtu3_writel(ibase
, SSUSB_U3_CTRL(0), value
);
89 static void switch_port_to_host(struct ssusb_mtk
*ssusb
)
93 dev_dbg(ssusb
->dev
, "%s\n", __func__
);
95 ssusb_port0_switch(ssusb
, USB2_PORT
, true);
97 if (ssusb
->otg_switch
.is_u3_drd
) {
98 ssusb_port0_switch(ssusb
, USB3_PORT
, true);
99 check_clk
= SSUSB_U3_MAC_RST_B_STS
;
102 ssusb_check_clocks(ssusb
, check_clk
);
104 /* after all clocks are stable */
105 toggle_opstate(ssusb
);
108 static void switch_port_to_device(struct ssusb_mtk
*ssusb
)
112 dev_dbg(ssusb
->dev
, "%s\n", __func__
);
114 ssusb_port0_switch(ssusb
, USB2_PORT
, false);
116 if (ssusb
->otg_switch
.is_u3_drd
) {
117 ssusb_port0_switch(ssusb
, USB3_PORT
, false);
118 check_clk
= SSUSB_U3_MAC_RST_B_STS
;
121 ssusb_check_clocks(ssusb
, check_clk
);
124 int ssusb_set_vbus(struct otg_switch_mtk
*otg_sx
, int is_on
)
126 struct ssusb_mtk
*ssusb
=
127 container_of(otg_sx
, struct ssusb_mtk
, otg_switch
);
128 struct regulator
*vbus
= otg_sx
->vbus
;
131 /* vbus is optional */
135 dev_dbg(ssusb
->dev
, "%s: turn %s\n", __func__
, is_on
? "on" : "off");
138 ret
= regulator_enable(vbus
);
140 dev_err(ssusb
->dev
, "vbus regulator enable failed\n");
144 regulator_disable(vbus
);
151 * switch to host: -> MTU3_VBUS_OFF --> MTU3_ID_GROUND
152 * switch to device: -> MTU3_ID_FLOAT --> MTU3_VBUS_VALID
154 static void ssusb_set_mailbox(struct otg_switch_mtk
*otg_sx
,
155 enum mtu3_vbus_id_state status
)
157 struct ssusb_mtk
*ssusb
=
158 container_of(otg_sx
, struct ssusb_mtk
, otg_switch
);
159 struct mtu3
*mtu
= ssusb
->u3d
;
161 dev_dbg(ssusb
->dev
, "mailbox %s\n", mailbox_state_string(status
));
162 mtu3_dbg_trace(ssusb
->dev
, "mailbox %s", mailbox_state_string(status
));
166 switch_port_to_host(ssusb
);
167 ssusb_set_vbus(otg_sx
, 1);
168 ssusb
->is_host
= true;
171 ssusb
->is_host
= false;
172 ssusb_set_vbus(otg_sx
, 0);
173 switch_port_to_device(ssusb
);
177 pm_relax(ssusb
->dev
);
179 case MTU3_VBUS_VALID
:
180 /* avoid suspend when works as device */
181 pm_stay_awake(ssusb
->dev
);
185 dev_err(ssusb
->dev
, "invalid state\n");
189 static void ssusb_id_work(struct work_struct
*work
)
191 struct otg_switch_mtk
*otg_sx
=
192 container_of(work
, struct otg_switch_mtk
, id_work
);
194 if (otg_sx
->id_event
)
195 ssusb_set_mailbox(otg_sx
, MTU3_ID_GROUND
);
197 ssusb_set_mailbox(otg_sx
, MTU3_ID_FLOAT
);
200 static void ssusb_vbus_work(struct work_struct
*work
)
202 struct otg_switch_mtk
*otg_sx
=
203 container_of(work
, struct otg_switch_mtk
, vbus_work
);
205 if (otg_sx
->vbus_event
)
206 ssusb_set_mailbox(otg_sx
, MTU3_VBUS_VALID
);
208 ssusb_set_mailbox(otg_sx
, MTU3_VBUS_OFF
);
212 * @ssusb_id_notifier is called in atomic context, but @ssusb_set_mailbox
213 * may sleep, so use work queue here
215 static int ssusb_id_notifier(struct notifier_block
*nb
,
216 unsigned long event
, void *ptr
)
218 struct otg_switch_mtk
*otg_sx
=
219 container_of(nb
, struct otg_switch_mtk
, id_nb
);
221 otg_sx
->id_event
= event
;
222 schedule_work(&otg_sx
->id_work
);
227 static int ssusb_vbus_notifier(struct notifier_block
*nb
,
228 unsigned long event
, void *ptr
)
230 struct otg_switch_mtk
*otg_sx
=
231 container_of(nb
, struct otg_switch_mtk
, vbus_nb
);
233 otg_sx
->vbus_event
= event
;
234 schedule_work(&otg_sx
->vbus_work
);
239 static int ssusb_extcon_register(struct otg_switch_mtk
*otg_sx
)
241 struct ssusb_mtk
*ssusb
=
242 container_of(otg_sx
, struct ssusb_mtk
, otg_switch
);
243 struct extcon_dev
*edev
= otg_sx
->edev
;
246 /* extcon is optional */
250 otg_sx
->vbus_nb
.notifier_call
= ssusb_vbus_notifier
;
251 ret
= devm_extcon_register_notifier(ssusb
->dev
, edev
, EXTCON_USB
,
254 dev_err(ssusb
->dev
, "failed to register notifier for USB\n");
258 otg_sx
->id_nb
.notifier_call
= ssusb_id_notifier
;
259 ret
= devm_extcon_register_notifier(ssusb
->dev
, edev
, EXTCON_USB_HOST
,
262 dev_err(ssusb
->dev
, "failed to register notifier for USB-HOST\n");
266 dev_dbg(ssusb
->dev
, "EXTCON_USB: %d, EXTCON_USB_HOST: %d\n",
267 extcon_get_state(edev
, EXTCON_USB
),
268 extcon_get_state(edev
, EXTCON_USB_HOST
));
270 /* default as host, switch to device mode if needed */
271 if (extcon_get_state(edev
, EXTCON_USB_HOST
) == false)
272 ssusb_set_mailbox(otg_sx
, MTU3_ID_FLOAT
);
273 if (extcon_get_state(edev
, EXTCON_USB
) == true)
274 ssusb_set_mailbox(otg_sx
, MTU3_VBUS_VALID
);
280 * We provide an interface via debugfs to switch between host and device modes
281 * depending on user input.
282 * This is useful in special cases, such as uses TYPE-A receptacle but also
283 * wants to support dual-role mode.
285 void ssusb_mode_switch(struct ssusb_mtk
*ssusb
, int to_host
)
287 struct otg_switch_mtk
*otg_sx
= &ssusb
->otg_switch
;
290 ssusb_set_force_mode(ssusb
, MTU3_DR_FORCE_HOST
);
291 ssusb_set_mailbox(otg_sx
, MTU3_VBUS_OFF
);
292 ssusb_set_mailbox(otg_sx
, MTU3_ID_GROUND
);
294 ssusb_set_force_mode(ssusb
, MTU3_DR_FORCE_DEVICE
);
295 ssusb_set_mailbox(otg_sx
, MTU3_ID_FLOAT
);
296 ssusb_set_mailbox(otg_sx
, MTU3_VBUS_VALID
);
300 void ssusb_set_force_mode(struct ssusb_mtk
*ssusb
,
301 enum mtu3_dr_force_mode mode
)
305 value
= mtu3_readl(ssusb
->ippc_base
, SSUSB_U2_CTRL(0));
307 case MTU3_DR_FORCE_DEVICE
:
308 value
|= SSUSB_U2_PORT_FORCE_IDDIG
| SSUSB_U2_PORT_RG_IDDIG
;
310 case MTU3_DR_FORCE_HOST
:
311 value
|= SSUSB_U2_PORT_FORCE_IDDIG
;
312 value
&= ~SSUSB_U2_PORT_RG_IDDIG
;
314 case MTU3_DR_FORCE_NONE
:
315 value
&= ~(SSUSB_U2_PORT_FORCE_IDDIG
| SSUSB_U2_PORT_RG_IDDIG
);
320 mtu3_writel(ssusb
->ippc_base
, SSUSB_U2_CTRL(0), value
);
323 static int ssusb_role_sw_set(struct usb_role_switch
*sw
, enum usb_role role
)
325 struct ssusb_mtk
*ssusb
= usb_role_switch_get_drvdata(sw
);
326 bool to_host
= false;
328 if (role
== USB_ROLE_HOST
)
331 if (to_host
^ ssusb
->is_host
)
332 ssusb_mode_switch(ssusb
, to_host
);
337 static enum usb_role
ssusb_role_sw_get(struct usb_role_switch
*sw
)
339 struct ssusb_mtk
*ssusb
= usb_role_switch_get_drvdata(sw
);
342 role
= ssusb
->is_host
? USB_ROLE_HOST
: USB_ROLE_DEVICE
;
347 static int ssusb_role_sw_register(struct otg_switch_mtk
*otg_sx
)
349 struct usb_role_switch_desc role_sx_desc
= { 0 };
350 struct ssusb_mtk
*ssusb
=
351 container_of(otg_sx
, struct ssusb_mtk
, otg_switch
);
353 if (!otg_sx
->role_sw_used
)
356 role_sx_desc
.set
= ssusb_role_sw_set
;
357 role_sx_desc
.get
= ssusb_role_sw_get
;
358 role_sx_desc
.fwnode
= dev_fwnode(ssusb
->dev
);
359 role_sx_desc
.driver_data
= ssusb
;
360 otg_sx
->role_sw
= usb_role_switch_register(ssusb
->dev
, &role_sx_desc
);
362 return PTR_ERR_OR_ZERO(otg_sx
->role_sw
);
365 int ssusb_otg_switch_init(struct ssusb_mtk
*ssusb
)
367 struct otg_switch_mtk
*otg_sx
= &ssusb
->otg_switch
;
370 INIT_WORK(&otg_sx
->id_work
, ssusb_id_work
);
371 INIT_WORK(&otg_sx
->vbus_work
, ssusb_vbus_work
);
373 if (otg_sx
->manual_drd_enabled
)
374 ssusb_dr_debugfs_init(ssusb
);
375 else if (otg_sx
->role_sw_used
)
376 ret
= ssusb_role_sw_register(otg_sx
);
378 ret
= ssusb_extcon_register(otg_sx
);
383 void ssusb_otg_switch_exit(struct ssusb_mtk
*ssusb
)
385 struct otg_switch_mtk
*otg_sx
= &ssusb
->otg_switch
;
387 cancel_work_sync(&otg_sx
->id_work
);
388 cancel_work_sync(&otg_sx
->vbus_work
);
389 usb_role_switch_unregister(otg_sx
->role_sw
);