2017-11-03 18:28:30 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-10-19 10:28:26 +08:00
|
|
|
/*
|
|
|
|
* mtu3_dr.c - dual role switch and host glue layer
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 MediaTek Inc.
|
|
|
|
*
|
|
|
|
* Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mtu3.h"
|
|
|
|
#include "mtu3_dr.h"
|
2019-03-21 10:53:47 +08:00
|
|
|
#include "mtu3_debug.h"
|
2016-10-19 10:28:26 +08:00
|
|
|
|
|
|
|
#define USB2_PORT 2
|
|
|
|
#define USB3_PORT 3
|
|
|
|
|
2021-06-08 15:57:37 +08:00
|
|
|
static inline struct ssusb_mtk *otg_sx_to_ssusb(struct otg_switch_mtk *otg_sx)
|
|
|
|
{
|
|
|
|
return container_of(otg_sx, struct ssusb_mtk, otg_switch);
|
|
|
|
}
|
|
|
|
|
2016-10-19 10:28:26 +08:00
|
|
|
static void toggle_opstate(struct ssusb_mtk *ssusb)
|
|
|
|
{
|
2022-04-19 16:12:45 +08:00
|
|
|
mtu3_setbits(ssusb->mac_base, U3D_DEVICE_CONTROL, DC_SESSION);
|
|
|
|
mtu3_setbits(ssusb->mac_base, U3D_POWER_MANAGEMENT, SOFT_CONN);
|
2016-10-19 10:28:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* only port0 supports dual-role mode */
|
|
|
|
static int ssusb_port0_switch(struct ssusb_mtk *ssusb,
|
|
|
|
int version, bool tohost)
|
|
|
|
{
|
|
|
|
void __iomem *ibase = ssusb->ippc_base;
|
|
|
|
u32 value;
|
|
|
|
|
|
|
|
dev_dbg(ssusb->dev, "%s (switch u%d port0 to %s)\n", __func__,
|
|
|
|
version, tohost ? "host" : "device");
|
|
|
|
|
|
|
|
if (version == USB2_PORT) {
|
|
|
|
/* 1. power off and disable u2 port0 */
|
|
|
|
value = mtu3_readl(ibase, SSUSB_U2_CTRL(0));
|
|
|
|
value |= SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS;
|
|
|
|
mtu3_writel(ibase, SSUSB_U2_CTRL(0), value);
|
|
|
|
|
|
|
|
/* 2. power on, enable u2 port0 and select its mode */
|
|
|
|
value = mtu3_readl(ibase, SSUSB_U2_CTRL(0));
|
|
|
|
value &= ~(SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS);
|
|
|
|
value = tohost ? (value | SSUSB_U2_PORT_HOST_SEL) :
|
|
|
|
(value & (~SSUSB_U2_PORT_HOST_SEL));
|
|
|
|
mtu3_writel(ibase, SSUSB_U2_CTRL(0), value);
|
|
|
|
} else {
|
|
|
|
/* 1. power off and disable u3 port0 */
|
|
|
|
value = mtu3_readl(ibase, SSUSB_U3_CTRL(0));
|
|
|
|
value |= SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS;
|
|
|
|
mtu3_writel(ibase, SSUSB_U3_CTRL(0), value);
|
|
|
|
|
|
|
|
/* 2. power on, enable u3 port0 and select its mode */
|
|
|
|
value = mtu3_readl(ibase, SSUSB_U3_CTRL(0));
|
|
|
|
value &= ~(SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS);
|
|
|
|
value = tohost ? (value | SSUSB_U3_PORT_HOST_SEL) :
|
|
|
|
(value & (~SSUSB_U3_PORT_HOST_SEL));
|
|
|
|
mtu3_writel(ibase, SSUSB_U3_CTRL(0), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void switch_port_to_host(struct ssusb_mtk *ssusb)
|
|
|
|
{
|
|
|
|
u32 check_clk = 0;
|
|
|
|
|
|
|
|
dev_dbg(ssusb->dev, "%s\n", __func__);
|
|
|
|
|
|
|
|
ssusb_port0_switch(ssusb, USB2_PORT, true);
|
|
|
|
|
|
|
|
if (ssusb->otg_switch.is_u3_drd) {
|
|
|
|
ssusb_port0_switch(ssusb, USB3_PORT, true);
|
|
|
|
check_clk = SSUSB_U3_MAC_RST_B_STS;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssusb_check_clocks(ssusb, check_clk);
|
|
|
|
|
|
|
|
/* after all clocks are stable */
|
|
|
|
toggle_opstate(ssusb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void switch_port_to_device(struct ssusb_mtk *ssusb)
|
|
|
|
{
|
|
|
|
u32 check_clk = 0;
|
|
|
|
|
|
|
|
dev_dbg(ssusb->dev, "%s\n", __func__);
|
|
|
|
|
|
|
|
ssusb_port0_switch(ssusb, USB2_PORT, false);
|
|
|
|
|
|
|
|
if (ssusb->otg_switch.is_u3_drd) {
|
|
|
|
ssusb_port0_switch(ssusb, USB3_PORT, false);
|
|
|
|
check_clk = SSUSB_U3_MAC_RST_B_STS;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssusb_check_clocks(ssusb, check_clk);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ssusb_set_vbus(struct otg_switch_mtk *otg_sx, int is_on)
|
|
|
|
{
|
2021-06-08 15:57:37 +08:00
|
|
|
struct ssusb_mtk *ssusb = otg_sx_to_ssusb(otg_sx);
|
2016-10-19 10:28:26 +08:00
|
|
|
struct regulator *vbus = otg_sx->vbus;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* vbus is optional */
|
|
|
|
if (!vbus)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
dev_dbg(ssusb->dev, "%s: turn %s\n", __func__, is_on ? "on" : "off");
|
|
|
|
|
|
|
|
if (is_on) {
|
|
|
|
ret = regulator_enable(vbus);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(ssusb->dev, "vbus regulator enable failed\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
regulator_disable(vbus);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-08 15:57:36 +08:00
|
|
|
static void ssusb_mode_sw_work(struct work_struct *work)
|
2016-10-19 10:28:26 +08:00
|
|
|
{
|
2021-06-08 15:57:36 +08:00
|
|
|
struct otg_switch_mtk *otg_sx =
|
|
|
|
container_of(work, struct otg_switch_mtk, dr_work);
|
2021-06-08 15:57:37 +08:00
|
|
|
struct ssusb_mtk *ssusb = otg_sx_to_ssusb(otg_sx);
|
2016-10-19 10:28:26 +08:00
|
|
|
struct mtu3 *mtu = ssusb->u3d;
|
2021-06-08 15:57:36 +08:00
|
|
|
enum usb_role desired_role = otg_sx->desired_role;
|
|
|
|
enum usb_role current_role;
|
|
|
|
|
|
|
|
current_role = ssusb->is_host ? USB_ROLE_HOST : USB_ROLE_DEVICE;
|
|
|
|
|
2021-07-15 17:07:52 +08:00
|
|
|
if (desired_role == USB_ROLE_NONE) {
|
|
|
|
/* the default mode is host as probe does */
|
2021-06-08 15:57:36 +08:00
|
|
|
desired_role = USB_ROLE_HOST;
|
2021-07-15 17:07:52 +08:00
|
|
|
if (otg_sx->default_role == USB_ROLE_DEVICE)
|
|
|
|
desired_role = USB_ROLE_DEVICE;
|
|
|
|
}
|
2016-10-19 10:28:26 +08:00
|
|
|
|
2021-06-08 15:57:36 +08:00
|
|
|
if (current_role == desired_role)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dev_dbg(ssusb->dev, "set role : %s\n", usb_role_string(desired_role));
|
|
|
|
mtu3_dbg_trace(ssusb->dev, "set role : %s", usb_role_string(desired_role));
|
2021-07-15 17:07:58 +08:00
|
|
|
pm_runtime_get_sync(ssusb->dev);
|
2016-10-19 10:28:26 +08:00
|
|
|
|
2021-06-08 15:57:36 +08:00
|
|
|
switch (desired_role) {
|
2021-06-08 15:57:35 +08:00
|
|
|
case USB_ROLE_HOST:
|
2021-06-08 15:57:38 +08:00
|
|
|
ssusb_set_force_mode(ssusb, MTU3_DR_FORCE_HOST);
|
2021-06-08 15:57:34 +08:00
|
|
|
mtu3_stop(mtu);
|
2016-10-19 10:28:26 +08:00
|
|
|
switch_port_to_host(ssusb);
|
|
|
|
ssusb_set_vbus(otg_sx, 1);
|
|
|
|
ssusb->is_host = true;
|
|
|
|
break;
|
2021-06-08 15:57:35 +08:00
|
|
|
case USB_ROLE_DEVICE:
|
2021-06-08 15:57:38 +08:00
|
|
|
ssusb_set_force_mode(ssusb, MTU3_DR_FORCE_DEVICE);
|
2016-10-19 10:28:26 +08:00
|
|
|
ssusb->is_host = false;
|
|
|
|
ssusb_set_vbus(otg_sx, 0);
|
|
|
|
switch_port_to_device(ssusb);
|
|
|
|
mtu3_start(mtu);
|
|
|
|
break;
|
2021-06-08 15:57:35 +08:00
|
|
|
case USB_ROLE_NONE:
|
2016-10-19 10:28:26 +08:00
|
|
|
default:
|
2021-06-08 15:57:35 +08:00
|
|
|
dev_err(ssusb->dev, "invalid role\n");
|
2016-10-19 10:28:26 +08:00
|
|
|
}
|
2021-07-15 17:07:58 +08:00
|
|
|
pm_runtime_put(ssusb->dev);
|
2016-10-19 10:28:26 +08:00
|
|
|
}
|
|
|
|
|
2021-06-08 15:57:36 +08:00
|
|
|
static void ssusb_set_mode(struct otg_switch_mtk *otg_sx, enum usb_role role)
|
2016-10-19 10:28:26 +08:00
|
|
|
{
|
2021-06-08 15:57:37 +08:00
|
|
|
struct ssusb_mtk *ssusb = otg_sx_to_ssusb(otg_sx);
|
2016-10-19 10:28:26 +08:00
|
|
|
|
2021-06-08 15:57:36 +08:00
|
|
|
if (ssusb->dr_mode != USB_DR_MODE_OTG)
|
|
|
|
return;
|
|
|
|
|
|
|
|
otg_sx->desired_role = role;
|
|
|
|
queue_work(system_freezable_wq, &otg_sx->dr_work);
|
2018-05-23 16:53:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ssusb_id_notifier(struct notifier_block *nb,
|
|
|
|
unsigned long event, void *ptr)
|
|
|
|
{
|
|
|
|
struct otg_switch_mtk *otg_sx =
|
|
|
|
container_of(nb, struct otg_switch_mtk, id_nb);
|
|
|
|
|
2021-06-08 15:57:36 +08:00
|
|
|
ssusb_set_mode(otg_sx, event ? USB_ROLE_HOST : USB_ROLE_DEVICE);
|
2016-10-19 10:28:26 +08:00
|
|
|
|
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ssusb_extcon_register(struct otg_switch_mtk *otg_sx)
|
|
|
|
{
|
2021-06-08 15:57:37 +08:00
|
|
|
struct ssusb_mtk *ssusb = otg_sx_to_ssusb(otg_sx);
|
2016-10-19 10:28:26 +08:00
|
|
|
struct extcon_dev *edev = otg_sx->edev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* extcon is optional */
|
|
|
|
if (!edev)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
otg_sx->id_nb.notifier_call = ssusb_id_notifier;
|
2017-03-28 12:42:02 +08:00
|
|
|
ret = devm_extcon_register_notifier(ssusb->dev, edev, EXTCON_USB_HOST,
|
2016-10-19 10:28:26 +08:00
|
|
|
&otg_sx->id_nb);
|
2019-03-21 10:53:39 +08:00
|
|
|
if (ret < 0) {
|
2016-10-19 10:28:26 +08:00
|
|
|
dev_err(ssusb->dev, "failed to register notifier for USB-HOST\n");
|
2019-03-21 10:53:39 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2016-10-19 10:28:26 +08:00
|
|
|
|
2021-06-08 15:57:36 +08:00
|
|
|
ret = extcon_get_state(edev, EXTCON_USB_HOST);
|
|
|
|
dev_dbg(ssusb->dev, "EXTCON_USB_HOST: %d\n", ret);
|
2016-10-19 10:28:26 +08:00
|
|
|
|
|
|
|
/* default as host, switch to device mode if needed */
|
2021-06-08 15:57:36 +08:00
|
|
|
if (!ret)
|
|
|
|
ssusb_set_mode(otg_sx, USB_ROLE_DEVICE);
|
2016-10-19 10:28:26 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We provide an interface via debugfs to switch between host and device modes
|
|
|
|
* depending on user input.
|
|
|
|
* This is useful in special cases, such as uses TYPE-A receptacle but also
|
|
|
|
* wants to support dual-role mode.
|
|
|
|
*/
|
2019-08-29 17:22:38 +08:00
|
|
|
void ssusb_mode_switch(struct ssusb_mtk *ssusb, int to_host)
|
2016-10-19 10:28:26 +08:00
|
|
|
{
|
|
|
|
struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
|
|
|
|
|
2021-06-08 15:57:38 +08:00
|
|
|
ssusb_set_mode(otg_sx, to_host ? USB_ROLE_HOST : USB_ROLE_DEVICE);
|
2016-10-19 10:28:26 +08:00
|
|
|
}
|
|
|
|
|
2017-10-13 17:10:42 +08:00
|
|
|
void ssusb_set_force_mode(struct ssusb_mtk *ssusb,
|
|
|
|
enum mtu3_dr_force_mode mode)
|
|
|
|
{
|
|
|
|
u32 value;
|
|
|
|
|
|
|
|
value = mtu3_readl(ssusb->ippc_base, SSUSB_U2_CTRL(0));
|
|
|
|
switch (mode) {
|
|
|
|
case MTU3_DR_FORCE_DEVICE:
|
|
|
|
value |= SSUSB_U2_PORT_FORCE_IDDIG | SSUSB_U2_PORT_RG_IDDIG;
|
|
|
|
break;
|
|
|
|
case MTU3_DR_FORCE_HOST:
|
|
|
|
value |= SSUSB_U2_PORT_FORCE_IDDIG;
|
|
|
|
value &= ~SSUSB_U2_PORT_RG_IDDIG;
|
|
|
|
break;
|
|
|
|
case MTU3_DR_FORCE_NONE:
|
|
|
|
value &= ~(SSUSB_U2_PORT_FORCE_IDDIG | SSUSB_U2_PORT_RG_IDDIG);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mtu3_writel(ssusb->ippc_base, SSUSB_U2_CTRL(0), value);
|
|
|
|
}
|
|
|
|
|
2020-03-02 21:53:49 +08:00
|
|
|
static int ssusb_role_sw_set(struct usb_role_switch *sw, enum usb_role role)
|
2019-08-29 17:22:38 +08:00
|
|
|
{
|
2020-03-02 21:53:49 +08:00
|
|
|
struct ssusb_mtk *ssusb = usb_role_switch_get_drvdata(sw);
|
2021-06-08 15:57:39 +08:00
|
|
|
struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
|
2019-08-29 17:22:38 +08:00
|
|
|
|
2021-06-08 15:57:39 +08:00
|
|
|
ssusb_set_mode(otg_sx, role);
|
2019-08-29 17:22:38 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-02 21:53:49 +08:00
|
|
|
static enum usb_role ssusb_role_sw_get(struct usb_role_switch *sw)
|
2019-08-29 17:22:38 +08:00
|
|
|
{
|
2020-03-02 21:53:49 +08:00
|
|
|
struct ssusb_mtk *ssusb = usb_role_switch_get_drvdata(sw);
|
2019-08-29 17:22:38 +08:00
|
|
|
|
2021-06-08 15:57:39 +08:00
|
|
|
return ssusb->is_host ? USB_ROLE_HOST : USB_ROLE_DEVICE;
|
2019-08-29 17:22:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ssusb_role_sw_register(struct otg_switch_mtk *otg_sx)
|
|
|
|
{
|
|
|
|
struct usb_role_switch_desc role_sx_desc = { 0 };
|
2021-06-08 15:57:37 +08:00
|
|
|
struct ssusb_mtk *ssusb = otg_sx_to_ssusb(otg_sx);
|
2021-07-15 17:07:52 +08:00
|
|
|
struct device *dev = ssusb->dev;
|
|
|
|
enum usb_dr_mode mode;
|
2019-08-29 17:22:38 +08:00
|
|
|
|
|
|
|
if (!otg_sx->role_sw_used)
|
|
|
|
return 0;
|
|
|
|
|
2021-07-15 17:07:52 +08:00
|
|
|
mode = usb_get_role_switch_default_mode(dev);
|
|
|
|
if (mode == USB_DR_MODE_PERIPHERAL)
|
|
|
|
otg_sx->default_role = USB_ROLE_DEVICE;
|
|
|
|
else
|
|
|
|
otg_sx->default_role = USB_ROLE_HOST;
|
|
|
|
|
2019-08-29 17:22:38 +08:00
|
|
|
role_sx_desc.set = ssusb_role_sw_set;
|
|
|
|
role_sx_desc.get = ssusb_role_sw_get;
|
2021-07-15 17:07:52 +08:00
|
|
|
role_sx_desc.fwnode = dev_fwnode(dev);
|
2020-03-02 21:53:49 +08:00
|
|
|
role_sx_desc.driver_data = ssusb;
|
2021-07-15 17:07:52 +08:00
|
|
|
otg_sx->role_sw = usb_role_switch_register(dev, &role_sx_desc);
|
|
|
|
if (IS_ERR(otg_sx->role_sw))
|
|
|
|
return PTR_ERR(otg_sx->role_sw);
|
2019-08-29 17:22:38 +08:00
|
|
|
|
2021-07-15 17:07:52 +08:00
|
|
|
ssusb_set_mode(otg_sx, otg_sx->default_role);
|
|
|
|
|
|
|
|
return 0;
|
2019-08-29 17:22:38 +08:00
|
|
|
}
|
|
|
|
|
2016-10-19 10:28:26 +08:00
|
|
|
int ssusb_otg_switch_init(struct ssusb_mtk *ssusb)
|
|
|
|
{
|
|
|
|
struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
|
2019-03-21 10:53:39 +08:00
|
|
|
int ret = 0;
|
2016-10-19 10:28:26 +08:00
|
|
|
|
2021-06-08 15:57:36 +08:00
|
|
|
INIT_WORK(&otg_sx->dr_work, ssusb_mode_sw_work);
|
2018-05-23 16:53:20 +08:00
|
|
|
|
2018-04-24 10:52:48 +08:00
|
|
|
if (otg_sx->manual_drd_enabled)
|
2019-03-21 10:53:47 +08:00
|
|
|
ssusb_dr_debugfs_init(ssusb);
|
2019-08-29 17:22:38 +08:00
|
|
|
else if (otg_sx->role_sw_used)
|
|
|
|
ret = ssusb_role_sw_register(otg_sx);
|
2018-04-24 10:52:48 +08:00
|
|
|
else
|
2019-03-21 10:53:39 +08:00
|
|
|
ret = ssusb_extcon_register(otg_sx);
|
2016-10-19 10:28:26 +08:00
|
|
|
|
2019-03-21 10:53:39 +08:00
|
|
|
return ret;
|
2016-10-19 10:28:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ssusb_otg_switch_exit(struct ssusb_mtk *ssusb)
|
|
|
|
{
|
|
|
|
struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
|
|
|
|
|
2021-06-08 15:57:36 +08:00
|
|
|
cancel_work_sync(&otg_sx->dr_work);
|
2019-08-29 17:22:38 +08:00
|
|
|
usb_role_switch_unregister(otg_sx->role_sw);
|
2016-10-19 10:28:26 +08:00
|
|
|
}
|