Commit 6b587394 authored by Chunfeng Yun's avatar Chunfeng Yun Committed by Greg Kroah-Hartman

usb: mtu3: support suspend/resume for dual-role mode

Support suspend/resume for dual-role mode including the single
port and multi-ports supported by host controller, when the host
supports mult-ports, only port0 (u2/u3) is used to support dual
role mode.
Signed-off-by: default avatarChunfeng Yun <chunfeng.yun@mediatek.com>
Link: https://lore.kernel.org/r/1626340078-29111-14-git-send-email-chunfeng.yun@mediatek.comSigned-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 427c6642
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
*/ */
#include <linux/dma-mapping.h> #include <linux/dma-mapping.h>
#include <linux/iopoll.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/of_address.h> #include <linux/of_address.h>
...@@ -1008,12 +1007,25 @@ void ssusb_gadget_exit(struct ssusb_mtk *ssusb) ...@@ -1008,12 +1007,25 @@ void ssusb_gadget_exit(struct ssusb_mtk *ssusb)
mtu3_hw_exit(mtu); mtu3_hw_exit(mtu);
} }
bool ssusb_gadget_ip_sleep_check(struct ssusb_mtk *ssusb)
{
struct mtu3 *mtu = ssusb->u3d;
/* host only, should wait for ip sleep */
if (!mtu)
return true;
/* device is started and pullup D+, ip can sleep */
if (mtu->is_active && mtu->softconnect)
return true;
/* ip can't sleep if not pullup D+ when support device mode */
return false;
}
int ssusb_gadget_suspend(struct ssusb_mtk *ssusb, pm_message_t msg) int ssusb_gadget_suspend(struct ssusb_mtk *ssusb, pm_message_t msg)
{ {
struct mtu3 *mtu = ssusb->u3d; struct mtu3 *mtu = ssusb->u3d;
void __iomem *ibase = mtu->ippc_base;
u32 value;
int ret = 0;
if (!mtu->gadget_driver) if (!mtu->gadget_driver)
return 0; return 0;
...@@ -1024,17 +1036,7 @@ int ssusb_gadget_suspend(struct ssusb_mtk *ssusb, pm_message_t msg) ...@@ -1024,17 +1036,7 @@ int ssusb_gadget_suspend(struct ssusb_mtk *ssusb, pm_message_t msg)
mtu3_dev_suspend(mtu); mtu3_dev_suspend(mtu);
synchronize_irq(mtu->irq); synchronize_irq(mtu->irq);
/* wait for ip to sleep */ return 0;
if (mtu->is_active && mtu->softconnect) {
ret = readl_poll_timeout(ibase + U3D_SSUSB_IP_PW_STS1,
value, (value & SSUSB_IP_SLEEP_STS), 100, 100000);
if (ret) {
dev_err(mtu->dev, "ip sleep failed!!!\n");
ret = -EBUSY;
}
}
return ret;
} }
int ssusb_gadget_resume(struct ssusb_mtk *ssusb, pm_message_t msg) int ssusb_gadget_resume(struct ssusb_mtk *ssusb, pm_message_t msg)
......
...@@ -149,6 +149,7 @@ static void ssusb_mode_sw_work(struct work_struct *work) ...@@ -149,6 +149,7 @@ static void ssusb_mode_sw_work(struct work_struct *work)
dev_dbg(ssusb->dev, "set role : %s\n", usb_role_string(desired_role)); 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)); mtu3_dbg_trace(ssusb->dev, "set role : %s", usb_role_string(desired_role));
pm_runtime_get_sync(ssusb->dev);
switch (desired_role) { switch (desired_role) {
case USB_ROLE_HOST: case USB_ROLE_HOST:
...@@ -169,6 +170,7 @@ static void ssusb_mode_sw_work(struct work_struct *work) ...@@ -169,6 +170,7 @@ static void ssusb_mode_sw_work(struct work_struct *work)
default: default:
dev_err(ssusb->dev, "invalid role\n"); dev_err(ssusb->dev, "invalid role\n");
} }
pm_runtime_put(ssusb->dev);
} }
static void ssusb_set_mode(struct otg_switch_mtk *otg_sx, enum usb_role role) static void ssusb_set_mode(struct otg_switch_mtk *otg_sx, enum usb_role role)
......
...@@ -59,6 +59,8 @@ int ssusb_gadget_init(struct ssusb_mtk *ssusb); ...@@ -59,6 +59,8 @@ int ssusb_gadget_init(struct ssusb_mtk *ssusb);
void ssusb_gadget_exit(struct ssusb_mtk *ssusb); void ssusb_gadget_exit(struct ssusb_mtk *ssusb);
int ssusb_gadget_suspend(struct ssusb_mtk *ssusb, pm_message_t msg); int ssusb_gadget_suspend(struct ssusb_mtk *ssusb, pm_message_t msg);
int ssusb_gadget_resume(struct ssusb_mtk *ssusb, pm_message_t msg); int ssusb_gadget_resume(struct ssusb_mtk *ssusb, pm_message_t msg);
bool ssusb_gadget_ip_sleep_check(struct ssusb_mtk *ssusb);
#else #else
static inline int ssusb_gadget_init(struct ssusb_mtk *ssusb) static inline int ssusb_gadget_init(struct ssusb_mtk *ssusb)
{ {
...@@ -79,6 +81,12 @@ ssusb_gadget_resume(struct ssusb_mtk *ssusb, pm_message_t msg) ...@@ -79,6 +81,12 @@ ssusb_gadget_resume(struct ssusb_mtk *ssusb, pm_message_t msg)
{ {
return 0; return 0;
} }
static inline bool ssusb_gadget_ip_sleep_check(struct ssusb_mtk *ssusb)
{
return true;
}
#endif #endif
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
*/ */
#include <linux/clk.h> #include <linux/clk.h>
#include <linux/iopoll.h>
#include <linux/irq.h> #include <linux/irq.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/mfd/syscon.h> #include <linux/mfd/syscon.h>
...@@ -254,7 +253,6 @@ int ssusb_host_suspend(struct ssusb_mtk *ssusb) ...@@ -254,7 +253,6 @@ int ssusb_host_suspend(struct ssusb_mtk *ssusb)
int num_u3p = ssusb->u3_ports; int num_u3p = ssusb->u3_ports;
int num_u2p = ssusb->u2_ports; int num_u2p = ssusb->u2_ports;
u32 value; u32 value;
int ret;
int i; int i;
/* power down u3 ports except skipped ones */ /* power down u3 ports except skipped ones */
...@@ -280,13 +278,7 @@ int ssusb_host_suspend(struct ssusb_mtk *ssusb) ...@@ -280,13 +278,7 @@ int ssusb_host_suspend(struct ssusb_mtk *ssusb)
/* power down host ip */ /* power down host ip */
mtu3_setbits(ibase, U3D_SSUSB_IP_PW_CTRL1, SSUSB_IP_HOST_PDN); mtu3_setbits(ibase, U3D_SSUSB_IP_PW_CTRL1, SSUSB_IP_HOST_PDN);
/* wait for host ip to sleep */ return 0;
ret = readl_poll_timeout(ibase + U3D_SSUSB_IP_PW_STS1, value,
(value & SSUSB_IP_SLEEP_STS), 100, 100000);
if (ret)
dev_err(ssusb->dev, "ip sleep failed!!!\n");
return ret;
} }
static void ssusb_host_setup(struct ssusb_mtk *ssusb) static void ssusb_host_setup(struct ssusb_mtk *ssusb)
......
...@@ -45,6 +45,29 @@ int ssusb_check_clocks(struct ssusb_mtk *ssusb, u32 ex_clks) ...@@ -45,6 +45,29 @@ int ssusb_check_clocks(struct ssusb_mtk *ssusb, u32 ex_clks)
return 0; return 0;
} }
static int wait_for_ip_sleep(struct ssusb_mtk *ssusb)
{
bool sleep_check = true;
u32 value;
int ret;
if (!ssusb->is_host)
sleep_check = ssusb_gadget_ip_sleep_check(ssusb);
if (!sleep_check)
return 0;
/* wait for ip enter sleep mode */
ret = readl_poll_timeout(ssusb->ippc_base + U3D_SSUSB_IP_PW_STS1, value,
(value & SSUSB_IP_SLEEP_STS), 100, 100000);
if (ret) {
dev_err(ssusb->dev, "ip sleep failed!!!\n");
ret = -EBUSY;
}
return ret;
}
static int ssusb_phy_init(struct ssusb_mtk *ssusb) static int ssusb_phy_init(struct ssusb_mtk *ssusb)
{ {
int i; int i;
...@@ -421,6 +444,28 @@ static int mtu3_remove(struct platform_device *pdev) ...@@ -421,6 +444,28 @@ static int mtu3_remove(struct platform_device *pdev)
return 0; return 0;
} }
static int resume_ip_and_ports(struct ssusb_mtk *ssusb, pm_message_t msg)
{
switch (ssusb->dr_mode) {
case USB_DR_MODE_PERIPHERAL:
ssusb_gadget_resume(ssusb, msg);
break;
case USB_DR_MODE_HOST:
ssusb_host_resume(ssusb, false);
break;
case USB_DR_MODE_OTG:
ssusb_host_resume(ssusb, !ssusb->is_host);
if (!ssusb->is_host)
ssusb_gadget_resume(ssusb, msg);
break;
default:
return -EINVAL;
}
return 0;
}
static int mtu3_suspend_common(struct device *dev, pm_message_t msg) static int mtu3_suspend_common(struct device *dev, pm_message_t msg)
{ {
struct ssusb_mtk *ssusb = dev_get_drvdata(dev); struct ssusb_mtk *ssusb = dev_get_drvdata(dev);
...@@ -432,26 +477,36 @@ static int mtu3_suspend_common(struct device *dev, pm_message_t msg) ...@@ -432,26 +477,36 @@ static int mtu3_suspend_common(struct device *dev, pm_message_t msg)
case USB_DR_MODE_PERIPHERAL: case USB_DR_MODE_PERIPHERAL:
ret = ssusb_gadget_suspend(ssusb, msg); ret = ssusb_gadget_suspend(ssusb, msg);
if (ret) if (ret)
return ret; goto err;
break; break;
case USB_DR_MODE_HOST: case USB_DR_MODE_HOST:
ssusb_host_suspend(ssusb); ssusb_host_suspend(ssusb);
break; break;
case USB_DR_MODE_OTG: case USB_DR_MODE_OTG:
if (!ssusb->is_host) if (!ssusb->is_host) {
return 0; ret = ssusb_gadget_suspend(ssusb, msg);
if (ret)
goto err;
}
ssusb_host_suspend(ssusb); ssusb_host_suspend(ssusb);
break; break;
default: default:
return -EINVAL; return -EINVAL;
} }
ret = wait_for_ip_sleep(ssusb);
if (ret)
goto sleep_err;
ssusb_phy_power_off(ssusb); ssusb_phy_power_off(ssusb);
clk_bulk_disable_unprepare(BULK_CLKS_CNT, ssusb->clks); clk_bulk_disable_unprepare(BULK_CLKS_CNT, ssusb->clks);
ssusb_wakeup_set(ssusb, true); ssusb_wakeup_set(ssusb, true);
return 0; sleep_err:
resume_ip_and_ports(ssusb, msg);
err:
return ret;
} }
static int mtu3_resume_common(struct device *dev, pm_message_t msg) static int mtu3_resume_common(struct device *dev, pm_message_t msg)
...@@ -470,24 +525,7 @@ static int mtu3_resume_common(struct device *dev, pm_message_t msg) ...@@ -470,24 +525,7 @@ static int mtu3_resume_common(struct device *dev, pm_message_t msg)
if (ret) if (ret)
goto phy_err; goto phy_err;
switch (ssusb->dr_mode) { return resume_ip_and_ports(ssusb, msg);
case USB_DR_MODE_PERIPHERAL:
ssusb_gadget_resume(ssusb, msg);
break;
case USB_DR_MODE_HOST:
ssusb_host_resume(ssusb, false);
break;
case USB_DR_MODE_OTG:
if (!ssusb->is_host)
return 0;
ssusb_host_resume(ssusb, true);
break;
default:
return -EINVAL;
}
return 0;
phy_err: phy_err:
clk_bulk_disable_unprepare(BULK_CLKS_CNT, ssusb->clks); clk_bulk_disable_unprepare(BULK_CLKS_CNT, ssusb->clks);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment