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 void ssusb_id_work(struct work_struct
*work
)
179 struct otg_switch_mtk
*otg_sx
=
180 container_of(work
, struct otg_switch_mtk
, id_work
);
182 if (otg_sx
->id_event
)
183 ssusb_set_mailbox(otg_sx
, MTU3_ID_GROUND
);
185 ssusb_set_mailbox(otg_sx
, MTU3_ID_FLOAT
);
188 static void ssusb_vbus_work(struct work_struct
*work
)
190 struct otg_switch_mtk
*otg_sx
=
191 container_of(work
, struct otg_switch_mtk
, vbus_work
);
193 if (otg_sx
->vbus_event
)
194 ssusb_set_mailbox(otg_sx
, MTU3_VBUS_VALID
);
196 ssusb_set_mailbox(otg_sx
, MTU3_VBUS_OFF
);
200 * @ssusb_id_notifier is called in atomic context, but @ssusb_set_mailbox
201 * may sleep, so use work queue here
203 static int ssusb_id_notifier(struct notifier_block
*nb
,
204 unsigned long event
, void *ptr
)
206 struct otg_switch_mtk
*otg_sx
=
207 container_of(nb
, struct otg_switch_mtk
, id_nb
);
209 otg_sx
->id_event
= event
;
210 schedule_work(&otg_sx
->id_work
);
215 static int ssusb_vbus_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
, vbus_nb
);
221 otg_sx
->vbus_event
= event
;
222 schedule_work(&otg_sx
->vbus_work
);
227 static int ssusb_extcon_register(struct otg_switch_mtk
*otg_sx
)
229 struct ssusb_mtk
*ssusb
=
230 container_of(otg_sx
, struct ssusb_mtk
, otg_switch
);
231 struct extcon_dev
*edev
= otg_sx
->edev
;
234 /* extcon is optional */
238 otg_sx
->vbus_nb
.notifier_call
= ssusb_vbus_notifier
;
239 ret
= devm_extcon_register_notifier(ssusb
->dev
, edev
, EXTCON_USB
,
242 dev_err(ssusb
->dev
, "failed to register notifier for USB\n");
244 otg_sx
->id_nb
.notifier_call
= ssusb_id_notifier
;
245 ret
= devm_extcon_register_notifier(ssusb
->dev
, edev
, EXTCON_USB_HOST
,
248 dev_err(ssusb
->dev
, "failed to register notifier for USB-HOST\n");
250 dev_dbg(ssusb
->dev
, "EXTCON_USB: %d, EXTCON_USB_HOST: %d\n",
251 extcon_get_state(edev
, EXTCON_USB
),
252 extcon_get_state(edev
, EXTCON_USB_HOST
));
254 /* default as host, switch to device mode if needed */
255 if (extcon_get_state(edev
, EXTCON_USB_HOST
) == false)
256 ssusb_set_mailbox(otg_sx
, MTU3_ID_FLOAT
);
257 if (extcon_get_state(edev
, EXTCON_USB
) == true)
258 ssusb_set_mailbox(otg_sx
, MTU3_VBUS_VALID
);
264 * We provide an interface via debugfs to switch between host and device modes
265 * depending on user input.
266 * This is useful in special cases, such as uses TYPE-A receptacle but also
267 * wants to support dual-role mode.
269 static void ssusb_mode_manual_switch(struct ssusb_mtk
*ssusb
, int to_host
)
271 struct otg_switch_mtk
*otg_sx
= &ssusb
->otg_switch
;
274 ssusb_set_force_mode(ssusb
, MTU3_DR_FORCE_HOST
);
275 ssusb_set_mailbox(otg_sx
, MTU3_VBUS_OFF
);
276 ssusb_set_mailbox(otg_sx
, MTU3_ID_GROUND
);
278 ssusb_set_force_mode(ssusb
, MTU3_DR_FORCE_DEVICE
);
279 ssusb_set_mailbox(otg_sx
, MTU3_ID_FLOAT
);
280 ssusb_set_mailbox(otg_sx
, MTU3_VBUS_VALID
);
284 static int ssusb_mode_show(struct seq_file
*sf
, void *unused
)
286 struct ssusb_mtk
*ssusb
= sf
->private;
288 seq_printf(sf
, "current mode: %s(%s drd)\n(echo device/host)\n",
289 ssusb
->is_host
? "host" : "device",
290 ssusb
->otg_switch
.manual_drd_enabled
? "manual" : "auto");
295 static int ssusb_mode_open(struct inode
*inode
, struct file
*file
)
297 return single_open(file
, ssusb_mode_show
, inode
->i_private
);
300 static ssize_t
ssusb_mode_write(struct file
*file
,
301 const char __user
*ubuf
, size_t count
, loff_t
*ppos
)
303 struct seq_file
*sf
= file
->private_data
;
304 struct ssusb_mtk
*ssusb
= sf
->private;
307 if (copy_from_user(&buf
, ubuf
, min_t(size_t, sizeof(buf
) - 1, count
)))
310 if (!strncmp(buf
, "host", 4) && !ssusb
->is_host
) {
311 ssusb_mode_manual_switch(ssusb
, 1);
312 } else if (!strncmp(buf
, "device", 6) && ssusb
->is_host
) {
313 ssusb_mode_manual_switch(ssusb
, 0);
315 dev_err(ssusb
->dev
, "wrong or duplicated setting\n");
322 static const struct file_operations ssusb_mode_fops
= {
323 .open
= ssusb_mode_open
,
324 .write
= ssusb_mode_write
,
327 .release
= single_release
,
330 static int ssusb_vbus_show(struct seq_file
*sf
, void *unused
)
332 struct ssusb_mtk
*ssusb
= sf
->private;
333 struct otg_switch_mtk
*otg_sx
= &ssusb
->otg_switch
;
335 seq_printf(sf
, "vbus state: %s\n(echo on/off)\n",
336 regulator_is_enabled(otg_sx
->vbus
) ? "on" : "off");
341 static int ssusb_vbus_open(struct inode
*inode
, struct file
*file
)
343 return single_open(file
, ssusb_vbus_show
, inode
->i_private
);
346 static ssize_t
ssusb_vbus_write(struct file
*file
,
347 const char __user
*ubuf
, size_t count
, loff_t
*ppos
)
349 struct seq_file
*sf
= file
->private_data
;
350 struct ssusb_mtk
*ssusb
= sf
->private;
351 struct otg_switch_mtk
*otg_sx
= &ssusb
->otg_switch
;
355 if (copy_from_user(&buf
, ubuf
, min_t(size_t, sizeof(buf
) - 1, count
)))
358 if (kstrtobool(buf
, &enable
)) {
359 dev_err(ssusb
->dev
, "wrong setting\n");
363 ssusb_set_vbus(otg_sx
, enable
);
368 static const struct file_operations ssusb_vbus_fops
= {
369 .open
= ssusb_vbus_open
,
370 .write
= ssusb_vbus_write
,
373 .release
= single_release
,
376 static void ssusb_debugfs_init(struct ssusb_mtk
*ssusb
)
380 root
= debugfs_create_dir(dev_name(ssusb
->dev
), usb_debug_root
);
381 ssusb
->dbgfs_root
= root
;
383 debugfs_create_file("mode", 0644, root
, ssusb
, &ssusb_mode_fops
);
384 debugfs_create_file("vbus", 0644, root
, ssusb
, &ssusb_vbus_fops
);
387 static void ssusb_debugfs_exit(struct ssusb_mtk
*ssusb
)
389 debugfs_remove_recursive(ssusb
->dbgfs_root
);
392 void ssusb_set_force_mode(struct ssusb_mtk
*ssusb
,
393 enum mtu3_dr_force_mode mode
)
397 value
= mtu3_readl(ssusb
->ippc_base
, SSUSB_U2_CTRL(0));
399 case MTU3_DR_FORCE_DEVICE
:
400 value
|= SSUSB_U2_PORT_FORCE_IDDIG
| SSUSB_U2_PORT_RG_IDDIG
;
402 case MTU3_DR_FORCE_HOST
:
403 value
|= SSUSB_U2_PORT_FORCE_IDDIG
;
404 value
&= ~SSUSB_U2_PORT_RG_IDDIG
;
406 case MTU3_DR_FORCE_NONE
:
407 value
&= ~(SSUSB_U2_PORT_FORCE_IDDIG
| SSUSB_U2_PORT_RG_IDDIG
);
412 mtu3_writel(ssusb
->ippc_base
, SSUSB_U2_CTRL(0), value
);
415 int ssusb_otg_switch_init(struct ssusb_mtk
*ssusb
)
417 struct otg_switch_mtk
*otg_sx
= &ssusb
->otg_switch
;
419 INIT_WORK(&otg_sx
->id_work
, ssusb_id_work
);
420 INIT_WORK(&otg_sx
->vbus_work
, ssusb_vbus_work
);
422 if (otg_sx
->manual_drd_enabled
)
423 ssusb_debugfs_init(ssusb
);
425 ssusb_extcon_register(otg_sx
);
430 void ssusb_otg_switch_exit(struct ssusb_mtk
*ssusb
)
432 struct otg_switch_mtk
*otg_sx
= &ssusb
->otg_switch
;
434 if (otg_sx
->manual_drd_enabled
)
435 ssusb_debugfs_exit(ssusb
);
437 cancel_work_sync(&otg_sx
->id_work
);
438 cancel_work_sync(&otg_sx
->vbus_work
);