1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016 MediaTek Inc.
5 * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
8 #include <linux/dma-mapping.h>
9 #include <linux/iopoll.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of_address.h>
13 #include <linux/of_irq.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_wakeirq.h>
19 #include "mtu3_debug.h"
21 /* u2-port0 should be powered on and enabled; */
22 int ssusb_check_clocks(struct ssusb_mtk *ssusb, u32 ex_clks)
24 void __iomem *ibase = ssusb->ippc_base;
28 check_val = ex_clks | SSUSB_SYS125_RST_B_STS | SSUSB_SYSPLL_STABLE |
31 ret = readl_poll_timeout(ibase + U3D_SSUSB_IP_PW_STS1, value,
32 (check_val == (value & check_val)), 100, 20000);
34 dev_err(ssusb->dev, "clks of sts1 are not stable!\n");
38 ret = readl_poll_timeout(ibase + U3D_SSUSB_IP_PW_STS2, value,
39 (value & SSUSB_U2_MAC_SYS_RST_B_STS), 100, 10000);
41 dev_err(ssusb->dev, "mac2 clock is not stable\n");
48 static int wait_for_ip_sleep(struct ssusb_mtk *ssusb)
50 bool sleep_check = true;
55 sleep_check = ssusb_gadget_ip_sleep_check(ssusb);
60 /* wait for ip enter sleep mode */
61 ret = readl_poll_timeout(ssusb->ippc_base + U3D_SSUSB_IP_PW_STS1, value,
62 (value & SSUSB_IP_SLEEP_STS), 100, 100000);
64 dev_err(ssusb->dev, "ip sleep failed!!!\n");
67 /* workaround: avoid wrong wakeup signal latch for some soc */
68 usleep_range(100, 200);
74 static int ssusb_phy_init(struct ssusb_mtk *ssusb)
79 for (i = 0; i < ssusb->num_phys; i++) {
80 ret = phy_init(ssusb->phys[i]);
88 phy_exit(ssusb->phys[i - 1]);
93 static int ssusb_phy_exit(struct ssusb_mtk *ssusb)
97 for (i = 0; i < ssusb->num_phys; i++)
98 phy_exit(ssusb->phys[i]);
103 static int ssusb_phy_power_on(struct ssusb_mtk *ssusb)
108 for (i = 0; i < ssusb->num_phys; i++) {
109 ret = phy_power_on(ssusb->phys[i]);
117 phy_power_off(ssusb->phys[i - 1]);
122 static void ssusb_phy_power_off(struct ssusb_mtk *ssusb)
126 for (i = 0; i < ssusb->num_phys; i++)
127 phy_power_off(ssusb->phys[i]);
130 static int ssusb_rscs_init(struct ssusb_mtk *ssusb)
134 ret = regulator_enable(ssusb->vusb33);
136 dev_err(ssusb->dev, "failed to enable vusb33\n");
140 ret = clk_bulk_prepare_enable(BULK_CLKS_CNT, ssusb->clks);
144 ret = ssusb_phy_init(ssusb);
146 dev_err(ssusb->dev, "failed to init phy\n");
150 ret = ssusb_phy_power_on(ssusb);
152 dev_err(ssusb->dev, "failed to power on phy\n");
159 ssusb_phy_exit(ssusb);
161 clk_bulk_disable_unprepare(BULK_CLKS_CNT, ssusb->clks);
163 regulator_disable(ssusb->vusb33);
168 static void ssusb_rscs_exit(struct ssusb_mtk *ssusb)
170 clk_bulk_disable_unprepare(BULK_CLKS_CNT, ssusb->clks);
171 regulator_disable(ssusb->vusb33);
172 ssusb_phy_power_off(ssusb);
173 ssusb_phy_exit(ssusb);
176 static void ssusb_ip_sw_reset(struct ssusb_mtk *ssusb)
178 /* reset whole ip (xhci & u3d) */
179 mtu3_setbits(ssusb->ippc_base, U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST);
181 mtu3_clrbits(ssusb->ippc_base, U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST);
184 * device ip may be powered on in firmware/BROM stage before entering
186 * power down device ip, otherwise ip-sleep will fail when working as
189 mtu3_setbits(ssusb->ippc_base, U3D_SSUSB_IP_PW_CTRL2, SSUSB_IP_DEV_PDN);
192 static int get_ssusb_rscs(struct platform_device *pdev, struct ssusb_mtk *ssusb)
194 struct device_node *node = pdev->dev.of_node;
195 struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
196 struct clk_bulk_data *clks = ssusb->clks;
197 struct device *dev = &pdev->dev;
201 ssusb->vusb33 = devm_regulator_get(dev, "vusb33");
202 if (IS_ERR(ssusb->vusb33)) {
203 dev_err(dev, "failed to get vusb33\n");
204 return PTR_ERR(ssusb->vusb33);
207 clks[0].id = "sys_ck";
208 clks[1].id = "ref_ck";
209 clks[2].id = "mcu_ck";
210 clks[3].id = "dma_ck";
211 ret = devm_clk_bulk_get_optional(dev, BULK_CLKS_CNT, clks);
215 ssusb->num_phys = of_count_phandle_with_args(node,
216 "phys", "#phy-cells");
217 if (ssusb->num_phys > 0) {
218 ssusb->phys = devm_kcalloc(dev, ssusb->num_phys,
219 sizeof(*ssusb->phys), GFP_KERNEL);
226 for (i = 0; i < ssusb->num_phys; i++) {
227 ssusb->phys[i] = devm_of_phy_get_by_index(dev, node, i);
228 if (IS_ERR(ssusb->phys[i])) {
229 dev_err(dev, "failed to get phy-%d\n", i);
230 return PTR_ERR(ssusb->phys[i]);
234 ssusb->ippc_base = devm_platform_ioremap_resource_byname(pdev, "ippc");
235 if (IS_ERR(ssusb->ippc_base))
236 return PTR_ERR(ssusb->ippc_base);
238 ssusb->wakeup_irq = platform_get_irq_byname_optional(pdev, "wakeup");
239 if (ssusb->wakeup_irq == -EPROBE_DEFER)
240 return ssusb->wakeup_irq;
242 ssusb->dr_mode = usb_get_dr_mode(dev);
243 if (ssusb->dr_mode == USB_DR_MODE_UNKNOWN)
244 ssusb->dr_mode = USB_DR_MODE_OTG;
246 if (ssusb->dr_mode == USB_DR_MODE_PERIPHERAL)
249 /* if host role is supported */
250 ret = ssusb_wakeup_of_property_parse(ssusb, node);
252 dev_err(dev, "failed to parse uwk property\n");
256 /* optional property, ignore the error if it does not exist */
257 of_property_read_u32(node, "mediatek,u3p-dis-msk",
258 &ssusb->u3p_dis_msk);
259 of_property_read_u32(node, "mediatek,u2p-dis-msk",
260 &ssusb->u2p_dis_msk);
262 otg_sx->vbus = devm_regulator_get(dev, "vbus");
263 if (IS_ERR(otg_sx->vbus)) {
264 dev_err(dev, "failed to get vbus\n");
265 return PTR_ERR(otg_sx->vbus);
268 if (ssusb->dr_mode == USB_DR_MODE_HOST)
271 /* if dual-role mode is supported */
272 otg_sx->is_u3_drd = of_property_read_bool(node, "mediatek,usb3-drd");
273 otg_sx->manual_drd_enabled =
274 of_property_read_bool(node, "enable-manual-drd");
275 otg_sx->role_sw_used = of_property_read_bool(node, "usb-role-switch");
277 /* can't disable port0 when use dual-role mode */
278 ssusb->u2p_dis_msk &= ~0x1;
280 if (otg_sx->role_sw_used || otg_sx->manual_drd_enabled)
283 if (of_property_read_bool(node, "extcon")) {
284 otg_sx->edev = extcon_get_edev_by_phandle(ssusb->dev, 0);
285 if (IS_ERR(otg_sx->edev)) {
286 return dev_err_probe(dev, PTR_ERR(otg_sx->edev),
287 "couldn't get extcon device\n");
292 dev_info(dev, "dr_mode: %d, is_u3_dr: %d, drd: %s\n",
293 ssusb->dr_mode, otg_sx->is_u3_drd,
294 otg_sx->manual_drd_enabled ? "manual" : "auto");
295 dev_info(dev, "u2p_dis_msk: %x, u3p_dis_msk: %x\n",
296 ssusb->u2p_dis_msk, ssusb->u3p_dis_msk);
301 static int mtu3_probe(struct platform_device *pdev)
303 struct device_node *node = pdev->dev.of_node;
304 struct device *dev = &pdev->dev;
305 struct ssusb_mtk *ssusb;
308 /* all elements are set to ZERO as default value */
309 ssusb = devm_kzalloc(dev, sizeof(*ssusb), GFP_KERNEL);
313 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
315 dev_err(dev, "No suitable DMA config available\n");
319 platform_set_drvdata(pdev, ssusb);
322 ret = get_ssusb_rscs(pdev, ssusb);
326 ssusb_debugfs_create_root(ssusb);
328 /* enable power domain */
329 pm_runtime_set_active(dev);
330 pm_runtime_use_autosuspend(dev);
331 pm_runtime_set_autosuspend_delay(dev, 4000);
332 pm_runtime_enable(dev);
333 pm_runtime_get_sync(dev);
335 device_init_wakeup(dev, true);
337 ret = ssusb_rscs_init(ssusb);
341 if (ssusb->wakeup_irq > 0) {
342 ret = dev_pm_set_dedicated_wake_irq(dev, ssusb->wakeup_irq);
344 dev_err(dev, "failed to set wakeup irq %d\n", ssusb->wakeup_irq);
347 dev_info(dev, "wakeup irq %d\n", ssusb->wakeup_irq);
350 ssusb_ip_sw_reset(ssusb);
352 if (IS_ENABLED(CONFIG_USB_MTU3_HOST))
353 ssusb->dr_mode = USB_DR_MODE_HOST;
354 else if (IS_ENABLED(CONFIG_USB_MTU3_GADGET))
355 ssusb->dr_mode = USB_DR_MODE_PERIPHERAL;
357 /* default as host */
358 ssusb->is_host = !(ssusb->dr_mode == USB_DR_MODE_PERIPHERAL);
360 switch (ssusb->dr_mode) {
361 case USB_DR_MODE_PERIPHERAL:
362 ret = ssusb_gadget_init(ssusb);
364 dev_err(dev, "failed to initialize gadget\n");
368 case USB_DR_MODE_HOST:
369 ret = ssusb_host_init(ssusb, node);
371 dev_err(dev, "failed to initialize host\n");
375 case USB_DR_MODE_OTG:
376 ret = ssusb_gadget_init(ssusb);
378 dev_err(dev, "failed to initialize gadget\n");
382 ret = ssusb_host_init(ssusb, node);
384 dev_err(dev, "failed to initialize host\n");
388 ret = ssusb_otg_switch_init(ssusb);
390 dev_err(dev, "failed to initialize switch\n");
395 dev_err(dev, "unsupported mode: %d\n", ssusb->dr_mode);
400 device_enable_async_suspend(dev);
401 pm_runtime_mark_last_busy(dev);
402 pm_runtime_put_autosuspend(dev);
403 pm_runtime_forbid(dev);
408 ssusb_host_exit(ssusb);
410 ssusb_gadget_exit(ssusb);
412 ssusb_rscs_exit(ssusb);
414 pm_runtime_put_noidle(dev);
415 pm_runtime_disable(dev);
416 ssusb_debugfs_remove_root(ssusb);
421 static int mtu3_remove(struct platform_device *pdev)
423 struct ssusb_mtk *ssusb = platform_get_drvdata(pdev);
425 pm_runtime_get_sync(&pdev->dev);
427 switch (ssusb->dr_mode) {
428 case USB_DR_MODE_PERIPHERAL:
429 ssusb_gadget_exit(ssusb);
431 case USB_DR_MODE_HOST:
432 ssusb_host_exit(ssusb);
434 case USB_DR_MODE_OTG:
435 ssusb_otg_switch_exit(ssusb);
436 ssusb_gadget_exit(ssusb);
437 ssusb_host_exit(ssusb);
443 ssusb_rscs_exit(ssusb);
444 ssusb_debugfs_remove_root(ssusb);
445 pm_runtime_disable(&pdev->dev);
446 pm_runtime_put_noidle(&pdev->dev);
447 pm_runtime_set_suspended(&pdev->dev);
452 static int resume_ip_and_ports(struct ssusb_mtk *ssusb, pm_message_t msg)
454 switch (ssusb->dr_mode) {
455 case USB_DR_MODE_PERIPHERAL:
456 ssusb_gadget_resume(ssusb, msg);
458 case USB_DR_MODE_HOST:
459 ssusb_host_resume(ssusb, false);
461 case USB_DR_MODE_OTG:
462 ssusb_host_resume(ssusb, !ssusb->is_host);
464 ssusb_gadget_resume(ssusb, msg);
474 static int mtu3_suspend_common(struct device *dev, pm_message_t msg)
476 struct ssusb_mtk *ssusb = dev_get_drvdata(dev);
479 dev_dbg(dev, "%s\n", __func__);
481 switch (ssusb->dr_mode) {
482 case USB_DR_MODE_PERIPHERAL:
483 ret = ssusb_gadget_suspend(ssusb, msg);
488 case USB_DR_MODE_HOST:
489 ssusb_host_suspend(ssusb);
491 case USB_DR_MODE_OTG:
492 if (!ssusb->is_host) {
493 ret = ssusb_gadget_suspend(ssusb, msg);
497 ssusb_host_suspend(ssusb);
503 ret = wait_for_ip_sleep(ssusb);
507 ssusb_phy_power_off(ssusb);
508 clk_bulk_disable_unprepare(BULK_CLKS_CNT, ssusb->clks);
509 ssusb_wakeup_set(ssusb, true);
513 resume_ip_and_ports(ssusb, msg);
518 static int mtu3_resume_common(struct device *dev, pm_message_t msg)
520 struct ssusb_mtk *ssusb = dev_get_drvdata(dev);
523 dev_dbg(dev, "%s\n", __func__);
525 ssusb_wakeup_set(ssusb, false);
526 ret = clk_bulk_prepare_enable(BULK_CLKS_CNT, ssusb->clks);
530 ret = ssusb_phy_power_on(ssusb);
534 return resume_ip_and_ports(ssusb, msg);
537 clk_bulk_disable_unprepare(BULK_CLKS_CNT, ssusb->clks);
542 static int __maybe_unused mtu3_suspend(struct device *dev)
544 return mtu3_suspend_common(dev, PMSG_SUSPEND);
547 static int __maybe_unused mtu3_resume(struct device *dev)
549 return mtu3_resume_common(dev, PMSG_SUSPEND);
552 static int __maybe_unused mtu3_runtime_suspend(struct device *dev)
554 if (!device_may_wakeup(dev))
557 return mtu3_suspend_common(dev, PMSG_AUTO_SUSPEND);
560 static int __maybe_unused mtu3_runtime_resume(struct device *dev)
562 if (!device_may_wakeup(dev))
565 return mtu3_resume_common(dev, PMSG_AUTO_SUSPEND);
568 static const struct dev_pm_ops mtu3_pm_ops = {
569 SET_SYSTEM_SLEEP_PM_OPS(mtu3_suspend, mtu3_resume)
570 SET_RUNTIME_PM_OPS(mtu3_runtime_suspend,
571 mtu3_runtime_resume, NULL)
574 #define DEV_PM_OPS (IS_ENABLED(CONFIG_PM) ? &mtu3_pm_ops : NULL)
576 static const struct of_device_id mtu3_of_match[] = {
577 {.compatible = "mediatek,mt8173-mtu3",},
578 {.compatible = "mediatek,mtu3",},
581 MODULE_DEVICE_TABLE(of, mtu3_of_match);
583 static struct platform_driver mtu3_driver = {
585 .remove = mtu3_remove,
587 .name = MTU3_DRIVER_NAME,
589 .of_match_table = mtu3_of_match,
592 module_platform_driver(mtu3_driver);
594 MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
595 MODULE_LICENSE("GPL v2");
596 MODULE_DESCRIPTION("MediaTek USB3 DRD Controller Driver");