xref: /linux/drivers/usb/dwc3/dwc3-pci.c (revision d7c93a903f33ff35aa0e6b5a8032eb9755b00826)
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
43f08fc2c3SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_ADLP		0x51ee
44782de5e7SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_ADLM		0x54ee
451384ab4fSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_ADLS		0x7ae1
46038438a2SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_RPLS		0x7a61
4773203bdeSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_TGL			0x9a15
481abade64SNehal Bakulchandra Shah #define PCI_DEVICE_ID_AMD_MR			0x163a
4972246da4SFelipe Balbi 
5094116f81SAndy Shevchenko #define PCI_INTEL_BXT_DSM_GUID		"732b85d5-b7a7-4a1b-9ba0-4bbd00ffd511"
519cecca75SFelipe Balbi #define PCI_INTEL_BXT_FUNC_PMU_PWR	4
529cecca75SFelipe Balbi #define PCI_INTEL_BXT_STATE_D0		0
539cecca75SFelipe Balbi #define PCI_INTEL_BXT_STATE_D3		3
549cecca75SFelipe Balbi 
557740d04dSHans de Goede #define GP_RWBAR			1
567740d04dSHans de Goede #define GP_RWREG1			0xa0
577740d04dSHans de Goede #define GP_RWREG1_ULPI_REFCLK_DISABLE	(1 << 17)
587740d04dSHans de Goede 
590f817ae6SFelipe Balbi /**
600f817ae6SFelipe Balbi  * struct dwc3_pci - Driver private structure
610f817ae6SFelipe Balbi  * @dwc3: child dwc3 platform_device
620f817ae6SFelipe Balbi  * @pci: our link to PCI bus
6394116f81SAndy Shevchenko  * @guid: _DSM GUID
649cecca75SFelipe Balbi  * @has_dsm_for_pm: true for devices which need to run _DSM on runtime PM
6587d852deSAndy Shevchenko  * @wakeup_work: work for asynchronous resume
660f817ae6SFelipe Balbi  */
670f817ae6SFelipe Balbi struct dwc3_pci {
680f817ae6SFelipe Balbi 	struct platform_device *dwc3;
690f817ae6SFelipe Balbi 	struct pci_dev *pci;
709cecca75SFelipe Balbi 
7194116f81SAndy Shevchenko 	guid_t guid;
729cecca75SFelipe Balbi 
739cecca75SFelipe Balbi 	unsigned int has_dsm_for_pm:1;
748eed00b2SManu Gautam 	struct work_struct wakeup_work;
750f817ae6SFelipe Balbi };
760f817ae6SFelipe Balbi 
77a89d977cSHeikki Krogerus static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
78a89d977cSHeikki Krogerus static const struct acpi_gpio_params cs_gpios = { 1, 0, false };
79a89d977cSHeikki Krogerus 
80a89d977cSHeikki Krogerus static const struct acpi_gpio_mapping acpi_dwc3_byt_gpios[] = {
81a89d977cSHeikki Krogerus 	{ "reset-gpios", &reset_gpios, 1 },
82a89d977cSHeikki Krogerus 	{ "cs-gpios", &cs_gpios, 1 },
83a89d977cSHeikki Krogerus 	{ },
84a89d977cSHeikki Krogerus };
85a89d977cSHeikki Krogerus 
865741022cSHans de Goede static struct gpiod_lookup_table platform_bytcr_gpios = {
875741022cSHans de Goede 	.dev_id		= "0000:00:16.0",
885741022cSHans de Goede 	.table		= {
895741022cSHans de Goede 		GPIO_LOOKUP("INT33FC:00", 54, "reset", GPIO_ACTIVE_HIGH),
905741022cSHans de Goede 		GPIO_LOOKUP("INT33FC:02", 14, "cs", GPIO_ACTIVE_HIGH),
915741022cSHans de Goede 		{}
925741022cSHans de Goede 	},
935741022cSHans de Goede };
945741022cSHans de Goede 
957740d04dSHans de Goede static int dwc3_byt_enable_ulpi_refclock(struct pci_dev *pci)
967740d04dSHans de Goede {
977740d04dSHans de Goede 	void __iomem	*reg;
987740d04dSHans de Goede 	u32		value;
997740d04dSHans de Goede 
1007740d04dSHans de Goede 	reg = pcim_iomap(pci, GP_RWBAR, 0);
101b497fff6SWei Yongjun 	if (!reg)
102b497fff6SWei Yongjun 		return -ENOMEM;
1037740d04dSHans de Goede 
1047740d04dSHans de Goede 	value = readl(reg + GP_RWREG1);
1057740d04dSHans de Goede 	if (!(value & GP_RWREG1_ULPI_REFCLK_DISABLE))
1067740d04dSHans de Goede 		goto unmap; /* ULPI refclk already enabled */
1077740d04dSHans de Goede 
1087740d04dSHans de Goede 	value &= ~GP_RWREG1_ULPI_REFCLK_DISABLE;
1097740d04dSHans de Goede 	writel(value, reg + GP_RWREG1);
1107740d04dSHans de Goede 	/* This comes from the Intel Android x86 tree w/o any explanation */
1117740d04dSHans de Goede 	msleep(100);
1127740d04dSHans de Goede unmap:
1137740d04dSHans de Goede 	pcim_iounmap(pci, reg);
1147740d04dSHans de Goede 	return 0;
1157740d04dSHans de Goede }
1167740d04dSHans de Goede 
1171a7b12f6SAndy Shevchenko static const struct property_entry dwc3_pci_intel_properties[] = {
1181a7b12f6SAndy Shevchenko 	PROPERTY_ENTRY_STRING("dr_mode", "peripheral"),
1191a7b12f6SAndy Shevchenko 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
1201a7b12f6SAndy Shevchenko 	{}
1211a7b12f6SAndy Shevchenko };
1220f817ae6SFelipe Balbi 
123*d7c93a90SHans de Goede static const struct property_entry dwc3_pci_intel_byt_properties[] = {
124*d7c93a90SHans de Goede 	PROPERTY_ENTRY_STRING("dr_mode", "peripheral"),
125*d7c93a90SHans de Goede 	PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
126*d7c93a90SHans de Goede 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
127*d7c93a90SHans de Goede 	{}
128*d7c93a90SHans de Goede };
129*d7c93a90SHans de Goede 
130c31d983bSAndy Shevchenko static const struct property_entry dwc3_pci_mrfld_properties[] = {
131c31d983bSAndy Shevchenko 	PROPERTY_ENTRY_STRING("dr_mode", "otg"),
132066c0959SAndy Shevchenko 	PROPERTY_ENTRY_STRING("linux,extcon-name", "mrfld_bcove_pwrsrc"),
133b522f830SAndy Shevchenko 	PROPERTY_ENTRY_BOOL("snps,dis_u3_susphy_quirk"),
134b522f830SAndy Shevchenko 	PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
13504357fafSFerry Toth 	PROPERTY_ENTRY_BOOL("snps,usb2-gadget-lpm-disable"),
136c31d983bSAndy Shevchenko 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
137c31d983bSAndy Shevchenko 	{}
138c31d983bSAndy Shevchenko };
139c31d983bSAndy Shevchenko 
1401a7b12f6SAndy Shevchenko static const struct property_entry dwc3_pci_amd_properties[] = {
141cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,has-lpm-erratum"),
142cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_U8("snps,lpm-nyet-threshold", 0xf),
143cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,u2exit_lfps_quirk"),
144cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,u2ss_inp3_quirk"),
145cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,req_p1p2p3_quirk"),
146cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,del_p1p2p3_quirk"),
147cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,del_phy_power_chg_quirk"),
148cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,lfps_filter_quirk"),
149cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,rx_detect_poll_quirk"),
150cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,tx_de_emphasis_quirk"),
151cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_U8("snps,tx_de_emphasis", 1),
1521a7b12f6SAndy Shevchenko 	/* FIXME these quirks should be removed when AMD NL tapes out */
153cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,disable_scramble_quirk"),
154cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,dis_u3_susphy_quirk"),
155cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
1560eae2fdeSJohn Youn 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
157e6fe66feSFelipe Balbi 	{}
158e6fe66feSFelipe Balbi };
159e6fe66feSFelipe Balbi 
1601abade64SNehal Bakulchandra Shah static const struct property_entry dwc3_pci_mr_properties[] = {
1611abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_STRING("dr_mode", "otg"),
1621abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_BOOL("usb-role-switch"),
1631abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_STRING("role-switch-default-mode", "host"),
1641abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
1651abade64SNehal Bakulchandra Shah 	{}
1661abade64SNehal Bakulchandra Shah };
1671abade64SNehal Bakulchandra Shah 
168e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_intel_swnode = {
169e492ce9bSHeikki Krogerus 	.properties = dwc3_pci_intel_properties,
170e492ce9bSHeikki Krogerus };
171e492ce9bSHeikki Krogerus 
172*d7c93a90SHans de Goede static const struct software_node dwc3_pci_intel_byt_swnode = {
173*d7c93a90SHans de Goede 	.properties = dwc3_pci_intel_byt_properties,
174*d7c93a90SHans de Goede };
175*d7c93a90SHans de Goede 
176e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_intel_mrfld_swnode = {
177e492ce9bSHeikki Krogerus 	.properties = dwc3_pci_mrfld_properties,
178e492ce9bSHeikki Krogerus };
179e492ce9bSHeikki Krogerus 
180e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_amd_swnode = {
181e492ce9bSHeikki Krogerus 	.properties = dwc3_pci_amd_properties,
182e492ce9bSHeikki Krogerus };
183e492ce9bSHeikki Krogerus 
1841abade64SNehal Bakulchandra Shah static const struct software_node dwc3_pci_amd_mr_swnode = {
1851abade64SNehal Bakulchandra Shah 	.properties = dwc3_pci_mr_properties,
1861abade64SNehal Bakulchandra Shah };
1871abade64SNehal Bakulchandra Shah 
1881a7b12f6SAndy Shevchenko static int dwc3_pci_quirks(struct dwc3_pci *dwc)
1891a7b12f6SAndy Shevchenko {
1901a7b12f6SAndy Shevchenko 	struct pci_dev			*pdev = dwc->pci;
191e6fe66feSFelipe Balbi 
1921a7b12f6SAndy Shevchenko 	if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
1939cecca75SFelipe Balbi 		if (pdev->device == PCI_DEVICE_ID_INTEL_BXT ||
194a609ce2aSRaymond Tan 		    pdev->device == PCI_DEVICE_ID_INTEL_BXT_M ||
195457d2285SRaymond Tan 		    pdev->device == PCI_DEVICE_ID_INTEL_EHL) {
19694116f81SAndy Shevchenko 			guid_parse(PCI_INTEL_BXT_DSM_GUID, &dwc->guid);
1979cecca75SFelipe Balbi 			dwc->has_dsm_for_pm = true;
1989cecca75SFelipe Balbi 		}
1999cecca75SFelipe Balbi 
200e6fe66feSFelipe Balbi 		if (pdev->device == PCI_DEVICE_ID_INTEL_BYT) {
201a89d977cSHeikki Krogerus 			struct gpio_desc *gpio;
2021a7b12f6SAndy Shevchenko 			int ret;
203a89d977cSHeikki Krogerus 
2047740d04dSHans de Goede 			/* On BYT the FW does not always enable the refclock */
2057740d04dSHans de Goede 			ret = dwc3_byt_enable_ulpi_refclock(pdev);
2067740d04dSHans de Goede 			if (ret)
2077740d04dSHans de Goede 				return ret;
2087740d04dSHans de Goede 
2094a56e413SAndy Shevchenko 			ret = devm_acpi_dev_add_driver_gpios(&pdev->dev,
210a89d977cSHeikki Krogerus 					acpi_dwc3_byt_gpios);
2114a56e413SAndy Shevchenko 			if (ret)
2124a56e413SAndy Shevchenko 				dev_dbg(&pdev->dev, "failed to add mapping table\n");
213a89d977cSHeikki Krogerus 
2142df033caSUwe Kleine-König 			/*
2155741022cSHans de Goede 			 * A lot of BYT devices lack ACPI resource entries for
2165741022cSHans de Goede 			 * the GPIOs, add a fallback mapping to the reference
2175741022cSHans de Goede 			 * design GPIOs which all boards seem to use.
2185741022cSHans de Goede 			 */
2195741022cSHans de Goede 			gpiod_add_lookup_table(&platform_bytcr_gpios);
2205741022cSHans de Goede 
2215741022cSHans de Goede 			/*
2222df033caSUwe Kleine-König 			 * These GPIOs will turn on the USB2 PHY. Note that we have to
2232df033caSUwe Kleine-König 			 * put the gpio descriptors again here because the phy driver
2242df033caSUwe Kleine-König 			 * might want to grab them, too.
2252df033caSUwe Kleine-König 			 */
2263004cfd6SStephan Gerhold 			gpio = gpiod_get_optional(&pdev->dev, "cs", GPIOD_OUT_LOW);
2272df033caSUwe Kleine-König 			if (IS_ERR(gpio))
2282df033caSUwe Kleine-König 				return PTR_ERR(gpio);
2292df033caSUwe Kleine-König 
230a89d977cSHeikki Krogerus 			gpiod_set_value_cansleep(gpio, 1);
2313004cfd6SStephan Gerhold 			gpiod_put(gpio);
232a89d977cSHeikki Krogerus 
2333004cfd6SStephan Gerhold 			gpio = gpiod_get_optional(&pdev->dev, "reset", GPIOD_OUT_LOW);
2342df033caSUwe Kleine-König 			if (IS_ERR(gpio))
2352df033caSUwe Kleine-König 				return PTR_ERR(gpio);
2362df033caSUwe Kleine-König 
2372df033caSUwe Kleine-König 			if (gpio) {
238a89d977cSHeikki Krogerus 				gpiod_set_value_cansleep(gpio, 1);
2393004cfd6SStephan Gerhold 				gpiod_put(gpio);
240a89d977cSHeikki Krogerus 				usleep_range(10000, 11000);
241a89d977cSHeikki Krogerus 			}
242a89d977cSHeikki Krogerus 		}
243e6fe66feSFelipe Balbi 	}
244a89d977cSHeikki Krogerus 
2452cd9ddf7SHeikki Krogerus 	return 0;
2462cd9ddf7SHeikki Krogerus }
24772246da4SFelipe Balbi 
2488eed00b2SManu Gautam #ifdef CONFIG_PM
2498eed00b2SManu Gautam static void dwc3_pci_resume_work(struct work_struct *work)
2508eed00b2SManu Gautam {
2518eed00b2SManu Gautam 	struct dwc3_pci *dwc = container_of(work, struct dwc3_pci, wakeup_work);
2528eed00b2SManu Gautam 	struct platform_device *dwc3 = dwc->dwc3;
2538eed00b2SManu Gautam 	int ret;
2548eed00b2SManu Gautam 
2558eed00b2SManu Gautam 	ret = pm_runtime_get_sync(&dwc3->dev);
2562655971aSAditya Pakki 	if (ret) {
2572655971aSAditya Pakki 		pm_runtime_put_sync_autosuspend(&dwc3->dev);
2588eed00b2SManu Gautam 		return;
2592655971aSAditya Pakki 	}
2608eed00b2SManu Gautam 
2618eed00b2SManu Gautam 	pm_runtime_mark_last_busy(&dwc3->dev);
2628eed00b2SManu Gautam 	pm_runtime_put_sync_autosuspend(&dwc3->dev);
2638eed00b2SManu Gautam }
2648eed00b2SManu Gautam #endif
2658eed00b2SManu Gautam 
2661a7b12f6SAndy Shevchenko static int dwc3_pci_probe(struct pci_dev *pci, const struct pci_device_id *id)
26772246da4SFelipe Balbi {
2680f817ae6SFelipe Balbi 	struct dwc3_pci		*dwc;
26972246da4SFelipe Balbi 	struct resource		res[2];
270b09e99eeSAndy Shevchenko 	int			ret;
271802ca850SChanho Park 	struct device		*dev = &pci->dev;
27272246da4SFelipe Balbi 
273f1c7e710SAndy Shevchenko 	ret = pcim_enable_device(pci);
27472246da4SFelipe Balbi 	if (ret) {
275802ca850SChanho Park 		dev_err(dev, "failed to enable pci device\n");
276802ca850SChanho Park 		return -ENODEV;
27772246da4SFelipe Balbi 	}
27872246da4SFelipe Balbi 
27972246da4SFelipe Balbi 	pci_set_master(pci);
28072246da4SFelipe Balbi 
2810f817ae6SFelipe Balbi 	dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
2820f817ae6SFelipe Balbi 	if (!dwc)
283f1c7e710SAndy Shevchenko 		return -ENOMEM;
2840f817ae6SFelipe Balbi 
2850f817ae6SFelipe Balbi 	dwc->dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO);
2860f817ae6SFelipe Balbi 	if (!dwc->dwc3)
2870f817ae6SFelipe Balbi 		return -ENOMEM;
28872246da4SFelipe Balbi 
28972246da4SFelipe Balbi 	memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
29072246da4SFelipe Balbi 
29172246da4SFelipe Balbi 	res[0].start	= pci_resource_start(pci, 0);
29272246da4SFelipe Balbi 	res[0].end	= pci_resource_end(pci, 0);
29372246da4SFelipe Balbi 	res[0].name	= "dwc_usb3";
29472246da4SFelipe Balbi 	res[0].flags	= IORESOURCE_MEM;
29572246da4SFelipe Balbi 
29672246da4SFelipe Balbi 	res[1].start	= pci->irq;
29772246da4SFelipe Balbi 	res[1].name	= "dwc_usb3";
29872246da4SFelipe Balbi 	res[1].flags	= IORESOURCE_IRQ;
29972246da4SFelipe Balbi 
3000f817ae6SFelipe Balbi 	ret = platform_device_add_resources(dwc->dwc3, res, ARRAY_SIZE(res));
30172246da4SFelipe Balbi 	if (ret) {
302802ca850SChanho Park 		dev_err(dev, "couldn't add resources to dwc3 device\n");
303cabdf83dSThinh Nguyen 		goto err;
30472246da4SFelipe Balbi 	}
30572246da4SFelipe Balbi 
3060f817ae6SFelipe Balbi 	dwc->pci = pci;
3070f817ae6SFelipe Balbi 	dwc->dwc3->dev.parent = dev;
3080f817ae6SFelipe Balbi 	ACPI_COMPANION_SET(&dwc->dwc3->dev, ACPI_COMPANION(dev));
30972246da4SFelipe Balbi 
310e492ce9bSHeikki Krogerus 	ret = device_add_software_node(&dwc->dwc3->dev, (void *)id->driver_data);
3111a7b12f6SAndy Shevchenko 	if (ret < 0)
3129bbfceeaSNavid Emamdoost 		goto err;
3131a7b12f6SAndy Shevchenko 
3140f817ae6SFelipe Balbi 	ret = dwc3_pci_quirks(dwc);
315474799f0SHeikki Krogerus 	if (ret)
316474799f0SHeikki Krogerus 		goto err;
317474799f0SHeikki Krogerus 
3180f817ae6SFelipe Balbi 	ret = platform_device_add(dwc->dwc3);
31972246da4SFelipe Balbi 	if (ret) {
320802ca850SChanho Park 		dev_err(dev, "failed to register dwc3 device\n");
3212cd9ddf7SHeikki Krogerus 		goto err;
32272246da4SFelipe Balbi 	}
32372246da4SFelipe Balbi 
324e9af9229SFelipe Balbi 	device_init_wakeup(dev, true);
3250f817ae6SFelipe Balbi 	pci_set_drvdata(pci, dwc);
326e9af9229SFelipe Balbi 	pm_runtime_put(dev);
3278eed00b2SManu Gautam #ifdef CONFIG_PM
3288eed00b2SManu Gautam 	INIT_WORK(&dwc->wakeup_work, dwc3_pci_resume_work);
3298eed00b2SManu Gautam #endif
330e9af9229SFelipe Balbi 
33172246da4SFelipe Balbi 	return 0;
3322cd9ddf7SHeikki Krogerus err:
333e492ce9bSHeikki Krogerus 	device_remove_software_node(&dwc->dwc3->dev);
3340f817ae6SFelipe Balbi 	platform_device_put(dwc->dwc3);
33572246da4SFelipe Balbi 	return ret;
33672246da4SFelipe Balbi }
33772246da4SFelipe Balbi 
338fb4e98abSBill Pemberton static void dwc3_pci_remove(struct pci_dev *pci)
33972246da4SFelipe Balbi {
3400f817ae6SFelipe Balbi 	struct dwc3_pci		*dwc = pci_get_drvdata(pci);
3417b412b04SKuppuswamy Sathyanarayanan 	struct pci_dev		*pdev = dwc->pci;
3420f817ae6SFelipe Balbi 
3437b412b04SKuppuswamy Sathyanarayanan 	if (pdev->device == PCI_DEVICE_ID_INTEL_BYT)
3445741022cSHans de Goede 		gpiod_remove_lookup_table(&platform_bytcr_gpios);
3458eed00b2SManu Gautam #ifdef CONFIG_PM
3468eed00b2SManu Gautam 	cancel_work_sync(&dwc->wakeup_work);
3478eed00b2SManu Gautam #endif
348e9af9229SFelipe Balbi 	device_init_wakeup(&pci->dev, false);
349e9af9229SFelipe Balbi 	pm_runtime_get(&pci->dev);
350e492ce9bSHeikki Krogerus 	device_remove_software_node(&dwc->dwc3->dev);
3510f817ae6SFelipe Balbi 	platform_device_unregister(dwc->dwc3);
35272246da4SFelipe Balbi }
35372246da4SFelipe Balbi 
354782df20cSJingoo Han static const struct pci_device_id dwc3_pci_id_table[] = {
3551a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BSW),
356e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3571a7b12f6SAndy Shevchenko 
3581a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BYT),
359*d7c93a90SHans de Goede 	  (kernel_ulong_t) &dwc3_pci_intel_byt_swnode, },
3601a7b12f6SAndy Shevchenko 
3611a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MRFLD),
362e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_mrfld_swnode, },
3631a7b12f6SAndy Shevchenko 
3643c3caae4SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CMLLP),
365e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3663c3caae4SHeikki Krogerus 
3677ae622c9SFelipe Balbi 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CMLH),
368e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3697ae622c9SFelipe Balbi 
3701a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SPTLP),
371e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3721a7b12f6SAndy Shevchenko 
3731a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SPTH),
374e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3751a7b12f6SAndy Shevchenko 
3761a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BXT),
377e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3781a7b12f6SAndy Shevchenko 
3791a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BXT_M),
380e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3811a7b12f6SAndy Shevchenko 
3821a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_APL),
383e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3841a7b12f6SAndy Shevchenko 
3851a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_KBP),
386e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3871a7b12f6SAndy Shevchenko 
3881a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_GLK),
389e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3901a7b12f6SAndy Shevchenko 
3911a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CNPLP),
392e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3931a7b12f6SAndy Shevchenko 
3941a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CNPH),
395e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3961a7b12f6SAndy Shevchenko 
397f5ae8869SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CNPV),
398e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
399f5ae8869SHeikki Krogerus 
4001a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICLLP),
401e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
4021a7b12f6SAndy Shevchenko 
403457d2285SRaymond Tan 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_EHL),
404457d2285SRaymond Tan 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
405dbb0569dSFelipe Balbi 
406b3649deeSFelipe Balbi 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGPLP),
407e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
408b3649deeSFelipe Balbi 
409c3f595a8SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGPH),
410e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
411c3f595a8SHeikki Krogerus 
412e25d1e85SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_JSP),
413e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
414e25d1e85SHeikki Krogerus 
415f08fc2c3SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADLP),
416f08fc2c3SHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
417f08fc2c3SHeikki Krogerus 
418782de5e7SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADLM),
419782de5e7SHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
420782de5e7SHeikki Krogerus 
4211384ab4fSHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADLS),
422e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
4231384ab4fSHeikki Krogerus 
424038438a2SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPLS),
425038438a2SHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
426038438a2SHeikki Krogerus 
42773203bdeSHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL),
42873203bdeSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
42973203bdeSHeikki Krogerus 
4301a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_NL_USB),
431e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_amd_swnode, },
4321abade64SNehal Bakulchandra Shah 
4331abade64SNehal Bakulchandra Shah 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_MR),
4341abade64SNehal Bakulchandra Shah 	  (kernel_ulong_t)&dwc3_pci_amd_mr_swnode, },
4351abade64SNehal Bakulchandra Shah 
43672246da4SFelipe Balbi 	{  }	/* Terminating Entry */
43772246da4SFelipe Balbi };
43872246da4SFelipe Balbi MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
43972246da4SFelipe Balbi 
44036daf3aaSFelipe Balbi #if defined(CONFIG_PM) || defined(CONFIG_PM_SLEEP)
44136daf3aaSFelipe Balbi static int dwc3_pci_dsm(struct dwc3_pci *dwc, int param)
44236daf3aaSFelipe Balbi {
44336daf3aaSFelipe Balbi 	union acpi_object *obj;
44436daf3aaSFelipe Balbi 	union acpi_object tmp;
44536daf3aaSFelipe Balbi 	union acpi_object argv4 = ACPI_INIT_DSM_ARGV4(1, &tmp);
44636daf3aaSFelipe Balbi 
44736daf3aaSFelipe Balbi 	if (!dwc->has_dsm_for_pm)
44836daf3aaSFelipe Balbi 		return 0;
44936daf3aaSFelipe Balbi 
45036daf3aaSFelipe Balbi 	tmp.type = ACPI_TYPE_INTEGER;
45136daf3aaSFelipe Balbi 	tmp.integer.value = param;
45236daf3aaSFelipe Balbi 
45394116f81SAndy Shevchenko 	obj = acpi_evaluate_dsm(ACPI_HANDLE(&dwc->pci->dev), &dwc->guid,
45436daf3aaSFelipe Balbi 			1, PCI_INTEL_BXT_FUNC_PMU_PWR, &argv4);
45536daf3aaSFelipe Balbi 	if (!obj) {
45636daf3aaSFelipe Balbi 		dev_err(&dwc->pci->dev, "failed to evaluate _DSM\n");
45736daf3aaSFelipe Balbi 		return -EIO;
45836daf3aaSFelipe Balbi 	}
45936daf3aaSFelipe Balbi 
46036daf3aaSFelipe Balbi 	ACPI_FREE(obj);
46136daf3aaSFelipe Balbi 
46236daf3aaSFelipe Balbi 	return 0;
46336daf3aaSFelipe Balbi }
46436daf3aaSFelipe Balbi #endif /* CONFIG_PM || CONFIG_PM_SLEEP */
46536daf3aaSFelipe Balbi 
466e9af9229SFelipe Balbi #ifdef CONFIG_PM
467e9af9229SFelipe Balbi static int dwc3_pci_runtime_suspend(struct device *dev)
468e9af9229SFelipe Balbi {
4699cecca75SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
4709cecca75SFelipe Balbi 
471de3ef1ebSRafael J. Wysocki 	if (device_can_wakeup(dev))
4729cecca75SFelipe Balbi 		return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D3);
473e9af9229SFelipe Balbi 
474e9af9229SFelipe Balbi 	return -EBUSY;
475e9af9229SFelipe Balbi }
476e9af9229SFelipe Balbi 
477f6c274e1SFelipe Balbi static int dwc3_pci_runtime_resume(struct device *dev)
478f6c274e1SFelipe Balbi {
4790f817ae6SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
4809cecca75SFelipe Balbi 	int			ret;
4819cecca75SFelipe Balbi 
4829cecca75SFelipe Balbi 	ret = dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D0);
4839cecca75SFelipe Balbi 	if (ret)
4849cecca75SFelipe Balbi 		return ret;
485f6c274e1SFelipe Balbi 
4868eed00b2SManu Gautam 	queue_work(pm_wq, &dwc->wakeup_work);
4878eed00b2SManu Gautam 
4888eed00b2SManu Gautam 	return 0;
489f6c274e1SFelipe Balbi }
490696118c0SFelipe Balbi #endif /* CONFIG_PM */
491f6c274e1SFelipe Balbi 
492696118c0SFelipe Balbi #ifdef CONFIG_PM_SLEEP
4939cecca75SFelipe Balbi static int dwc3_pci_suspend(struct device *dev)
494e9af9229SFelipe Balbi {
4959cecca75SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
4969cecca75SFelipe Balbi 
4979cecca75SFelipe Balbi 	return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D3);
4989cecca75SFelipe Balbi }
4999cecca75SFelipe Balbi 
5009cecca75SFelipe Balbi static int dwc3_pci_resume(struct device *dev)
5019cecca75SFelipe Balbi {
5029cecca75SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
5039cecca75SFelipe Balbi 
5049cecca75SFelipe Balbi 	return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D0);
505e9af9229SFelipe Balbi }
506696118c0SFelipe Balbi #endif /* CONFIG_PM_SLEEP */
507e9af9229SFelipe Balbi 
50895aa932cSDoug Wilson static const struct dev_pm_ops dwc3_pci_dev_pm_ops = {
5099cecca75SFelipe Balbi 	SET_SYSTEM_SLEEP_PM_OPS(dwc3_pci_suspend, dwc3_pci_resume)
510f6c274e1SFelipe Balbi 	SET_RUNTIME_PM_OPS(dwc3_pci_runtime_suspend, dwc3_pci_runtime_resume,
511e9af9229SFelipe Balbi 		NULL)
512e9af9229SFelipe Balbi };
513e9af9229SFelipe Balbi 
51472246da4SFelipe Balbi static struct pci_driver dwc3_pci_driver = {
5150949e99bSFelipe Balbi 	.name		= "dwc3-pci",
51672246da4SFelipe Balbi 	.id_table	= dwc3_pci_id_table,
51772246da4SFelipe Balbi 	.probe		= dwc3_pci_probe,
5187690417dSBill Pemberton 	.remove		= dwc3_pci_remove,
519e9af9229SFelipe Balbi 	.driver		= {
520e9af9229SFelipe Balbi 		.pm	= &dwc3_pci_dev_pm_ops,
521e9af9229SFelipe Balbi 	}
52272246da4SFelipe Balbi };
52372246da4SFelipe Balbi 
52472246da4SFelipe Balbi MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
5255945f789SFelipe Balbi MODULE_LICENSE("GPL v2");
52672246da4SFelipe Balbi MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
52772246da4SFelipe Balbi 
52895656336SGreg Kroah-Hartman module_pci_driver(dwc3_pci_driver);
529