1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, The Linux Foundation. All rights reserved.
4 * Inspired by dwc3-of-simple.c
10 #include <linux/irq.h>
11 #include <linux/clk-provider.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/extcon.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/phy/phy.h>
18 #include <linux/usb/of.h>
19 #include <linux/reset.h>
20 #include <linux/iopoll.h>
24 /* USB QSCRATCH Hardware registers */
25 #define QSCRATCH_HS_PHY_CTRL 0x10
26 #define UTMI_OTG_VBUS_VALID BIT(20)
27 #define SW_SESSVLD_SEL BIT(28)
29 #define QSCRATCH_SS_PHY_CTRL 0x30
30 #define LANE0_PWR_PRESENT BIT(24)
32 #define QSCRATCH_GENERAL_CFG 0x08
33 #define PIPE_UTMI_CLK_SEL BIT(0)
34 #define PIPE3_PHYSTATUS_SW BIT(3)
35 #define PIPE_UTMI_CLK_DIS BIT(8)
37 #define PWR_EVNT_IRQ_STAT_REG 0x58
38 #define PWR_EVNT_LPM_IN_L2_MASK BIT(4)
39 #define PWR_EVNT_LPM_OUT_L2_MASK BIT(5)
43 void __iomem *qscratch_base;
44 struct platform_device *dwc3;
47 struct reset_control *resets;
54 struct extcon_dev *edev;
55 struct extcon_dev *host_edev;
56 struct notifier_block vbus_nb;
57 struct notifier_block host_nb;
59 enum usb_dr_mode mode;
64 static inline void dwc3_qcom_setbits(void __iomem *base, u32 offset, u32 val)
68 reg = readl(base + offset);
70 writel(reg, base + offset);
72 /* ensure that above write is through */
76 static inline void dwc3_qcom_clrbits(void __iomem *base, u32 offset, u32 val)
80 reg = readl(base + offset);
82 writel(reg, base + offset);
84 /* ensure that above write is through */
88 static void dwc3_qcom_vbus_override_enable(struct dwc3_qcom *qcom, bool enable)
91 dwc3_qcom_setbits(qcom->qscratch_base, QSCRATCH_SS_PHY_CTRL,
93 dwc3_qcom_setbits(qcom->qscratch_base, QSCRATCH_HS_PHY_CTRL,
94 UTMI_OTG_VBUS_VALID | SW_SESSVLD_SEL);
96 dwc3_qcom_clrbits(qcom->qscratch_base, QSCRATCH_SS_PHY_CTRL,
98 dwc3_qcom_clrbits(qcom->qscratch_base, QSCRATCH_HS_PHY_CTRL,
99 UTMI_OTG_VBUS_VALID | SW_SESSVLD_SEL);
103 static int dwc3_qcom_vbus_notifier(struct notifier_block *nb,
104 unsigned long event, void *ptr)
106 struct dwc3_qcom *qcom = container_of(nb, struct dwc3_qcom, vbus_nb);
108 /* enable vbus override for device mode */
109 dwc3_qcom_vbus_override_enable(qcom, event);
110 qcom->mode = event ? USB_DR_MODE_PERIPHERAL : USB_DR_MODE_HOST;
115 static int dwc3_qcom_host_notifier(struct notifier_block *nb,
116 unsigned long event, void *ptr)
118 struct dwc3_qcom *qcom = container_of(nb, struct dwc3_qcom, host_nb);
120 /* disable vbus override in host mode */
121 dwc3_qcom_vbus_override_enable(qcom, !event);
122 qcom->mode = event ? USB_DR_MODE_HOST : USB_DR_MODE_PERIPHERAL;
127 static int dwc3_qcom_register_extcon(struct dwc3_qcom *qcom)
129 struct device *dev = qcom->dev;
130 struct extcon_dev *host_edev;
133 if (!of_property_read_bool(dev->of_node, "extcon"))
136 qcom->edev = extcon_get_edev_by_phandle(dev, 0);
137 if (IS_ERR(qcom->edev))
138 return PTR_ERR(qcom->edev);
140 qcom->vbus_nb.notifier_call = dwc3_qcom_vbus_notifier;
142 qcom->host_edev = extcon_get_edev_by_phandle(dev, 1);
143 if (IS_ERR(qcom->host_edev))
144 qcom->host_edev = NULL;
146 ret = devm_extcon_register_notifier(dev, qcom->edev, EXTCON_USB,
149 dev_err(dev, "VBUS notifier register failed\n");
154 host_edev = qcom->host_edev;
156 host_edev = qcom->edev;
158 qcom->host_nb.notifier_call = dwc3_qcom_host_notifier;
159 ret = devm_extcon_register_notifier(dev, host_edev, EXTCON_USB_HOST,
162 dev_err(dev, "Host notifier register failed\n");
166 /* Update initial VBUS override based on extcon state */
167 if (extcon_get_state(qcom->edev, EXTCON_USB) ||
168 !extcon_get_state(host_edev, EXTCON_USB_HOST))
169 dwc3_qcom_vbus_notifier(&qcom->vbus_nb, true, qcom->edev);
171 dwc3_qcom_vbus_notifier(&qcom->vbus_nb, false, qcom->edev);
176 /* Only usable in contexts where the role can not change. */
177 static bool dwc3_qcom_is_host(struct dwc3_qcom *qcom)
179 struct dwc3 *dwc = platform_get_drvdata(qcom->dwc3);
184 static void dwc3_qcom_disable_interrupts(struct dwc3_qcom *qcom)
186 if (qcom->hs_phy_irq) {
187 disable_irq_wake(qcom->hs_phy_irq);
188 disable_irq_nosync(qcom->hs_phy_irq);
191 if (qcom->dp_hs_phy_irq) {
192 disable_irq_wake(qcom->dp_hs_phy_irq);
193 disable_irq_nosync(qcom->dp_hs_phy_irq);
196 if (qcom->dm_hs_phy_irq) {
197 disable_irq_wake(qcom->dm_hs_phy_irq);
198 disable_irq_nosync(qcom->dm_hs_phy_irq);
201 if (qcom->ss_phy_irq) {
202 disable_irq_wake(qcom->ss_phy_irq);
203 disable_irq_nosync(qcom->ss_phy_irq);
207 static void dwc3_qcom_enable_interrupts(struct dwc3_qcom *qcom)
209 if (qcom->hs_phy_irq) {
210 enable_irq(qcom->hs_phy_irq);
211 enable_irq_wake(qcom->hs_phy_irq);
214 if (qcom->dp_hs_phy_irq) {
215 enable_irq(qcom->dp_hs_phy_irq);
216 enable_irq_wake(qcom->dp_hs_phy_irq);
219 if (qcom->dm_hs_phy_irq) {
220 enable_irq(qcom->dm_hs_phy_irq);
221 enable_irq_wake(qcom->dm_hs_phy_irq);
224 if (qcom->ss_phy_irq) {
225 enable_irq(qcom->ss_phy_irq);
226 enable_irq_wake(qcom->ss_phy_irq);
230 static int dwc3_qcom_suspend(struct dwc3_qcom *qcom)
235 if (qcom->is_suspended)
238 val = readl(qcom->qscratch_base + PWR_EVNT_IRQ_STAT_REG);
239 if (!(val & PWR_EVNT_LPM_IN_L2_MASK))
240 dev_err(qcom->dev, "HS-PHY not in L2\n");
242 for (i = qcom->num_clocks - 1; i >= 0; i--)
243 clk_disable_unprepare(qcom->clks[i]);
245 if (device_may_wakeup(qcom->dev))
246 dwc3_qcom_enable_interrupts(qcom);
248 qcom->is_suspended = true;
253 static int dwc3_qcom_resume(struct dwc3_qcom *qcom)
258 if (!qcom->is_suspended)
261 if (device_may_wakeup(qcom->dev))
262 dwc3_qcom_disable_interrupts(qcom);
264 for (i = 0; i < qcom->num_clocks; i++) {
265 ret = clk_prepare_enable(qcom->clks[i]);
268 clk_disable_unprepare(qcom->clks[i]);
273 /* Clear existing events from PHY related to L2 in/out */
274 dwc3_qcom_setbits(qcom->qscratch_base, PWR_EVNT_IRQ_STAT_REG,
275 PWR_EVNT_LPM_IN_L2_MASK | PWR_EVNT_LPM_OUT_L2_MASK);
277 qcom->is_suspended = false;
282 static irqreturn_t qcom_dwc3_resume_irq(int irq, void *data)
284 struct dwc3_qcom *qcom = data;
285 struct dwc3 *dwc = platform_get_drvdata(qcom->dwc3);
287 /* If pm_suspended then let pm_resume take care of resuming h/w */
288 if (qcom->pm_suspended)
292 * This is safe as role switching is done from a freezable workqueue
293 * and the wakeup interrupts are disabled as part of resume.
295 if (dwc3_qcom_is_host(qcom))
296 pm_runtime_resume(&dwc->xhci->dev);
301 static void dwc3_qcom_select_utmi_clk(struct dwc3_qcom *qcom)
303 /* Configure dwc3 to use UTMI clock as PIPE clock not present */
304 dwc3_qcom_setbits(qcom->qscratch_base, QSCRATCH_GENERAL_CFG,
307 usleep_range(100, 1000);
309 dwc3_qcom_setbits(qcom->qscratch_base, QSCRATCH_GENERAL_CFG,
310 PIPE_UTMI_CLK_SEL | PIPE3_PHYSTATUS_SW);
312 usleep_range(100, 1000);
314 dwc3_qcom_clrbits(qcom->qscratch_base, QSCRATCH_GENERAL_CFG,
318 static int dwc3_qcom_setup_irq(struct platform_device *pdev)
320 struct dwc3_qcom *qcom = platform_get_drvdata(pdev);
323 irq = platform_get_irq_byname(pdev, "hs_phy_irq");
325 /* Keep wakeup interrupts disabled until suspend */
326 irq_set_status_flags(irq, IRQ_NOAUTOEN);
327 ret = devm_request_threaded_irq(qcom->dev, irq, NULL,
328 qcom_dwc3_resume_irq,
329 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
330 "qcom_dwc3 HS", qcom);
332 dev_err(qcom->dev, "hs_phy_irq failed: %d\n", ret);
335 qcom->hs_phy_irq = irq;
338 irq = platform_get_irq_byname(pdev, "dp_hs_phy_irq");
340 irq_set_status_flags(irq, IRQ_NOAUTOEN);
341 ret = devm_request_threaded_irq(qcom->dev, irq, NULL,
342 qcom_dwc3_resume_irq,
343 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
344 "qcom_dwc3 DP_HS", qcom);
346 dev_err(qcom->dev, "dp_hs_phy_irq failed: %d\n", ret);
349 qcom->dp_hs_phy_irq = irq;
352 irq = platform_get_irq_byname(pdev, "dm_hs_phy_irq");
354 irq_set_status_flags(irq, IRQ_NOAUTOEN);
355 ret = devm_request_threaded_irq(qcom->dev, irq, NULL,
356 qcom_dwc3_resume_irq,
357 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
358 "qcom_dwc3 DM_HS", qcom);
360 dev_err(qcom->dev, "dm_hs_phy_irq failed: %d\n", ret);
363 qcom->dm_hs_phy_irq = irq;
366 irq = platform_get_irq_byname(pdev, "ss_phy_irq");
368 irq_set_status_flags(irq, IRQ_NOAUTOEN);
369 ret = devm_request_threaded_irq(qcom->dev, irq, NULL,
370 qcom_dwc3_resume_irq,
371 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
372 "qcom_dwc3 SS", qcom);
374 dev_err(qcom->dev, "ss_phy_irq failed: %d\n", ret);
377 qcom->ss_phy_irq = irq;
383 static int dwc3_qcom_clk_init(struct dwc3_qcom *qcom, int count)
385 struct device *dev = qcom->dev;
386 struct device_node *np = dev->of_node;
389 qcom->num_clocks = count;
394 qcom->clks = devm_kcalloc(dev, qcom->num_clocks,
395 sizeof(struct clk *), GFP_KERNEL);
399 for (i = 0; i < qcom->num_clocks; i++) {
403 clk = of_clk_get(np, i);
406 clk_put(qcom->clks[i]);
410 ret = clk_prepare_enable(clk);
413 clk_disable_unprepare(qcom->clks[i]);
414 clk_put(qcom->clks[i]);
427 static int dwc3_qcom_probe(struct platform_device *pdev)
429 struct device_node *np = pdev->dev.of_node, *dwc3_np;
430 struct device *dev = &pdev->dev;
431 struct dwc3_qcom *qcom;
432 struct resource *res;
434 bool ignore_pipe_clk;
436 qcom = devm_kzalloc(&pdev->dev, sizeof(*qcom), GFP_KERNEL);
440 platform_set_drvdata(pdev, qcom);
441 qcom->dev = &pdev->dev;
443 qcom->resets = devm_reset_control_array_get_optional_exclusive(dev);
444 if (IS_ERR(qcom->resets)) {
445 ret = PTR_ERR(qcom->resets);
446 dev_err(&pdev->dev, "failed to get resets, err=%d\n", ret);
450 ret = reset_control_assert(qcom->resets);
452 dev_err(&pdev->dev, "failed to assert resets, err=%d\n", ret);
456 usleep_range(10, 1000);
458 ret = reset_control_deassert(qcom->resets);
460 dev_err(&pdev->dev, "failed to deassert resets, err=%d\n", ret);
464 ret = dwc3_qcom_clk_init(qcom, of_count_phandle_with_args(np,
465 "clocks", "#clock-cells"));
467 dev_err(dev, "failed to get clocks\n");
471 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
472 qcom->qscratch_base = devm_ioremap_resource(dev, res);
473 if (IS_ERR(qcom->qscratch_base)) {
474 dev_err(dev, "failed to map qscratch, err=%d\n", ret);
475 ret = PTR_ERR(qcom->qscratch_base);
479 ret = dwc3_qcom_setup_irq(pdev);
483 dwc3_np = of_get_child_by_name(np, "dwc3");
485 dev_err(dev, "failed to find dwc3 core child\n");
491 * Disable pipe_clk requirement if specified. Used when dwc3
492 * operates without SSPHY and only HS/FS/LS modes are supported.
494 ignore_pipe_clk = device_property_read_bool(dev,
495 "qcom,select-utmi-as-pipe-clk");
497 dwc3_qcom_select_utmi_clk(qcom);
499 ret = of_platform_populate(np, NULL, NULL, dev);
501 dev_err(dev, "failed to register dwc3 core - %d\n", ret);
505 qcom->dwc3 = of_find_device_by_node(dwc3_np);
507 dev_err(&pdev->dev, "failed to get dwc3 platform device\n");
512 qcom->mode = usb_get_dr_mode(&qcom->dwc3->dev);
514 /* enable vbus override for device mode */
515 if (qcom->mode != USB_DR_MODE_HOST)
516 dwc3_qcom_vbus_override_enable(qcom, true);
518 /* register extcon to override sw_vbus on Vbus change later */
519 ret = dwc3_qcom_register_extcon(qcom);
523 device_init_wakeup(&pdev->dev, 1);
524 qcom->is_suspended = false;
525 pm_runtime_set_active(dev);
526 pm_runtime_enable(dev);
527 pm_runtime_forbid(dev);
532 of_platform_depopulate(&pdev->dev);
534 for (i = qcom->num_clocks - 1; i >= 0; i--) {
535 clk_disable_unprepare(qcom->clks[i]);
536 clk_put(qcom->clks[i]);
539 reset_control_assert(qcom->resets);
544 static int dwc3_qcom_remove(struct platform_device *pdev)
546 struct dwc3_qcom *qcom = platform_get_drvdata(pdev);
547 struct device *dev = &pdev->dev;
550 of_platform_depopulate(dev);
552 for (i = qcom->num_clocks - 1; i >= 0; i--) {
553 clk_disable_unprepare(qcom->clks[i]);
554 clk_put(qcom->clks[i]);
556 qcom->num_clocks = 0;
558 reset_control_assert(qcom->resets);
560 pm_runtime_allow(dev);
561 pm_runtime_disable(dev);
566 static int __maybe_unused dwc3_qcom_pm_suspend(struct device *dev)
568 struct dwc3_qcom *qcom = dev_get_drvdata(dev);
571 ret = dwc3_qcom_suspend(qcom);
573 qcom->pm_suspended = true;
578 static int __maybe_unused dwc3_qcom_pm_resume(struct device *dev)
580 struct dwc3_qcom *qcom = dev_get_drvdata(dev);
583 ret = dwc3_qcom_resume(qcom);
585 qcom->pm_suspended = false;
590 static int __maybe_unused dwc3_qcom_runtime_suspend(struct device *dev)
592 struct dwc3_qcom *qcom = dev_get_drvdata(dev);
594 return dwc3_qcom_suspend(qcom);
597 static int __maybe_unused dwc3_qcom_runtime_resume(struct device *dev)
599 struct dwc3_qcom *qcom = dev_get_drvdata(dev);
601 return dwc3_qcom_resume(qcom);
604 static const struct dev_pm_ops dwc3_qcom_dev_pm_ops = {
605 SET_SYSTEM_SLEEP_PM_OPS(dwc3_qcom_pm_suspend, dwc3_qcom_pm_resume)
606 SET_RUNTIME_PM_OPS(dwc3_qcom_runtime_suspend, dwc3_qcom_runtime_resume,
610 static const struct of_device_id dwc3_qcom_of_match[] = {
611 { .compatible = "qcom,dwc3" },
612 { .compatible = "qcom,msm8996-dwc3" },
613 { .compatible = "qcom,sdm845-dwc3" },
616 MODULE_DEVICE_TABLE(of, dwc3_qcom_of_match);
618 static struct platform_driver dwc3_qcom_driver = {
619 .probe = dwc3_qcom_probe,
620 .remove = dwc3_qcom_remove,
623 .pm = &dwc3_qcom_dev_pm_ops,
624 .of_match_table = dwc3_qcom_of_match,
628 module_platform_driver(dwc3_qcom_driver);
630 MODULE_LICENSE("GPL v2");
631 MODULE_DESCRIPTION("DesignWare DWC3 QCOM Glue Driver");