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/debugfs.h>
11 #include <linux/irq.h>
12 #include <linux/kernel.h>
13 #include <linux/of_device.h>
14 #include <linux/pinctrl/consumer.h>
15 #include <linux/seq_file.h>
16 #include <linux/uaccess.h>
24 enum mtu3_vbus_id_state
{
31 static void toggle_opstate(struct ssusb_mtk
*ssusb
)
33 if (!ssusb
->otg_switch
.is_u3_drd
) {
34 mtu3_setbits(ssusb
->mac_base
, U3D_DEVICE_CONTROL
, DC_SESSION
);
35 mtu3_setbits(ssusb
->mac_base
, U3D_POWER_MANAGEMENT
, SOFT_CONN
);
39 /* only port0 supports dual-role mode */
40 static int ssusb_port0_switch(struct ssusb_mtk
*ssusb
,
41 int version
, bool tohost
)
43 void __iomem
*ibase
= ssusb
->ippc_base
;
46 dev_dbg(ssusb
->dev
, "%s (switch u%d port0 to %s)\n", __func__
,
47 version
, tohost
? "host" : "device");
49 if (version
== USB2_PORT
) {
50 /* 1. power off and disable u2 port0 */
51 value
= mtu3_readl(ibase
, SSUSB_U2_CTRL(0));
52 value
|= SSUSB_U2_PORT_PDN
| SSUSB_U2_PORT_DIS
;
53 mtu3_writel(ibase
, SSUSB_U2_CTRL(0), value
);
55 /* 2. power on, enable u2 port0 and select its mode */
56 value
= mtu3_readl(ibase
, SSUSB_U2_CTRL(0));
57 value
&= ~(SSUSB_U2_PORT_PDN
| SSUSB_U2_PORT_DIS
);
58 value
= tohost
? (value
| SSUSB_U2_PORT_HOST_SEL
) :
59 (value
& (~SSUSB_U2_PORT_HOST_SEL
));
60 mtu3_writel(ibase
, SSUSB_U2_CTRL(0), value
);
62 /* 1. power off and disable u3 port0 */
63 value
= mtu3_readl(ibase
, SSUSB_U3_CTRL(0));
64 value
|= SSUSB_U3_PORT_PDN
| SSUSB_U3_PORT_DIS
;
65 mtu3_writel(ibase
, SSUSB_U3_CTRL(0), value
);
67 /* 2. power on, enable u3 port0 and select its mode */
68 value
= mtu3_readl(ibase
, SSUSB_U3_CTRL(0));
69 value
&= ~(SSUSB_U3_PORT_PDN
| SSUSB_U3_PORT_DIS
);
70 value
= tohost
? (value
| SSUSB_U3_PORT_HOST_SEL
) :
71 (value
& (~SSUSB_U3_PORT_HOST_SEL
));
72 mtu3_writel(ibase
, SSUSB_U3_CTRL(0), value
);
78 static void switch_port_to_host(struct ssusb_mtk
*ssusb
)
82 dev_dbg(ssusb
->dev
, "%s\n", __func__
);
84 ssusb_port0_switch(ssusb
, USB2_PORT
, true);
86 if (ssusb
->otg_switch
.is_u3_drd
) {
87 ssusb_port0_switch(ssusb
, USB3_PORT
, true);
88 check_clk
= SSUSB_U3_MAC_RST_B_STS
;
91 ssusb_check_clocks(ssusb
, check_clk
);
93 /* after all clocks are stable */
94 toggle_opstate(ssusb
);
97 static void switch_port_to_device(struct ssusb_mtk
*ssusb
)
101 dev_dbg(ssusb
->dev
, "%s\n", __func__
);
103 ssusb_port0_switch(ssusb
, USB2_PORT
, false);
105 if (ssusb
->otg_switch
.is_u3_drd
) {
106 ssusb_port0_switch(ssusb
, USB3_PORT
, false);
107 check_clk
= SSUSB_U3_MAC_RST_B_STS
;
110 ssusb_check_clocks(ssusb
, check_clk
);
113 int ssusb_set_vbus(struct otg_switch_mtk
*otg_sx
, int is_on
)
115 struct ssusb_mtk
*ssusb
=
116 container_of(otg_sx
, struct ssusb_mtk
, otg_switch
);
117 struct regulator
*vbus
= otg_sx
->vbus
;
120 /* vbus is optional */
124 dev_dbg(ssusb
->dev
, "%s: turn %s\n", __func__
, is_on
? "on" : "off");
127 ret
= regulator_enable(vbus
);
129 dev_err(ssusb
->dev
, "vbus regulator enable failed\n");
133 regulator_disable(vbus
);
140 * switch to host: -> MTU3_VBUS_OFF --> MTU3_ID_GROUND
141 * switch to device: -> MTU3_ID_FLOAT --> MTU3_VBUS_VALID
143 static void ssusb_set_mailbox(struct otg_switch_mtk
*otg_sx
,
144 enum mtu3_vbus_id_state status
)
146 struct ssusb_mtk
*ssusb
=
147 container_of(otg_sx
, struct ssusb_mtk
, otg_switch
);
148 struct mtu3
*mtu
= ssusb
->u3d
;
150 dev_dbg(ssusb
->dev
, "mailbox state(%d)\n", status
);
154 switch_port_to_host(ssusb
);
155 ssusb_set_vbus(otg_sx
, 1);
156 ssusb
->is_host
= true;
159 ssusb
->is_host
= false;
160 ssusb_set_vbus(otg_sx
, 0);
161 switch_port_to_device(ssusb
);
165 pm_relax(ssusb
->dev
);
167 case MTU3_VBUS_VALID
:
168 /* avoid suspend when works as device */
169 pm_stay_awake(ssusb
->dev
);
173 dev_err(ssusb
->dev
, "invalid state\n");
177 static int ssusb_id_notifier(struct notifier_block
*nb
,
178 unsigned long event
, void *ptr
)
180 struct otg_switch_mtk
*otg_sx
=
181 container_of(nb
, struct otg_switch_mtk
, id_nb
);
184 ssusb_set_mailbox(otg_sx
, MTU3_ID_GROUND
);
186 ssusb_set_mailbox(otg_sx
, MTU3_ID_FLOAT
);
191 static int ssusb_vbus_notifier(struct notifier_block
*nb
,
192 unsigned long event
, void *ptr
)
194 struct otg_switch_mtk
*otg_sx
=
195 container_of(nb
, struct otg_switch_mtk
, vbus_nb
);
198 ssusb_set_mailbox(otg_sx
, MTU3_VBUS_VALID
);
200 ssusb_set_mailbox(otg_sx
, MTU3_VBUS_OFF
);
205 static int ssusb_extcon_register(struct otg_switch_mtk
*otg_sx
)
207 struct ssusb_mtk
*ssusb
=
208 container_of(otg_sx
, struct ssusb_mtk
, otg_switch
);
209 struct extcon_dev
*edev
= otg_sx
->edev
;
212 /* extcon is optional */
216 otg_sx
->vbus_nb
.notifier_call
= ssusb_vbus_notifier
;
217 ret
= devm_extcon_register_notifier(ssusb
->dev
, edev
, EXTCON_USB
,
220 dev_err(ssusb
->dev
, "failed to register notifier for USB\n");
222 otg_sx
->id_nb
.notifier_call
= ssusb_id_notifier
;
223 ret
= devm_extcon_register_notifier(ssusb
->dev
, edev
, EXTCON_USB_HOST
,
226 dev_err(ssusb
->dev
, "failed to register notifier for USB-HOST\n");
228 dev_dbg(ssusb
->dev
, "EXTCON_USB: %d, EXTCON_USB_HOST: %d\n",
229 extcon_get_state(edev
, EXTCON_USB
),
230 extcon_get_state(edev
, EXTCON_USB_HOST
));
232 /* default as host, switch to device mode if needed */
233 if (extcon_get_state(edev
, EXTCON_USB_HOST
) == false)
234 ssusb_set_mailbox(otg_sx
, MTU3_ID_FLOAT
);
235 if (extcon_get_state(edev
, EXTCON_USB
) == true)
236 ssusb_set_mailbox(otg_sx
, MTU3_VBUS_VALID
);
241 static void extcon_register_dwork(struct work_struct
*work
)
243 struct delayed_work
*dwork
= to_delayed_work(work
);
244 struct otg_switch_mtk
*otg_sx
=
245 container_of(dwork
, struct otg_switch_mtk
, extcon_reg_dwork
);
247 ssusb_extcon_register(otg_sx
);
251 * We provide an interface via debugfs to switch between host and device modes
252 * depending on user input.
253 * This is useful in special cases, such as uses TYPE-A receptacle but also
254 * wants to support dual-role mode.
256 static void ssusb_mode_manual_switch(struct ssusb_mtk
*ssusb
, int to_host
)
258 struct otg_switch_mtk
*otg_sx
= &ssusb
->otg_switch
;
261 ssusb_set_force_mode(ssusb
, MTU3_DR_FORCE_HOST
);
262 ssusb_set_mailbox(otg_sx
, MTU3_VBUS_OFF
);
263 ssusb_set_mailbox(otg_sx
, MTU3_ID_GROUND
);
265 ssusb_set_force_mode(ssusb
, MTU3_DR_FORCE_DEVICE
);
266 ssusb_set_mailbox(otg_sx
, MTU3_ID_FLOAT
);
267 ssusb_set_mailbox(otg_sx
, MTU3_VBUS_VALID
);
271 static int ssusb_mode_show(struct seq_file
*sf
, void *unused
)
273 struct ssusb_mtk
*ssusb
= sf
->private;
275 seq_printf(sf
, "current mode: %s(%s drd)\n(echo device/host)\n",
276 ssusb
->is_host
? "host" : "device",
277 ssusb
->otg_switch
.manual_drd_enabled
? "manual" : "auto");
282 static int ssusb_mode_open(struct inode
*inode
, struct file
*file
)
284 return single_open(file
, ssusb_mode_show
, inode
->i_private
);
287 static ssize_t
ssusb_mode_write(struct file
*file
,
288 const char __user
*ubuf
, size_t count
, loff_t
*ppos
)
290 struct seq_file
*sf
= file
->private_data
;
291 struct ssusb_mtk
*ssusb
= sf
->private;
294 if (copy_from_user(&buf
, ubuf
, min_t(size_t, sizeof(buf
) - 1, count
)))
297 if (!strncmp(buf
, "host", 4) && !ssusb
->is_host
) {
298 ssusb_mode_manual_switch(ssusb
, 1);
299 } else if (!strncmp(buf
, "device", 6) && ssusb
->is_host
) {
300 ssusb_mode_manual_switch(ssusb
, 0);
302 dev_err(ssusb
->dev
, "wrong or duplicated setting\n");
309 static const struct file_operations ssusb_mode_fops
= {
310 .open
= ssusb_mode_open
,
311 .write
= ssusb_mode_write
,
314 .release
= single_release
,
317 static int ssusb_vbus_show(struct seq_file
*sf
, void *unused
)
319 struct ssusb_mtk
*ssusb
= sf
->private;
320 struct otg_switch_mtk
*otg_sx
= &ssusb
->otg_switch
;
322 seq_printf(sf
, "vbus state: %s\n(echo on/off)\n",
323 regulator_is_enabled(otg_sx
->vbus
) ? "on" : "off");
328 static int ssusb_vbus_open(struct inode
*inode
, struct file
*file
)
330 return single_open(file
, ssusb_vbus_show
, inode
->i_private
);
333 static ssize_t
ssusb_vbus_write(struct file
*file
,
334 const char __user
*ubuf
, size_t count
, loff_t
*ppos
)
336 struct seq_file
*sf
= file
->private_data
;
337 struct ssusb_mtk
*ssusb
= sf
->private;
338 struct otg_switch_mtk
*otg_sx
= &ssusb
->otg_switch
;
342 if (copy_from_user(&buf
, ubuf
, min_t(size_t, sizeof(buf
) - 1, count
)))
345 if (kstrtobool(buf
, &enable
)) {
346 dev_err(ssusb
->dev
, "wrong setting\n");
350 ssusb_set_vbus(otg_sx
, enable
);
355 static const struct file_operations ssusb_vbus_fops
= {
356 .open
= ssusb_vbus_open
,
357 .write
= ssusb_vbus_write
,
360 .release
= single_release
,
363 static void ssusb_debugfs_init(struct ssusb_mtk
*ssusb
)
367 root
= debugfs_create_dir(dev_name(ssusb
->dev
), usb_debug_root
);
369 dev_err(ssusb
->dev
, "create debugfs root failed\n");
372 ssusb
->dbgfs_root
= root
;
374 debugfs_create_file("mode", 0644, root
, ssusb
, &ssusb_mode_fops
);
375 debugfs_create_file("vbus", 0644, root
, ssusb
, &ssusb_vbus_fops
);
378 static void ssusb_debugfs_exit(struct ssusb_mtk
*ssusb
)
380 debugfs_remove_recursive(ssusb
->dbgfs_root
);
383 void ssusb_set_force_mode(struct ssusb_mtk
*ssusb
,
384 enum mtu3_dr_force_mode mode
)
388 value
= mtu3_readl(ssusb
->ippc_base
, SSUSB_U2_CTRL(0));
390 case MTU3_DR_FORCE_DEVICE
:
391 value
|= SSUSB_U2_PORT_FORCE_IDDIG
| SSUSB_U2_PORT_RG_IDDIG
;
393 case MTU3_DR_FORCE_HOST
:
394 value
|= SSUSB_U2_PORT_FORCE_IDDIG
;
395 value
&= ~SSUSB_U2_PORT_RG_IDDIG
;
397 case MTU3_DR_FORCE_NONE
:
398 value
&= ~(SSUSB_U2_PORT_FORCE_IDDIG
| SSUSB_U2_PORT_RG_IDDIG
);
403 mtu3_writel(ssusb
->ippc_base
, SSUSB_U2_CTRL(0), value
);
406 int ssusb_otg_switch_init(struct ssusb_mtk
*ssusb
)
408 struct otg_switch_mtk
*otg_sx
= &ssusb
->otg_switch
;
410 if (otg_sx
->manual_drd_enabled
) {
411 ssusb_debugfs_init(ssusb
);
413 INIT_DELAYED_WORK(&otg_sx
->extcon_reg_dwork
,
414 extcon_register_dwork
);
417 * It is enough to delay 1s for waiting for
418 * host initialization
420 schedule_delayed_work(&otg_sx
->extcon_reg_dwork
, HZ
);
426 void ssusb_otg_switch_exit(struct ssusb_mtk
*ssusb
)
428 struct otg_switch_mtk
*otg_sx
= &ssusb
->otg_switch
;
430 if (otg_sx
->manual_drd_enabled
)
431 ssusb_debugfs_exit(ssusb
);
433 cancel_delayed_work(&otg_sx
->extcon_reg_dwork
);