15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 227088e00SAditya Srivastava /* 372246da4SFelipe Balbi * dwc3-pci.c - PCI Specific glue layer 472246da4SFelipe Balbi * 510623b87SAlexander A. Klimov * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com 672246da4SFelipe Balbi * 772246da4SFelipe Balbi * Authors: Felipe Balbi <balbi@ti.com>, 872246da4SFelipe Balbi * Sebastian Andrzej Siewior <bigeasy@linutronix.de> 972246da4SFelipe Balbi */ 1072246da4SFelipe Balbi 1172246da4SFelipe Balbi #include <linux/kernel.h> 1246a57283SStephen Rothwell #include <linux/module.h> 1372246da4SFelipe Balbi #include <linux/slab.h> 1472246da4SFelipe Balbi #include <linux/pci.h> 158eed00b2SManu Gautam #include <linux/workqueue.h> 16e9af9229SFelipe Balbi #include <linux/pm_runtime.h> 1772246da4SFelipe Balbi #include <linux/platform_device.h> 18a89d977cSHeikki Krogerus #include <linux/gpio/consumer.h> 195741022cSHans de Goede #include <linux/gpio/machine.h> 20a89d977cSHeikki Krogerus #include <linux/acpi.h> 21cf48305dSHeikki Krogerus #include <linux/delay.h> 228f317b47SHuang Rui 23b62cd96dSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_BYT 0x0f37 2485601f8cSDavid Cohen #define PCI_DEVICE_ID_INTEL_MRFLD 0x119e 259a5a0783SJohn Youn #define PCI_DEVICE_ID_INTEL_BSW 0x22b7 2684a2b61bSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_SPTLP 0x9d30 2784a2b61bSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_SPTH 0xa130 28b4c580a4SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_BXT 0x0aaa 291ffb4d5cSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_BXT_M 0x1aaa 30b4c580a4SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_APL 0x5aaa 314491ed50SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_KBP 0xa2b0 323c3caae4SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_CMLLP 0x02ee 333c3caae4SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_CMLH 0x06ee 348f8983a5SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_GLK 0x31aa 3568217959SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_CNPLP 0x9dee 3668217959SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_CNPH 0xa36e 37f5ae8869SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_CNPV 0xa3b0 3800908693SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_ICLLP 0x34ee 39457d2285SRaymond Tan #define PCI_DEVICE_ID_INTEL_EHL 0x4b7e 40b3649deeSFelipe Balbi #define PCI_DEVICE_ID_INTEL_TGPLP 0xa0ee 41c3f595a8SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_TGPH 0x43ee 42e25d1e85SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_JSP 0x4dee 43ecb0a2f1SShruthi Sanil #define PCI_DEVICE_ID_INTEL_ADL 0x465e 44f08fc2c3SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_ADLP 0x51ee 45782de5e7SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_ADLM 0x54ee 461384ab4fSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_ADLS 0x7ae1 47*bad0d1d7SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_RPL 0x460e 48038438a2SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_RPLS 0x7a61 49973e0f7aSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_MTLP 0x7ec1 50973e0f7aSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_MTL 0x7e7e 5173203bdeSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_TGL 0x9a15 521abade64SNehal Bakulchandra Shah #define PCI_DEVICE_ID_AMD_MR 0x163a 5372246da4SFelipe Balbi 5494116f81SAndy Shevchenko #define PCI_INTEL_BXT_DSM_GUID "732b85d5-b7a7-4a1b-9ba0-4bbd00ffd511" 559cecca75SFelipe Balbi #define PCI_INTEL_BXT_FUNC_PMU_PWR 4 569cecca75SFelipe Balbi #define PCI_INTEL_BXT_STATE_D0 0 579cecca75SFelipe Balbi #define PCI_INTEL_BXT_STATE_D3 3 589cecca75SFelipe Balbi 597740d04dSHans de Goede #define GP_RWBAR 1 607740d04dSHans de Goede #define GP_RWREG1 0xa0 617740d04dSHans de Goede #define GP_RWREG1_ULPI_REFCLK_DISABLE (1 << 17) 627740d04dSHans de Goede 630f817ae6SFelipe Balbi /** 640f817ae6SFelipe Balbi * struct dwc3_pci - Driver private structure 650f817ae6SFelipe Balbi * @dwc3: child dwc3 platform_device 660f817ae6SFelipe Balbi * @pci: our link to PCI bus 6794116f81SAndy Shevchenko * @guid: _DSM GUID 689cecca75SFelipe Balbi * @has_dsm_for_pm: true for devices which need to run _DSM on runtime PM 6987d852deSAndy Shevchenko * @wakeup_work: work for asynchronous resume 700f817ae6SFelipe Balbi */ 710f817ae6SFelipe Balbi struct dwc3_pci { 720f817ae6SFelipe Balbi struct platform_device *dwc3; 730f817ae6SFelipe Balbi struct pci_dev *pci; 749cecca75SFelipe Balbi 7594116f81SAndy Shevchenko guid_t guid; 769cecca75SFelipe Balbi 779cecca75SFelipe Balbi unsigned int has_dsm_for_pm:1; 788eed00b2SManu Gautam struct work_struct wakeup_work; 790f817ae6SFelipe Balbi }; 800f817ae6SFelipe Balbi 81a89d977cSHeikki Krogerus static const struct acpi_gpio_params reset_gpios = { 0, 0, false }; 82a89d977cSHeikki Krogerus static const struct acpi_gpio_params cs_gpios = { 1, 0, false }; 83a89d977cSHeikki Krogerus 84a89d977cSHeikki Krogerus static const struct acpi_gpio_mapping acpi_dwc3_byt_gpios[] = { 85a89d977cSHeikki Krogerus { "reset-gpios", &reset_gpios, 1 }, 86a89d977cSHeikki Krogerus { "cs-gpios", &cs_gpios, 1 }, 87a89d977cSHeikki Krogerus { }, 88a89d977cSHeikki Krogerus }; 89a89d977cSHeikki Krogerus 905741022cSHans de Goede static struct gpiod_lookup_table platform_bytcr_gpios = { 915741022cSHans de Goede .dev_id = "0000:00:16.0", 925741022cSHans de Goede .table = { 9362e3f0afSHans de Goede GPIO_LOOKUP("INT33FC:00", 54, "cs", GPIO_ACTIVE_HIGH), 9462e3f0afSHans de Goede GPIO_LOOKUP("INT33FC:02", 14, "reset", GPIO_ACTIVE_HIGH), 955741022cSHans de Goede {} 965741022cSHans de Goede }, 975741022cSHans de Goede }; 985741022cSHans de Goede 997740d04dSHans de Goede static int dwc3_byt_enable_ulpi_refclock(struct pci_dev *pci) 1007740d04dSHans de Goede { 1017740d04dSHans de Goede void __iomem *reg; 1027740d04dSHans de Goede u32 value; 1037740d04dSHans de Goede 1047740d04dSHans de Goede reg = pcim_iomap(pci, GP_RWBAR, 0); 105b497fff6SWei Yongjun if (!reg) 106b497fff6SWei Yongjun return -ENOMEM; 1077740d04dSHans de Goede 1087740d04dSHans de Goede value = readl(reg + GP_RWREG1); 1097740d04dSHans de Goede if (!(value & GP_RWREG1_ULPI_REFCLK_DISABLE)) 1107740d04dSHans de Goede goto unmap; /* ULPI refclk already enabled */ 1117740d04dSHans de Goede 1127740d04dSHans de Goede value &= ~GP_RWREG1_ULPI_REFCLK_DISABLE; 1137740d04dSHans de Goede writel(value, reg + GP_RWREG1); 1147740d04dSHans de Goede /* This comes from the Intel Android x86 tree w/o any explanation */ 1157740d04dSHans de Goede msleep(100); 1167740d04dSHans de Goede unmap: 1177740d04dSHans de Goede pcim_iounmap(pci, reg); 1187740d04dSHans de Goede return 0; 1197740d04dSHans de Goede } 1207740d04dSHans de Goede 1211a7b12f6SAndy Shevchenko static const struct property_entry dwc3_pci_intel_properties[] = { 1221a7b12f6SAndy Shevchenko PROPERTY_ENTRY_STRING("dr_mode", "peripheral"), 1231a7b12f6SAndy Shevchenko PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"), 1241a7b12f6SAndy Shevchenko {} 1251a7b12f6SAndy Shevchenko }; 1260f817ae6SFelipe Balbi 127582ab24eSHans de Goede static const struct property_entry dwc3_pci_intel_phy_charger_detect_properties[] = { 128582ab24eSHans de Goede PROPERTY_ENTRY_STRING("dr_mode", "peripheral"), 129582ab24eSHans de Goede PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"), 130582ab24eSHans de Goede PROPERTY_ENTRY_BOOL("linux,phy_charger_detect"), 1317ddda261SStephan Gerhold PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"), 132ca9400efSGreg Kroah-Hartman {} 133ca9400efSGreg Kroah-Hartman }; 134ca9400efSGreg Kroah-Hartman 135d7c93a90SHans de Goede static const struct property_entry dwc3_pci_intel_byt_properties[] = { 136d7c93a90SHans de Goede PROPERTY_ENTRY_STRING("dr_mode", "peripheral"), 137d7c93a90SHans de Goede PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"), 138582ab24eSHans de Goede PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"), 139582ab24eSHans de Goede {} 140582ab24eSHans de Goede }; 141582ab24eSHans de Goede 142c31d983bSAndy Shevchenko static const struct property_entry dwc3_pci_mrfld_properties[] = { 143c31d983bSAndy Shevchenko PROPERTY_ENTRY_STRING("dr_mode", "otg"), 144066c0959SAndy Shevchenko PROPERTY_ENTRY_STRING("linux,extcon-name", "mrfld_bcove_pwrsrc"), 145b522f830SAndy Shevchenko PROPERTY_ENTRY_BOOL("snps,dis_u3_susphy_quirk"), 146b522f830SAndy Shevchenko PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"), 14704357fafSFerry Toth PROPERTY_ENTRY_BOOL("snps,usb2-gadget-lpm-disable"), 148c31d983bSAndy Shevchenko PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"), 149c31d983bSAndy Shevchenko {} 150c31d983bSAndy Shevchenko }; 151c31d983bSAndy Shevchenko 1521a7b12f6SAndy Shevchenko static const struct property_entry dwc3_pci_amd_properties[] = { 153cf48305dSHeikki Krogerus PROPERTY_ENTRY_BOOL("snps,has-lpm-erratum"), 154cf48305dSHeikki Krogerus PROPERTY_ENTRY_U8("snps,lpm-nyet-threshold", 0xf), 155cf48305dSHeikki Krogerus PROPERTY_ENTRY_BOOL("snps,u2exit_lfps_quirk"), 156cf48305dSHeikki Krogerus PROPERTY_ENTRY_BOOL("snps,u2ss_inp3_quirk"), 157cf48305dSHeikki Krogerus PROPERTY_ENTRY_BOOL("snps,req_p1p2p3_quirk"), 158cf48305dSHeikki Krogerus PROPERTY_ENTRY_BOOL("snps,del_p1p2p3_quirk"), 159cf48305dSHeikki Krogerus PROPERTY_ENTRY_BOOL("snps,del_phy_power_chg_quirk"), 160cf48305dSHeikki Krogerus PROPERTY_ENTRY_BOOL("snps,lfps_filter_quirk"), 161cf48305dSHeikki Krogerus PROPERTY_ENTRY_BOOL("snps,rx_detect_poll_quirk"), 162cf48305dSHeikki Krogerus PROPERTY_ENTRY_BOOL("snps,tx_de_emphasis_quirk"), 163cf48305dSHeikki Krogerus PROPERTY_ENTRY_U8("snps,tx_de_emphasis", 1), 1641a7b12f6SAndy Shevchenko /* FIXME these quirks should be removed when AMD NL tapes out */ 165cf48305dSHeikki Krogerus PROPERTY_ENTRY_BOOL("snps,disable_scramble_quirk"), 166cf48305dSHeikki Krogerus PROPERTY_ENTRY_BOOL("snps,dis_u3_susphy_quirk"), 167cf48305dSHeikki Krogerus PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"), 1680eae2fdeSJohn Youn PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"), 169e6fe66feSFelipe Balbi {} 170e6fe66feSFelipe Balbi }; 171e6fe66feSFelipe Balbi 1721abade64SNehal Bakulchandra Shah static const struct property_entry dwc3_pci_mr_properties[] = { 1731abade64SNehal Bakulchandra Shah PROPERTY_ENTRY_STRING("dr_mode", "otg"), 1741abade64SNehal Bakulchandra Shah PROPERTY_ENTRY_BOOL("usb-role-switch"), 1751abade64SNehal Bakulchandra Shah PROPERTY_ENTRY_STRING("role-switch-default-mode", "host"), 1761abade64SNehal Bakulchandra Shah PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"), 1771abade64SNehal Bakulchandra Shah {} 1781abade64SNehal Bakulchandra Shah }; 1791abade64SNehal Bakulchandra Shah 180e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_intel_swnode = { 181e492ce9bSHeikki Krogerus .properties = dwc3_pci_intel_properties, 182e492ce9bSHeikki Krogerus }; 183e492ce9bSHeikki Krogerus 184582ab24eSHans de Goede static const struct software_node dwc3_pci_intel_phy_charger_detect_swnode = { 185582ab24eSHans de Goede .properties = dwc3_pci_intel_phy_charger_detect_properties, 186582ab24eSHans de Goede }; 187582ab24eSHans de Goede 188d7c93a90SHans de Goede static const struct software_node dwc3_pci_intel_byt_swnode = { 189d7c93a90SHans de Goede .properties = dwc3_pci_intel_byt_properties, 190d7c93a90SHans de Goede }; 191d7c93a90SHans de Goede 192e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_intel_mrfld_swnode = { 193e492ce9bSHeikki Krogerus .properties = dwc3_pci_mrfld_properties, 194e492ce9bSHeikki Krogerus }; 195e492ce9bSHeikki Krogerus 196e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_amd_swnode = { 197e492ce9bSHeikki Krogerus .properties = dwc3_pci_amd_properties, 198e492ce9bSHeikki Krogerus }; 199e492ce9bSHeikki Krogerus 2001abade64SNehal Bakulchandra Shah static const struct software_node dwc3_pci_amd_mr_swnode = { 2011abade64SNehal Bakulchandra Shah .properties = dwc3_pci_mr_properties, 2021abade64SNehal Bakulchandra Shah }; 2031abade64SNehal Bakulchandra Shah 204e285cb40SHans de Goede static int dwc3_pci_quirks(struct dwc3_pci *dwc, 205e285cb40SHans de Goede const struct software_node *swnode) 2061a7b12f6SAndy Shevchenko { 2071a7b12f6SAndy Shevchenko struct pci_dev *pdev = dwc->pci; 208e6fe66feSFelipe Balbi 2091a7b12f6SAndy Shevchenko if (pdev->vendor == PCI_VENDOR_ID_INTEL) { 2109cecca75SFelipe Balbi if (pdev->device == PCI_DEVICE_ID_INTEL_BXT || 211a609ce2aSRaymond Tan pdev->device == PCI_DEVICE_ID_INTEL_BXT_M || 212457d2285SRaymond Tan pdev->device == PCI_DEVICE_ID_INTEL_EHL) { 21394116f81SAndy Shevchenko guid_parse(PCI_INTEL_BXT_DSM_GUID, &dwc->guid); 2149cecca75SFelipe Balbi dwc->has_dsm_for_pm = true; 2159cecca75SFelipe Balbi } 2169cecca75SFelipe Balbi 217e6fe66feSFelipe Balbi if (pdev->device == PCI_DEVICE_ID_INTEL_BYT) { 218a89d977cSHeikki Krogerus struct gpio_desc *gpio; 2191a7b12f6SAndy Shevchenko int ret; 220a89d977cSHeikki Krogerus 2217740d04dSHans de Goede /* On BYT the FW does not always enable the refclock */ 2227740d04dSHans de Goede ret = dwc3_byt_enable_ulpi_refclock(pdev); 2237740d04dSHans de Goede if (ret) 2247740d04dSHans de Goede return ret; 2257740d04dSHans de Goede 2264a56e413SAndy Shevchenko ret = devm_acpi_dev_add_driver_gpios(&pdev->dev, 227a89d977cSHeikki Krogerus acpi_dwc3_byt_gpios); 2284a56e413SAndy Shevchenko if (ret) 2294a56e413SAndy Shevchenko dev_dbg(&pdev->dev, "failed to add mapping table\n"); 230a89d977cSHeikki Krogerus 2312df033caSUwe Kleine-König /* 2325741022cSHans de Goede * A lot of BYT devices lack ACPI resource entries for 2335741022cSHans de Goede * the GPIOs, add a fallback mapping to the reference 2345741022cSHans de Goede * design GPIOs which all boards seem to use. 2355741022cSHans de Goede */ 2365741022cSHans de Goede gpiod_add_lookup_table(&platform_bytcr_gpios); 2375741022cSHans de Goede 2385741022cSHans de Goede /* 2392df033caSUwe Kleine-König * These GPIOs will turn on the USB2 PHY. Note that we have to 2402df033caSUwe Kleine-König * put the gpio descriptors again here because the phy driver 2412df033caSUwe Kleine-König * might want to grab them, too. 2422df033caSUwe Kleine-König */ 2433004cfd6SStephan Gerhold gpio = gpiod_get_optional(&pdev->dev, "cs", GPIOD_OUT_LOW); 2442df033caSUwe Kleine-König if (IS_ERR(gpio)) 2452df033caSUwe Kleine-König return PTR_ERR(gpio); 2462df033caSUwe Kleine-König 247a89d977cSHeikki Krogerus gpiod_set_value_cansleep(gpio, 1); 2483004cfd6SStephan Gerhold gpiod_put(gpio); 249a89d977cSHeikki Krogerus 2503004cfd6SStephan Gerhold gpio = gpiod_get_optional(&pdev->dev, "reset", GPIOD_OUT_LOW); 2512df033caSUwe Kleine-König if (IS_ERR(gpio)) 2522df033caSUwe Kleine-König return PTR_ERR(gpio); 2532df033caSUwe Kleine-König 2542df033caSUwe Kleine-König if (gpio) { 255a89d977cSHeikki Krogerus gpiod_set_value_cansleep(gpio, 1); 2563004cfd6SStephan Gerhold gpiod_put(gpio); 257a89d977cSHeikki Krogerus usleep_range(10000, 11000); 258a89d977cSHeikki Krogerus } 259582ab24eSHans de Goede 260582ab24eSHans de Goede /* 261a5d847b0SHans de Goede * Make the pdev name predictable (only 1 DWC3 on BYT) 262a5d847b0SHans de Goede * and patch the phy dev-name into the lookup table so 263a5d847b0SHans de Goede * that the phy-driver can get the GPIOs. 264a5d847b0SHans de Goede */ 265a5d847b0SHans de Goede dwc->dwc3->id = PLATFORM_DEVID_NONE; 266a5d847b0SHans de Goede platform_bytcr_gpios.dev_id = "dwc3.ulpi"; 267a5d847b0SHans de Goede 268a5d847b0SHans de Goede /* 269582ab24eSHans de Goede * Some Android tablets with a Crystal Cove PMIC 270582ab24eSHans de Goede * (INT33FD), rely on the TUSB1211 phy for charger 271582ab24eSHans de Goede * detection. These can be identified by them _not_ 272582ab24eSHans de Goede * using the standard ACPI battery and ac drivers. 273582ab24eSHans de Goede */ 274582ab24eSHans de Goede if (acpi_dev_present("INT33FD", "1", 2) && 275582ab24eSHans de Goede acpi_quirk_skip_acpi_ac_and_battery()) { 276582ab24eSHans de Goede dev_info(&pdev->dev, "Using TUSB1211 phy for charger detection\n"); 277582ab24eSHans de Goede swnode = &dwc3_pci_intel_phy_charger_detect_swnode; 278582ab24eSHans de Goede } 279a89d977cSHeikki Krogerus } 280e6fe66feSFelipe Balbi } 281a89d977cSHeikki Krogerus 282e285cb40SHans de Goede return device_add_software_node(&dwc->dwc3->dev, swnode); 2832cd9ddf7SHeikki Krogerus } 28472246da4SFelipe Balbi 2858eed00b2SManu Gautam #ifdef CONFIG_PM 2868eed00b2SManu Gautam static void dwc3_pci_resume_work(struct work_struct *work) 2878eed00b2SManu Gautam { 2888eed00b2SManu Gautam struct dwc3_pci *dwc = container_of(work, struct dwc3_pci, wakeup_work); 2898eed00b2SManu Gautam struct platform_device *dwc3 = dwc->dwc3; 2908eed00b2SManu Gautam int ret; 2918eed00b2SManu Gautam 2928eed00b2SManu Gautam ret = pm_runtime_get_sync(&dwc3->dev); 293a03e2ddaSZheng Yongjun if (ret < 0) { 2942655971aSAditya Pakki pm_runtime_put_sync_autosuspend(&dwc3->dev); 2958eed00b2SManu Gautam return; 2962655971aSAditya Pakki } 2978eed00b2SManu Gautam 2988eed00b2SManu Gautam pm_runtime_mark_last_busy(&dwc3->dev); 2998eed00b2SManu Gautam pm_runtime_put_sync_autosuspend(&dwc3->dev); 3008eed00b2SManu Gautam } 3018eed00b2SManu Gautam #endif 3028eed00b2SManu Gautam 3031a7b12f6SAndy Shevchenko static int dwc3_pci_probe(struct pci_dev *pci, const struct pci_device_id *id) 30472246da4SFelipe Balbi { 3050f817ae6SFelipe Balbi struct dwc3_pci *dwc; 30672246da4SFelipe Balbi struct resource res[2]; 307b09e99eeSAndy Shevchenko int ret; 308802ca850SChanho Park struct device *dev = &pci->dev; 30972246da4SFelipe Balbi 310f1c7e710SAndy Shevchenko ret = pcim_enable_device(pci); 31172246da4SFelipe Balbi if (ret) { 312802ca850SChanho Park dev_err(dev, "failed to enable pci device\n"); 313802ca850SChanho Park return -ENODEV; 31472246da4SFelipe Balbi } 31572246da4SFelipe Balbi 31672246da4SFelipe Balbi pci_set_master(pci); 31772246da4SFelipe Balbi 3180f817ae6SFelipe Balbi dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL); 3190f817ae6SFelipe Balbi if (!dwc) 320f1c7e710SAndy Shevchenko return -ENOMEM; 3210f817ae6SFelipe Balbi 3220f817ae6SFelipe Balbi dwc->dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO); 3230f817ae6SFelipe Balbi if (!dwc->dwc3) 3240f817ae6SFelipe Balbi return -ENOMEM; 32572246da4SFelipe Balbi 32672246da4SFelipe Balbi memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res)); 32772246da4SFelipe Balbi 32872246da4SFelipe Balbi res[0].start = pci_resource_start(pci, 0); 32972246da4SFelipe Balbi res[0].end = pci_resource_end(pci, 0); 33072246da4SFelipe Balbi res[0].name = "dwc_usb3"; 33172246da4SFelipe Balbi res[0].flags = IORESOURCE_MEM; 33272246da4SFelipe Balbi 33372246da4SFelipe Balbi res[1].start = pci->irq; 33472246da4SFelipe Balbi res[1].name = "dwc_usb3"; 33572246da4SFelipe Balbi res[1].flags = IORESOURCE_IRQ; 33672246da4SFelipe Balbi 3370f817ae6SFelipe Balbi ret = platform_device_add_resources(dwc->dwc3, res, ARRAY_SIZE(res)); 33872246da4SFelipe Balbi if (ret) { 339802ca850SChanho Park dev_err(dev, "couldn't add resources to dwc3 device\n"); 340cabdf83dSThinh Nguyen goto err; 34172246da4SFelipe Balbi } 34272246da4SFelipe Balbi 3430f817ae6SFelipe Balbi dwc->pci = pci; 3440f817ae6SFelipe Balbi dwc->dwc3->dev.parent = dev; 3450f817ae6SFelipe Balbi ACPI_COMPANION_SET(&dwc->dwc3->dev, ACPI_COMPANION(dev)); 34672246da4SFelipe Balbi 347e285cb40SHans de Goede ret = dwc3_pci_quirks(dwc, (void *)id->driver_data); 348474799f0SHeikki Krogerus if (ret) 349474799f0SHeikki Krogerus goto err; 350474799f0SHeikki Krogerus 3510f817ae6SFelipe Balbi ret = platform_device_add(dwc->dwc3); 35272246da4SFelipe Balbi if (ret) { 353802ca850SChanho Park dev_err(dev, "failed to register dwc3 device\n"); 3542cd9ddf7SHeikki Krogerus goto err; 35572246da4SFelipe Balbi } 35672246da4SFelipe Balbi 357e9af9229SFelipe Balbi device_init_wakeup(dev, true); 3580f817ae6SFelipe Balbi pci_set_drvdata(pci, dwc); 359e9af9229SFelipe Balbi pm_runtime_put(dev); 3608eed00b2SManu Gautam #ifdef CONFIG_PM 3618eed00b2SManu Gautam INIT_WORK(&dwc->wakeup_work, dwc3_pci_resume_work); 3628eed00b2SManu Gautam #endif 363e9af9229SFelipe Balbi 36472246da4SFelipe Balbi return 0; 3652cd9ddf7SHeikki Krogerus err: 366e492ce9bSHeikki Krogerus device_remove_software_node(&dwc->dwc3->dev); 3670f817ae6SFelipe Balbi platform_device_put(dwc->dwc3); 36872246da4SFelipe Balbi return ret; 36972246da4SFelipe Balbi } 37072246da4SFelipe Balbi 371fb4e98abSBill Pemberton static void dwc3_pci_remove(struct pci_dev *pci) 37272246da4SFelipe Balbi { 3730f817ae6SFelipe Balbi struct dwc3_pci *dwc = pci_get_drvdata(pci); 3747b412b04SKuppuswamy Sathyanarayanan struct pci_dev *pdev = dwc->pci; 3750f817ae6SFelipe Balbi 3767b412b04SKuppuswamy Sathyanarayanan if (pdev->device == PCI_DEVICE_ID_INTEL_BYT) 3775741022cSHans de Goede gpiod_remove_lookup_table(&platform_bytcr_gpios); 3788eed00b2SManu Gautam #ifdef CONFIG_PM 3798eed00b2SManu Gautam cancel_work_sync(&dwc->wakeup_work); 3808eed00b2SManu Gautam #endif 381e9af9229SFelipe Balbi device_init_wakeup(&pci->dev, false); 382e9af9229SFelipe Balbi pm_runtime_get(&pci->dev); 383e492ce9bSHeikki Krogerus device_remove_software_node(&dwc->dwc3->dev); 3840f817ae6SFelipe Balbi platform_device_unregister(dwc->dwc3); 38572246da4SFelipe Balbi } 38672246da4SFelipe Balbi 387782df20cSJingoo Han static const struct pci_device_id dwc3_pci_id_table[] = { 3881a7b12f6SAndy Shevchenko { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BSW), 389e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 3901a7b12f6SAndy Shevchenko 3911a7b12f6SAndy Shevchenko { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BYT), 392d7c93a90SHans de Goede (kernel_ulong_t) &dwc3_pci_intel_byt_swnode, }, 3931a7b12f6SAndy Shevchenko 3941a7b12f6SAndy Shevchenko { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MRFLD), 395e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_mrfld_swnode, }, 3961a7b12f6SAndy Shevchenko 3973c3caae4SHeikki Krogerus { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CMLLP), 398e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 3993c3caae4SHeikki Krogerus 4007ae622c9SFelipe Balbi { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CMLH), 401e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 4027ae622c9SFelipe Balbi 4031a7b12f6SAndy Shevchenko { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SPTLP), 404e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 4051a7b12f6SAndy Shevchenko 4061a7b12f6SAndy Shevchenko { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SPTH), 407e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 4081a7b12f6SAndy Shevchenko 4091a7b12f6SAndy Shevchenko { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BXT), 410e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 4111a7b12f6SAndy Shevchenko 4121a7b12f6SAndy Shevchenko { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BXT_M), 413e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 4141a7b12f6SAndy Shevchenko 4151a7b12f6SAndy Shevchenko { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_APL), 416e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 4171a7b12f6SAndy Shevchenko 4181a7b12f6SAndy Shevchenko { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_KBP), 419e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 4201a7b12f6SAndy Shevchenko 4211a7b12f6SAndy Shevchenko { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_GLK), 422e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 4231a7b12f6SAndy Shevchenko 4241a7b12f6SAndy Shevchenko { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CNPLP), 425e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 4261a7b12f6SAndy Shevchenko 4271a7b12f6SAndy Shevchenko { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CNPH), 428e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 4291a7b12f6SAndy Shevchenko 430f5ae8869SHeikki Krogerus { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CNPV), 431e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 432f5ae8869SHeikki Krogerus 4331a7b12f6SAndy Shevchenko { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICLLP), 434e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 4351a7b12f6SAndy Shevchenko 436457d2285SRaymond Tan { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_EHL), 437457d2285SRaymond Tan (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 438dbb0569dSFelipe Balbi 439b3649deeSFelipe Balbi { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGPLP), 440e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 441b3649deeSFelipe Balbi 442c3f595a8SHeikki Krogerus { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGPH), 443e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 444c3f595a8SHeikki Krogerus 445e25d1e85SHeikki Krogerus { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_JSP), 446e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 447e25d1e85SHeikki Krogerus 448ecb0a2f1SShruthi Sanil { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL), 449ecb0a2f1SShruthi Sanil (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 450ecb0a2f1SShruthi Sanil 451f08fc2c3SHeikki Krogerus { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADLP), 452f08fc2c3SHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 453f08fc2c3SHeikki Krogerus 454782de5e7SHeikki Krogerus { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADLM), 455782de5e7SHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 456782de5e7SHeikki Krogerus 4571384ab4fSHeikki Krogerus { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADLS), 458e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 4591384ab4fSHeikki Krogerus 460*bad0d1d7SHeikki Krogerus { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPL), 461*bad0d1d7SHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 462*bad0d1d7SHeikki Krogerus 463038438a2SHeikki Krogerus { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPLS), 464038438a2SHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 465038438a2SHeikki Krogerus 466973e0f7aSHeikki Krogerus { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTLP), 467973e0f7aSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 468973e0f7aSHeikki Krogerus 469973e0f7aSHeikki Krogerus { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL), 470973e0f7aSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 471973e0f7aSHeikki Krogerus 47273203bdeSHeikki Krogerus { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL), 47373203bdeSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_intel_swnode, }, 47473203bdeSHeikki Krogerus 4751a7b12f6SAndy Shevchenko { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_NL_USB), 476e492ce9bSHeikki Krogerus (kernel_ulong_t) &dwc3_pci_amd_swnode, }, 4771abade64SNehal Bakulchandra Shah 4781abade64SNehal Bakulchandra Shah { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_MR), 4791abade64SNehal Bakulchandra Shah (kernel_ulong_t)&dwc3_pci_amd_mr_swnode, }, 4801abade64SNehal Bakulchandra Shah 48172246da4SFelipe Balbi { } /* Terminating Entry */ 48272246da4SFelipe Balbi }; 48372246da4SFelipe Balbi MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table); 48472246da4SFelipe Balbi 48536daf3aaSFelipe Balbi #if defined(CONFIG_PM) || defined(CONFIG_PM_SLEEP) 48636daf3aaSFelipe Balbi static int dwc3_pci_dsm(struct dwc3_pci *dwc, int param) 48736daf3aaSFelipe Balbi { 48836daf3aaSFelipe Balbi union acpi_object *obj; 48936daf3aaSFelipe Balbi union acpi_object tmp; 49036daf3aaSFelipe Balbi union acpi_object argv4 = ACPI_INIT_DSM_ARGV4(1, &tmp); 49136daf3aaSFelipe Balbi 49236daf3aaSFelipe Balbi if (!dwc->has_dsm_for_pm) 49336daf3aaSFelipe Balbi return 0; 49436daf3aaSFelipe Balbi 49536daf3aaSFelipe Balbi tmp.type = ACPI_TYPE_INTEGER; 49636daf3aaSFelipe Balbi tmp.integer.value = param; 49736daf3aaSFelipe Balbi 49894116f81SAndy Shevchenko obj = acpi_evaluate_dsm(ACPI_HANDLE(&dwc->pci->dev), &dwc->guid, 49936daf3aaSFelipe Balbi 1, PCI_INTEL_BXT_FUNC_PMU_PWR, &argv4); 50036daf3aaSFelipe Balbi if (!obj) { 50136daf3aaSFelipe Balbi dev_err(&dwc->pci->dev, "failed to evaluate _DSM\n"); 50236daf3aaSFelipe Balbi return -EIO; 50336daf3aaSFelipe Balbi } 50436daf3aaSFelipe Balbi 50536daf3aaSFelipe Balbi ACPI_FREE(obj); 50636daf3aaSFelipe Balbi 50736daf3aaSFelipe Balbi return 0; 50836daf3aaSFelipe Balbi } 50936daf3aaSFelipe Balbi #endif /* CONFIG_PM || CONFIG_PM_SLEEP */ 51036daf3aaSFelipe Balbi 511e9af9229SFelipe Balbi #ifdef CONFIG_PM 512e9af9229SFelipe Balbi static int dwc3_pci_runtime_suspend(struct device *dev) 513e9af9229SFelipe Balbi { 5149cecca75SFelipe Balbi struct dwc3_pci *dwc = dev_get_drvdata(dev); 5159cecca75SFelipe Balbi 516de3ef1ebSRafael J. Wysocki if (device_can_wakeup(dev)) 5179cecca75SFelipe Balbi return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D3); 518e9af9229SFelipe Balbi 519e9af9229SFelipe Balbi return -EBUSY; 520e9af9229SFelipe Balbi } 521e9af9229SFelipe Balbi 522f6c274e1SFelipe Balbi static int dwc3_pci_runtime_resume(struct device *dev) 523f6c274e1SFelipe Balbi { 5240f817ae6SFelipe Balbi struct dwc3_pci *dwc = dev_get_drvdata(dev); 5259cecca75SFelipe Balbi int ret; 5269cecca75SFelipe Balbi 5279cecca75SFelipe Balbi ret = dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D0); 5289cecca75SFelipe Balbi if (ret) 5299cecca75SFelipe Balbi return ret; 530f6c274e1SFelipe Balbi 5318eed00b2SManu Gautam queue_work(pm_wq, &dwc->wakeup_work); 5328eed00b2SManu Gautam 5338eed00b2SManu Gautam return 0; 534f6c274e1SFelipe Balbi } 535696118c0SFelipe Balbi #endif /* CONFIG_PM */ 536f6c274e1SFelipe Balbi 537696118c0SFelipe Balbi #ifdef CONFIG_PM_SLEEP 5389cecca75SFelipe Balbi static int dwc3_pci_suspend(struct device *dev) 539e9af9229SFelipe Balbi { 5409cecca75SFelipe Balbi struct dwc3_pci *dwc = dev_get_drvdata(dev); 5419cecca75SFelipe Balbi 5429cecca75SFelipe Balbi return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D3); 5439cecca75SFelipe Balbi } 5449cecca75SFelipe Balbi 5459cecca75SFelipe Balbi static int dwc3_pci_resume(struct device *dev) 5469cecca75SFelipe Balbi { 5479cecca75SFelipe Balbi struct dwc3_pci *dwc = dev_get_drvdata(dev); 5489cecca75SFelipe Balbi 5499cecca75SFelipe Balbi return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D0); 550e9af9229SFelipe Balbi } 551696118c0SFelipe Balbi #endif /* CONFIG_PM_SLEEP */ 552e9af9229SFelipe Balbi 55395aa932cSDoug Wilson static const struct dev_pm_ops dwc3_pci_dev_pm_ops = { 5549cecca75SFelipe Balbi SET_SYSTEM_SLEEP_PM_OPS(dwc3_pci_suspend, dwc3_pci_resume) 555f6c274e1SFelipe Balbi SET_RUNTIME_PM_OPS(dwc3_pci_runtime_suspend, dwc3_pci_runtime_resume, 556e9af9229SFelipe Balbi NULL) 557e9af9229SFelipe Balbi }; 558e9af9229SFelipe Balbi 55972246da4SFelipe Balbi static struct pci_driver dwc3_pci_driver = { 5600949e99bSFelipe Balbi .name = "dwc3-pci", 56172246da4SFelipe Balbi .id_table = dwc3_pci_id_table, 56272246da4SFelipe Balbi .probe = dwc3_pci_probe, 5637690417dSBill Pemberton .remove = dwc3_pci_remove, 564e9af9229SFelipe Balbi .driver = { 565e9af9229SFelipe Balbi .pm = &dwc3_pci_dev_pm_ops, 566e9af9229SFelipe Balbi } 56772246da4SFelipe Balbi }; 56872246da4SFelipe Balbi 56972246da4SFelipe Balbi MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); 5705945f789SFelipe Balbi MODULE_LICENSE("GPL v2"); 57172246da4SFelipe Balbi MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer"); 57272246da4SFelipe Balbi 57395656336SGreg Kroah-Hartman module_pci_driver(dwc3_pci_driver); 574