Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Copyright (c) 2018, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * Inspired by dwc3-of-simple.c |
| 5 | */ |
| 6 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7 | #include <linux/acpi.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8 | #include <linux/io.h> |
| 9 | #include <linux/of.h> |
| 10 | #include <linux/clk.h> |
| 11 | #include <linux/irq.h> |
| 12 | #include <linux/clk-provider.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/extcon.h> |
| 16 | #include <linux/of_platform.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/phy/phy.h> |
| 19 | #include <linux/usb/of.h> |
| 20 | #include <linux/reset.h> |
| 21 | #include <linux/iopoll.h> |
| 22 | |
| 23 | #include "core.h" |
| 24 | |
| 25 | /* USB QSCRATCH Hardware registers */ |
| 26 | #define QSCRATCH_HS_PHY_CTRL 0x10 |
| 27 | #define UTMI_OTG_VBUS_VALID BIT(20) |
| 28 | #define SW_SESSVLD_SEL BIT(28) |
| 29 | |
| 30 | #define QSCRATCH_SS_PHY_CTRL 0x30 |
| 31 | #define LANE0_PWR_PRESENT BIT(24) |
| 32 | |
| 33 | #define QSCRATCH_GENERAL_CFG 0x08 |
| 34 | #define PIPE_UTMI_CLK_SEL BIT(0) |
| 35 | #define PIPE3_PHYSTATUS_SW BIT(3) |
| 36 | #define PIPE_UTMI_CLK_DIS BIT(8) |
| 37 | |
| 38 | #define PWR_EVNT_IRQ_STAT_REG 0x58 |
| 39 | #define PWR_EVNT_LPM_IN_L2_MASK BIT(4) |
| 40 | #define PWR_EVNT_LPM_OUT_L2_MASK BIT(5) |
| 41 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 42 | #define SDM845_QSCRATCH_BASE_OFFSET 0xf8800 |
| 43 | #define SDM845_QSCRATCH_SIZE 0x400 |
| 44 | #define SDM845_DWC3_CORE_SIZE 0xcd00 |
| 45 | |
| 46 | struct dwc3_acpi_pdata { |
| 47 | u32 qscratch_base_offset; |
| 48 | u32 qscratch_base_size; |
| 49 | u32 dwc3_core_base_size; |
| 50 | int hs_phy_irq_index; |
| 51 | int dp_hs_phy_irq_index; |
| 52 | int dm_hs_phy_irq_index; |
| 53 | int ss_phy_irq_index; |
| 54 | }; |
| 55 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 56 | struct dwc3_qcom { |
| 57 | struct device *dev; |
| 58 | void __iomem *qscratch_base; |
| 59 | struct platform_device *dwc3; |
| 60 | struct clk **clks; |
| 61 | int num_clocks; |
| 62 | struct reset_control *resets; |
| 63 | |
| 64 | int hs_phy_irq; |
| 65 | int dp_hs_phy_irq; |
| 66 | int dm_hs_phy_irq; |
| 67 | int ss_phy_irq; |
| 68 | |
| 69 | struct extcon_dev *edev; |
| 70 | struct extcon_dev *host_edev; |
| 71 | struct notifier_block vbus_nb; |
| 72 | struct notifier_block host_nb; |
| 73 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 74 | const struct dwc3_acpi_pdata *acpi_pdata; |
| 75 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 76 | enum usb_dr_mode mode; |
| 77 | bool is_suspended; |
| 78 | bool pm_suspended; |
| 79 | }; |
| 80 | |
| 81 | static inline void dwc3_qcom_setbits(void __iomem *base, u32 offset, u32 val) |
| 82 | { |
| 83 | u32 reg; |
| 84 | |
| 85 | reg = readl(base + offset); |
| 86 | reg |= val; |
| 87 | writel(reg, base + offset); |
| 88 | |
| 89 | /* ensure that above write is through */ |
| 90 | readl(base + offset); |
| 91 | } |
| 92 | |
| 93 | static inline void dwc3_qcom_clrbits(void __iomem *base, u32 offset, u32 val) |
| 94 | { |
| 95 | u32 reg; |
| 96 | |
| 97 | reg = readl(base + offset); |
| 98 | reg &= ~val; |
| 99 | writel(reg, base + offset); |
| 100 | |
| 101 | /* ensure that above write is through */ |
| 102 | readl(base + offset); |
| 103 | } |
| 104 | |
| 105 | static void dwc3_qcom_vbus_overrride_enable(struct dwc3_qcom *qcom, bool enable) |
| 106 | { |
| 107 | if (enable) { |
| 108 | dwc3_qcom_setbits(qcom->qscratch_base, QSCRATCH_SS_PHY_CTRL, |
| 109 | LANE0_PWR_PRESENT); |
| 110 | dwc3_qcom_setbits(qcom->qscratch_base, QSCRATCH_HS_PHY_CTRL, |
| 111 | UTMI_OTG_VBUS_VALID | SW_SESSVLD_SEL); |
| 112 | } else { |
| 113 | dwc3_qcom_clrbits(qcom->qscratch_base, QSCRATCH_SS_PHY_CTRL, |
| 114 | LANE0_PWR_PRESENT); |
| 115 | dwc3_qcom_clrbits(qcom->qscratch_base, QSCRATCH_HS_PHY_CTRL, |
| 116 | UTMI_OTG_VBUS_VALID | SW_SESSVLD_SEL); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | static int dwc3_qcom_vbus_notifier(struct notifier_block *nb, |
| 121 | unsigned long event, void *ptr) |
| 122 | { |
| 123 | struct dwc3_qcom *qcom = container_of(nb, struct dwc3_qcom, vbus_nb); |
| 124 | |
| 125 | /* enable vbus override for device mode */ |
| 126 | dwc3_qcom_vbus_overrride_enable(qcom, event); |
| 127 | qcom->mode = event ? USB_DR_MODE_PERIPHERAL : USB_DR_MODE_HOST; |
| 128 | |
| 129 | return NOTIFY_DONE; |
| 130 | } |
| 131 | |
| 132 | static int dwc3_qcom_host_notifier(struct notifier_block *nb, |
| 133 | unsigned long event, void *ptr) |
| 134 | { |
| 135 | struct dwc3_qcom *qcom = container_of(nb, struct dwc3_qcom, host_nb); |
| 136 | |
| 137 | /* disable vbus override in host mode */ |
| 138 | dwc3_qcom_vbus_overrride_enable(qcom, !event); |
| 139 | qcom->mode = event ? USB_DR_MODE_HOST : USB_DR_MODE_PERIPHERAL; |
| 140 | |
| 141 | return NOTIFY_DONE; |
| 142 | } |
| 143 | |
| 144 | static int dwc3_qcom_register_extcon(struct dwc3_qcom *qcom) |
| 145 | { |
| 146 | struct device *dev = qcom->dev; |
| 147 | struct extcon_dev *host_edev; |
| 148 | int ret; |
| 149 | |
| 150 | if (!of_property_read_bool(dev->of_node, "extcon")) |
| 151 | return 0; |
| 152 | |
| 153 | qcom->edev = extcon_get_edev_by_phandle(dev, 0); |
| 154 | if (IS_ERR(qcom->edev)) |
| 155 | return PTR_ERR(qcom->edev); |
| 156 | |
| 157 | qcom->vbus_nb.notifier_call = dwc3_qcom_vbus_notifier; |
| 158 | |
| 159 | qcom->host_edev = extcon_get_edev_by_phandle(dev, 1); |
| 160 | if (IS_ERR(qcom->host_edev)) |
| 161 | qcom->host_edev = NULL; |
| 162 | |
| 163 | ret = devm_extcon_register_notifier(dev, qcom->edev, EXTCON_USB, |
| 164 | &qcom->vbus_nb); |
| 165 | if (ret < 0) { |
| 166 | dev_err(dev, "VBUS notifier register failed\n"); |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | if (qcom->host_edev) |
| 171 | host_edev = qcom->host_edev; |
| 172 | else |
| 173 | host_edev = qcom->edev; |
| 174 | |
| 175 | qcom->host_nb.notifier_call = dwc3_qcom_host_notifier; |
| 176 | ret = devm_extcon_register_notifier(dev, host_edev, EXTCON_USB_HOST, |
| 177 | &qcom->host_nb); |
| 178 | if (ret < 0) { |
| 179 | dev_err(dev, "Host notifier register failed\n"); |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | /* Update initial VBUS override based on extcon state */ |
| 184 | if (extcon_get_state(qcom->edev, EXTCON_USB) || |
| 185 | !extcon_get_state(host_edev, EXTCON_USB_HOST)) |
| 186 | dwc3_qcom_vbus_notifier(&qcom->vbus_nb, true, qcom->edev); |
| 187 | else |
| 188 | dwc3_qcom_vbus_notifier(&qcom->vbus_nb, false, qcom->edev); |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static void dwc3_qcom_disable_interrupts(struct dwc3_qcom *qcom) |
| 194 | { |
| 195 | if (qcom->hs_phy_irq) { |
| 196 | disable_irq_wake(qcom->hs_phy_irq); |
| 197 | disable_irq_nosync(qcom->hs_phy_irq); |
| 198 | } |
| 199 | |
| 200 | if (qcom->dp_hs_phy_irq) { |
| 201 | disable_irq_wake(qcom->dp_hs_phy_irq); |
| 202 | disable_irq_nosync(qcom->dp_hs_phy_irq); |
| 203 | } |
| 204 | |
| 205 | if (qcom->dm_hs_phy_irq) { |
| 206 | disable_irq_wake(qcom->dm_hs_phy_irq); |
| 207 | disable_irq_nosync(qcom->dm_hs_phy_irq); |
| 208 | } |
| 209 | |
| 210 | if (qcom->ss_phy_irq) { |
| 211 | disable_irq_wake(qcom->ss_phy_irq); |
| 212 | disable_irq_nosync(qcom->ss_phy_irq); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | static void dwc3_qcom_enable_interrupts(struct dwc3_qcom *qcom) |
| 217 | { |
| 218 | if (qcom->hs_phy_irq) { |
| 219 | enable_irq(qcom->hs_phy_irq); |
| 220 | enable_irq_wake(qcom->hs_phy_irq); |
| 221 | } |
| 222 | |
| 223 | if (qcom->dp_hs_phy_irq) { |
| 224 | enable_irq(qcom->dp_hs_phy_irq); |
| 225 | enable_irq_wake(qcom->dp_hs_phy_irq); |
| 226 | } |
| 227 | |
| 228 | if (qcom->dm_hs_phy_irq) { |
| 229 | enable_irq(qcom->dm_hs_phy_irq); |
| 230 | enable_irq_wake(qcom->dm_hs_phy_irq); |
| 231 | } |
| 232 | |
| 233 | if (qcom->ss_phy_irq) { |
| 234 | enable_irq(qcom->ss_phy_irq); |
| 235 | enable_irq_wake(qcom->ss_phy_irq); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | static int dwc3_qcom_suspend(struct dwc3_qcom *qcom) |
| 240 | { |
| 241 | u32 val; |
| 242 | int i; |
| 243 | |
| 244 | if (qcom->is_suspended) |
| 245 | return 0; |
| 246 | |
| 247 | val = readl(qcom->qscratch_base + PWR_EVNT_IRQ_STAT_REG); |
| 248 | if (!(val & PWR_EVNT_LPM_IN_L2_MASK)) |
| 249 | dev_err(qcom->dev, "HS-PHY not in L2\n"); |
| 250 | |
| 251 | for (i = qcom->num_clocks - 1; i >= 0; i--) |
| 252 | clk_disable_unprepare(qcom->clks[i]); |
| 253 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 254 | if (device_may_wakeup(qcom->dev)) |
| 255 | dwc3_qcom_enable_interrupts(qcom); |
| 256 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 257 | qcom->is_suspended = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static int dwc3_qcom_resume(struct dwc3_qcom *qcom) |
| 263 | { |
| 264 | int ret; |
| 265 | int i; |
| 266 | |
| 267 | if (!qcom->is_suspended) |
| 268 | return 0; |
| 269 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 270 | if (device_may_wakeup(qcom->dev)) |
| 271 | dwc3_qcom_disable_interrupts(qcom); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 272 | |
| 273 | for (i = 0; i < qcom->num_clocks; i++) { |
| 274 | ret = clk_prepare_enable(qcom->clks[i]); |
| 275 | if (ret < 0) { |
| 276 | while (--i >= 0) |
| 277 | clk_disable_unprepare(qcom->clks[i]); |
| 278 | return ret; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /* Clear existing events from PHY related to L2 in/out */ |
| 283 | dwc3_qcom_setbits(qcom->qscratch_base, PWR_EVNT_IRQ_STAT_REG, |
| 284 | PWR_EVNT_LPM_IN_L2_MASK | PWR_EVNT_LPM_OUT_L2_MASK); |
| 285 | |
| 286 | qcom->is_suspended = false; |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | static irqreturn_t qcom_dwc3_resume_irq(int irq, void *data) |
| 292 | { |
| 293 | struct dwc3_qcom *qcom = data; |
| 294 | struct dwc3 *dwc = platform_get_drvdata(qcom->dwc3); |
| 295 | |
| 296 | /* If pm_suspended then let pm_resume take care of resuming h/w */ |
| 297 | if (qcom->pm_suspended) |
| 298 | return IRQ_HANDLED; |
| 299 | |
| 300 | if (dwc->xhci) |
| 301 | pm_runtime_resume(&dwc->xhci->dev); |
| 302 | |
| 303 | return IRQ_HANDLED; |
| 304 | } |
| 305 | |
| 306 | static void dwc3_qcom_select_utmi_clk(struct dwc3_qcom *qcom) |
| 307 | { |
| 308 | /* Configure dwc3 to use UTMI clock as PIPE clock not present */ |
| 309 | dwc3_qcom_setbits(qcom->qscratch_base, QSCRATCH_GENERAL_CFG, |
| 310 | PIPE_UTMI_CLK_DIS); |
| 311 | |
| 312 | usleep_range(100, 1000); |
| 313 | |
| 314 | dwc3_qcom_setbits(qcom->qscratch_base, QSCRATCH_GENERAL_CFG, |
| 315 | PIPE_UTMI_CLK_SEL | PIPE3_PHYSTATUS_SW); |
| 316 | |
| 317 | usleep_range(100, 1000); |
| 318 | |
| 319 | dwc3_qcom_clrbits(qcom->qscratch_base, QSCRATCH_GENERAL_CFG, |
| 320 | PIPE_UTMI_CLK_DIS); |
| 321 | } |
| 322 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 323 | static int dwc3_qcom_get_irq(struct platform_device *pdev, |
| 324 | const char *name, int num) |
| 325 | { |
| 326 | struct device_node *np = pdev->dev.of_node; |
| 327 | int ret; |
| 328 | |
| 329 | if (np) |
| 330 | ret = platform_get_irq_byname(pdev, name); |
| 331 | else |
| 332 | ret = platform_get_irq(pdev, num); |
| 333 | |
| 334 | return ret; |
| 335 | } |
| 336 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 337 | static int dwc3_qcom_setup_irq(struct platform_device *pdev) |
| 338 | { |
| 339 | struct dwc3_qcom *qcom = platform_get_drvdata(pdev); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 340 | const struct dwc3_acpi_pdata *pdata = qcom->acpi_pdata; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 341 | int irq, ret; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 342 | irq = dwc3_qcom_get_irq(pdev, "hs_phy_irq", |
| 343 | pdata ? pdata->hs_phy_irq_index : -1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 344 | if (irq > 0) { |
| 345 | /* Keep wakeup interrupts disabled until suspend */ |
| 346 | irq_set_status_flags(irq, IRQ_NOAUTOEN); |
| 347 | ret = devm_request_threaded_irq(qcom->dev, irq, NULL, |
| 348 | qcom_dwc3_resume_irq, |
| 349 | IRQF_TRIGGER_HIGH | IRQF_ONESHOT, |
| 350 | "qcom_dwc3 HS", qcom); |
| 351 | if (ret) { |
| 352 | dev_err(qcom->dev, "hs_phy_irq failed: %d\n", ret); |
| 353 | return ret; |
| 354 | } |
| 355 | qcom->hs_phy_irq = irq; |
| 356 | } |
| 357 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 358 | irq = dwc3_qcom_get_irq(pdev, "dp_hs_phy_irq", |
| 359 | pdata ? pdata->dp_hs_phy_irq_index : -1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 360 | if (irq > 0) { |
| 361 | irq_set_status_flags(irq, IRQ_NOAUTOEN); |
| 362 | ret = devm_request_threaded_irq(qcom->dev, irq, NULL, |
| 363 | qcom_dwc3_resume_irq, |
| 364 | IRQF_TRIGGER_HIGH | IRQF_ONESHOT, |
| 365 | "qcom_dwc3 DP_HS", qcom); |
| 366 | if (ret) { |
| 367 | dev_err(qcom->dev, "dp_hs_phy_irq failed: %d\n", ret); |
| 368 | return ret; |
| 369 | } |
| 370 | qcom->dp_hs_phy_irq = irq; |
| 371 | } |
| 372 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 373 | irq = dwc3_qcom_get_irq(pdev, "dm_hs_phy_irq", |
| 374 | pdata ? pdata->dm_hs_phy_irq_index : -1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 375 | if (irq > 0) { |
| 376 | irq_set_status_flags(irq, IRQ_NOAUTOEN); |
| 377 | ret = devm_request_threaded_irq(qcom->dev, irq, NULL, |
| 378 | qcom_dwc3_resume_irq, |
| 379 | IRQF_TRIGGER_HIGH | IRQF_ONESHOT, |
| 380 | "qcom_dwc3 DM_HS", qcom); |
| 381 | if (ret) { |
| 382 | dev_err(qcom->dev, "dm_hs_phy_irq failed: %d\n", ret); |
| 383 | return ret; |
| 384 | } |
| 385 | qcom->dm_hs_phy_irq = irq; |
| 386 | } |
| 387 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 388 | irq = dwc3_qcom_get_irq(pdev, "ss_phy_irq", |
| 389 | pdata ? pdata->ss_phy_irq_index : -1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 390 | if (irq > 0) { |
| 391 | irq_set_status_flags(irq, IRQ_NOAUTOEN); |
| 392 | ret = devm_request_threaded_irq(qcom->dev, irq, NULL, |
| 393 | qcom_dwc3_resume_irq, |
| 394 | IRQF_TRIGGER_HIGH | IRQF_ONESHOT, |
| 395 | "qcom_dwc3 SS", qcom); |
| 396 | if (ret) { |
| 397 | dev_err(qcom->dev, "ss_phy_irq failed: %d\n", ret); |
| 398 | return ret; |
| 399 | } |
| 400 | qcom->ss_phy_irq = irq; |
| 401 | } |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static int dwc3_qcom_clk_init(struct dwc3_qcom *qcom, int count) |
| 407 | { |
| 408 | struct device *dev = qcom->dev; |
| 409 | struct device_node *np = dev->of_node; |
| 410 | int i; |
| 411 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 412 | if (!np || !count) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 413 | return 0; |
| 414 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 415 | if (count < 0) |
| 416 | return count; |
| 417 | |
| 418 | qcom->num_clocks = count; |
| 419 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 420 | qcom->clks = devm_kcalloc(dev, qcom->num_clocks, |
| 421 | sizeof(struct clk *), GFP_KERNEL); |
| 422 | if (!qcom->clks) |
| 423 | return -ENOMEM; |
| 424 | |
| 425 | for (i = 0; i < qcom->num_clocks; i++) { |
| 426 | struct clk *clk; |
| 427 | int ret; |
| 428 | |
| 429 | clk = of_clk_get(np, i); |
| 430 | if (IS_ERR(clk)) { |
| 431 | while (--i >= 0) |
| 432 | clk_put(qcom->clks[i]); |
| 433 | return PTR_ERR(clk); |
| 434 | } |
| 435 | |
| 436 | ret = clk_prepare_enable(clk); |
| 437 | if (ret < 0) { |
| 438 | while (--i >= 0) { |
| 439 | clk_disable_unprepare(qcom->clks[i]); |
| 440 | clk_put(qcom->clks[i]); |
| 441 | } |
| 442 | clk_put(clk); |
| 443 | |
| 444 | return ret; |
| 445 | } |
| 446 | |
| 447 | qcom->clks[i] = clk; |
| 448 | } |
| 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 453 | static const struct property_entry dwc3_qcom_acpi_properties[] = { |
| 454 | PROPERTY_ENTRY_STRING("dr_mode", "host"), |
| 455 | {} |
| 456 | }; |
| 457 | |
| 458 | static int dwc3_qcom_acpi_register_core(struct platform_device *pdev) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 459 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 460 | struct dwc3_qcom *qcom = platform_get_drvdata(pdev); |
| 461 | struct device *dev = &pdev->dev; |
| 462 | struct resource *res, *child_res = NULL; |
| 463 | int irq; |
| 464 | int ret; |
| 465 | |
| 466 | qcom->dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO); |
| 467 | if (!qcom->dwc3) |
| 468 | return -ENOMEM; |
| 469 | |
| 470 | qcom->dwc3->dev.parent = dev; |
| 471 | qcom->dwc3->dev.type = dev->type; |
| 472 | qcom->dwc3->dev.dma_mask = dev->dma_mask; |
| 473 | qcom->dwc3->dev.dma_parms = dev->dma_parms; |
| 474 | qcom->dwc3->dev.coherent_dma_mask = dev->coherent_dma_mask; |
| 475 | |
| 476 | child_res = kcalloc(2, sizeof(*child_res), GFP_KERNEL); |
| 477 | if (!child_res) |
| 478 | return -ENOMEM; |
| 479 | |
| 480 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 481 | if (!res) { |
| 482 | dev_err(&pdev->dev, "failed to get memory resource\n"); |
| 483 | ret = -ENODEV; |
| 484 | goto out; |
| 485 | } |
| 486 | |
| 487 | child_res[0].flags = res->flags; |
| 488 | child_res[0].start = res->start; |
| 489 | child_res[0].end = child_res[0].start + |
| 490 | qcom->acpi_pdata->dwc3_core_base_size; |
| 491 | |
| 492 | irq = platform_get_irq(pdev, 0); |
| 493 | child_res[1].flags = IORESOURCE_IRQ; |
| 494 | child_res[1].start = child_res[1].end = irq; |
| 495 | |
| 496 | ret = platform_device_add_resources(qcom->dwc3, child_res, 2); |
| 497 | if (ret) { |
| 498 | dev_err(&pdev->dev, "failed to add resources\n"); |
| 499 | goto out; |
| 500 | } |
| 501 | |
| 502 | ret = platform_device_add_properties(qcom->dwc3, |
| 503 | dwc3_qcom_acpi_properties); |
| 504 | if (ret < 0) { |
| 505 | dev_err(&pdev->dev, "failed to add properties\n"); |
| 506 | goto out; |
| 507 | } |
| 508 | |
| 509 | ret = platform_device_add(qcom->dwc3); |
| 510 | if (ret) |
| 511 | dev_err(&pdev->dev, "failed to add device\n"); |
| 512 | |
| 513 | out: |
| 514 | kfree(child_res); |
| 515 | return ret; |
| 516 | } |
| 517 | |
| 518 | static int dwc3_qcom_of_register_core(struct platform_device *pdev) |
| 519 | { |
| 520 | struct dwc3_qcom *qcom = platform_get_drvdata(pdev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 521 | struct device_node *np = pdev->dev.of_node, *dwc3_np; |
| 522 | struct device *dev = &pdev->dev; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 523 | int ret; |
| 524 | |
| 525 | dwc3_np = of_get_child_by_name(np, "dwc3"); |
| 526 | if (!dwc3_np) { |
| 527 | dev_err(dev, "failed to find dwc3 core child\n"); |
| 528 | return -ENODEV; |
| 529 | } |
| 530 | |
| 531 | ret = of_platform_populate(np, NULL, NULL, dev); |
| 532 | if (ret) { |
| 533 | dev_err(dev, "failed to register dwc3 core - %d\n", ret); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 534 | goto node_put; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | qcom->dwc3 = of_find_device_by_node(dwc3_np); |
| 538 | if (!qcom->dwc3) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 539 | ret = -ENODEV; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 540 | dev_err(dev, "failed to get dwc3 platform device\n"); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 543 | node_put: |
| 544 | of_node_put(dwc3_np); |
| 545 | |
| 546 | return ret; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | static const struct dwc3_acpi_pdata sdm845_acpi_pdata = { |
| 550 | .qscratch_base_offset = SDM845_QSCRATCH_BASE_OFFSET, |
| 551 | .qscratch_base_size = SDM845_QSCRATCH_SIZE, |
| 552 | .dwc3_core_base_size = SDM845_DWC3_CORE_SIZE, |
| 553 | .hs_phy_irq_index = 1, |
| 554 | .dp_hs_phy_irq_index = 4, |
| 555 | .dm_hs_phy_irq_index = 3, |
| 556 | .ss_phy_irq_index = 2 |
| 557 | }; |
| 558 | |
| 559 | static int dwc3_qcom_probe(struct platform_device *pdev) |
| 560 | { |
| 561 | struct device_node *np = pdev->dev.of_node; |
| 562 | struct device *dev = &pdev->dev; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 563 | struct dwc3_qcom *qcom; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 564 | struct resource *res, *parent_res = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 565 | int ret, i; |
| 566 | bool ignore_pipe_clk; |
| 567 | |
| 568 | qcom = devm_kzalloc(&pdev->dev, sizeof(*qcom), GFP_KERNEL); |
| 569 | if (!qcom) |
| 570 | return -ENOMEM; |
| 571 | |
| 572 | platform_set_drvdata(pdev, qcom); |
| 573 | qcom->dev = &pdev->dev; |
| 574 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 575 | if (has_acpi_companion(dev)) { |
| 576 | qcom->acpi_pdata = acpi_device_get_match_data(dev); |
| 577 | if (!qcom->acpi_pdata) { |
| 578 | dev_err(&pdev->dev, "no supporting ACPI device data\n"); |
| 579 | return -EINVAL; |
| 580 | } |
| 581 | } |
| 582 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 583 | qcom->resets = devm_reset_control_array_get_optional_exclusive(dev); |
| 584 | if (IS_ERR(qcom->resets)) { |
| 585 | ret = PTR_ERR(qcom->resets); |
| 586 | dev_err(&pdev->dev, "failed to get resets, err=%d\n", ret); |
| 587 | return ret; |
| 588 | } |
| 589 | |
| 590 | ret = reset_control_assert(qcom->resets); |
| 591 | if (ret) { |
| 592 | dev_err(&pdev->dev, "failed to assert resets, err=%d\n", ret); |
| 593 | return ret; |
| 594 | } |
| 595 | |
| 596 | usleep_range(10, 1000); |
| 597 | |
| 598 | ret = reset_control_deassert(qcom->resets); |
| 599 | if (ret) { |
| 600 | dev_err(&pdev->dev, "failed to deassert resets, err=%d\n", ret); |
| 601 | goto reset_assert; |
| 602 | } |
| 603 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 604 | ret = dwc3_qcom_clk_init(qcom, of_clk_get_parent_count(np)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 605 | if (ret) { |
| 606 | dev_err(dev, "failed to get clocks\n"); |
| 607 | goto reset_assert; |
| 608 | } |
| 609 | |
| 610 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 611 | |
| 612 | if (np) { |
| 613 | parent_res = res; |
| 614 | } else { |
| 615 | parent_res = kmemdup(res, sizeof(struct resource), GFP_KERNEL); |
| 616 | if (!parent_res) |
| 617 | return -ENOMEM; |
| 618 | |
| 619 | parent_res->start = res->start + |
| 620 | qcom->acpi_pdata->qscratch_base_offset; |
| 621 | parent_res->end = parent_res->start + |
| 622 | qcom->acpi_pdata->qscratch_base_size; |
| 623 | } |
| 624 | |
| 625 | qcom->qscratch_base = devm_ioremap_resource(dev, parent_res); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 626 | if (IS_ERR(qcom->qscratch_base)) { |
| 627 | dev_err(dev, "failed to map qscratch, err=%d\n", ret); |
| 628 | ret = PTR_ERR(qcom->qscratch_base); |
| 629 | goto clk_disable; |
| 630 | } |
| 631 | |
| 632 | ret = dwc3_qcom_setup_irq(pdev); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 633 | if (ret) { |
| 634 | dev_err(dev, "failed to setup IRQs, err=%d\n", ret); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 635 | goto clk_disable; |
| 636 | } |
| 637 | |
| 638 | /* |
| 639 | * Disable pipe_clk requirement if specified. Used when dwc3 |
| 640 | * operates without SSPHY and only HS/FS/LS modes are supported. |
| 641 | */ |
| 642 | ignore_pipe_clk = device_property_read_bool(dev, |
| 643 | "qcom,select-utmi-as-pipe-clk"); |
| 644 | if (ignore_pipe_clk) |
| 645 | dwc3_qcom_select_utmi_clk(qcom); |
| 646 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 647 | if (np) |
| 648 | ret = dwc3_qcom_of_register_core(pdev); |
| 649 | else |
| 650 | ret = dwc3_qcom_acpi_register_core(pdev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 651 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 652 | if (ret) { |
| 653 | dev_err(dev, "failed to register DWC3 Core, err=%d\n", ret); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 654 | goto depopulate; |
| 655 | } |
| 656 | |
| 657 | qcom->mode = usb_get_dr_mode(&qcom->dwc3->dev); |
| 658 | |
| 659 | /* enable vbus override for device mode */ |
| 660 | if (qcom->mode == USB_DR_MODE_PERIPHERAL) |
| 661 | dwc3_qcom_vbus_overrride_enable(qcom, true); |
| 662 | |
| 663 | /* register extcon to override sw_vbus on Vbus change later */ |
| 664 | ret = dwc3_qcom_register_extcon(qcom); |
| 665 | if (ret) |
| 666 | goto depopulate; |
| 667 | |
| 668 | device_init_wakeup(&pdev->dev, 1); |
| 669 | qcom->is_suspended = false; |
| 670 | pm_runtime_set_active(dev); |
| 671 | pm_runtime_enable(dev); |
| 672 | pm_runtime_forbid(dev); |
| 673 | |
| 674 | return 0; |
| 675 | |
| 676 | depopulate: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 677 | if (np) |
| 678 | of_platform_depopulate(&pdev->dev); |
| 679 | else |
| 680 | platform_device_put(pdev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 681 | clk_disable: |
| 682 | for (i = qcom->num_clocks - 1; i >= 0; i--) { |
| 683 | clk_disable_unprepare(qcom->clks[i]); |
| 684 | clk_put(qcom->clks[i]); |
| 685 | } |
| 686 | reset_assert: |
| 687 | reset_control_assert(qcom->resets); |
| 688 | |
| 689 | return ret; |
| 690 | } |
| 691 | |
| 692 | static int dwc3_qcom_remove(struct platform_device *pdev) |
| 693 | { |
| 694 | struct dwc3_qcom *qcom = platform_get_drvdata(pdev); |
| 695 | struct device *dev = &pdev->dev; |
| 696 | int i; |
| 697 | |
| 698 | of_platform_depopulate(dev); |
| 699 | |
| 700 | for (i = qcom->num_clocks - 1; i >= 0; i--) { |
| 701 | clk_disable_unprepare(qcom->clks[i]); |
| 702 | clk_put(qcom->clks[i]); |
| 703 | } |
| 704 | qcom->num_clocks = 0; |
| 705 | |
| 706 | reset_control_assert(qcom->resets); |
| 707 | |
| 708 | pm_runtime_allow(dev); |
| 709 | pm_runtime_disable(dev); |
| 710 | |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | static int __maybe_unused dwc3_qcom_pm_suspend(struct device *dev) |
| 715 | { |
| 716 | struct dwc3_qcom *qcom = dev_get_drvdata(dev); |
| 717 | int ret = 0; |
| 718 | |
| 719 | ret = dwc3_qcom_suspend(qcom); |
| 720 | if (!ret) |
| 721 | qcom->pm_suspended = true; |
| 722 | |
| 723 | return ret; |
| 724 | } |
| 725 | |
| 726 | static int __maybe_unused dwc3_qcom_pm_resume(struct device *dev) |
| 727 | { |
| 728 | struct dwc3_qcom *qcom = dev_get_drvdata(dev); |
| 729 | int ret; |
| 730 | |
| 731 | ret = dwc3_qcom_resume(qcom); |
| 732 | if (!ret) |
| 733 | qcom->pm_suspended = false; |
| 734 | |
| 735 | return ret; |
| 736 | } |
| 737 | |
| 738 | static int __maybe_unused dwc3_qcom_runtime_suspend(struct device *dev) |
| 739 | { |
| 740 | struct dwc3_qcom *qcom = dev_get_drvdata(dev); |
| 741 | |
| 742 | return dwc3_qcom_suspend(qcom); |
| 743 | } |
| 744 | |
| 745 | static int __maybe_unused dwc3_qcom_runtime_resume(struct device *dev) |
| 746 | { |
| 747 | struct dwc3_qcom *qcom = dev_get_drvdata(dev); |
| 748 | |
| 749 | return dwc3_qcom_resume(qcom); |
| 750 | } |
| 751 | |
| 752 | static const struct dev_pm_ops dwc3_qcom_dev_pm_ops = { |
| 753 | SET_SYSTEM_SLEEP_PM_OPS(dwc3_qcom_pm_suspend, dwc3_qcom_pm_resume) |
| 754 | SET_RUNTIME_PM_OPS(dwc3_qcom_runtime_suspend, dwc3_qcom_runtime_resume, |
| 755 | NULL) |
| 756 | }; |
| 757 | |
| 758 | static const struct of_device_id dwc3_qcom_of_match[] = { |
| 759 | { .compatible = "qcom,dwc3" }, |
| 760 | { .compatible = "qcom,msm8996-dwc3" }, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 761 | { .compatible = "qcom,msm8998-dwc3" }, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 762 | { .compatible = "qcom,sdm845-dwc3" }, |
| 763 | { } |
| 764 | }; |
| 765 | MODULE_DEVICE_TABLE(of, dwc3_qcom_of_match); |
| 766 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 767 | static const struct acpi_device_id dwc3_qcom_acpi_match[] = { |
| 768 | { "QCOM2430", (unsigned long)&sdm845_acpi_pdata }, |
| 769 | { }, |
| 770 | }; |
| 771 | MODULE_DEVICE_TABLE(acpi, dwc3_qcom_acpi_match); |
| 772 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 773 | static struct platform_driver dwc3_qcom_driver = { |
| 774 | .probe = dwc3_qcom_probe, |
| 775 | .remove = dwc3_qcom_remove, |
| 776 | .driver = { |
| 777 | .name = "dwc3-qcom", |
| 778 | .pm = &dwc3_qcom_dev_pm_ops, |
| 779 | .of_match_table = dwc3_qcom_of_match, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 780 | .acpi_match_table = ACPI_PTR(dwc3_qcom_acpi_match), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 781 | }, |
| 782 | }; |
| 783 | |
| 784 | module_platform_driver(dwc3_qcom_driver); |
| 785 | |
| 786 | MODULE_LICENSE("GPL v2"); |
| 787 | MODULE_DESCRIPTION("DesignWare DWC3 QCOM Glue Driver"); |