xref: /linux/drivers/usb/dwc3/dwc3-pci.c (revision 1abade64563ef5388db545b55cf158e849f6e717)
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
4673203bdeSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_TGL			0x9a15
47*1abade64SNehal Bakulchandra Shah #define PCI_DEVICE_ID_AMD_MR			0x163a
4872246da4SFelipe Balbi 
4994116f81SAndy Shevchenko #define PCI_INTEL_BXT_DSM_GUID		"732b85d5-b7a7-4a1b-9ba0-4bbd00ffd511"
509cecca75SFelipe Balbi #define PCI_INTEL_BXT_FUNC_PMU_PWR	4
519cecca75SFelipe Balbi #define PCI_INTEL_BXT_STATE_D0		0
529cecca75SFelipe Balbi #define PCI_INTEL_BXT_STATE_D3		3
539cecca75SFelipe Balbi 
547740d04dSHans de Goede #define GP_RWBAR			1
557740d04dSHans de Goede #define GP_RWREG1			0xa0
567740d04dSHans de Goede #define GP_RWREG1_ULPI_REFCLK_DISABLE	(1 << 17)
577740d04dSHans de Goede 
580f817ae6SFelipe Balbi /**
590f817ae6SFelipe Balbi  * struct dwc3_pci - Driver private structure
600f817ae6SFelipe Balbi  * @dwc3: child dwc3 platform_device
610f817ae6SFelipe Balbi  * @pci: our link to PCI bus
6294116f81SAndy Shevchenko  * @guid: _DSM GUID
639cecca75SFelipe Balbi  * @has_dsm_for_pm: true for devices which need to run _DSM on runtime PM
6487d852deSAndy Shevchenko  * @wakeup_work: work for asynchronous resume
650f817ae6SFelipe Balbi  */
660f817ae6SFelipe Balbi struct dwc3_pci {
670f817ae6SFelipe Balbi 	struct platform_device *dwc3;
680f817ae6SFelipe Balbi 	struct pci_dev *pci;
699cecca75SFelipe Balbi 
7094116f81SAndy Shevchenko 	guid_t guid;
719cecca75SFelipe Balbi 
729cecca75SFelipe Balbi 	unsigned int has_dsm_for_pm:1;
738eed00b2SManu Gautam 	struct work_struct wakeup_work;
740f817ae6SFelipe Balbi };
750f817ae6SFelipe Balbi 
76a89d977cSHeikki Krogerus static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
77a89d977cSHeikki Krogerus static const struct acpi_gpio_params cs_gpios = { 1, 0, false };
78a89d977cSHeikki Krogerus 
79a89d977cSHeikki Krogerus static const struct acpi_gpio_mapping acpi_dwc3_byt_gpios[] = {
80a89d977cSHeikki Krogerus 	{ "reset-gpios", &reset_gpios, 1 },
81a89d977cSHeikki Krogerus 	{ "cs-gpios", &cs_gpios, 1 },
82a89d977cSHeikki Krogerus 	{ },
83a89d977cSHeikki Krogerus };
84a89d977cSHeikki Krogerus 
855741022cSHans de Goede static struct gpiod_lookup_table platform_bytcr_gpios = {
865741022cSHans de Goede 	.dev_id		= "0000:00:16.0",
875741022cSHans de Goede 	.table		= {
885741022cSHans de Goede 		GPIO_LOOKUP("INT33FC:00", 54, "reset", GPIO_ACTIVE_HIGH),
895741022cSHans de Goede 		GPIO_LOOKUP("INT33FC:02", 14, "cs", GPIO_ACTIVE_HIGH),
905741022cSHans de Goede 		{}
915741022cSHans de Goede 	},
925741022cSHans de Goede };
935741022cSHans de Goede 
947740d04dSHans de Goede static int dwc3_byt_enable_ulpi_refclock(struct pci_dev *pci)
957740d04dSHans de Goede {
967740d04dSHans de Goede 	void __iomem	*reg;
977740d04dSHans de Goede 	u32		value;
987740d04dSHans de Goede 
997740d04dSHans de Goede 	reg = pcim_iomap(pci, GP_RWBAR, 0);
100b497fff6SWei Yongjun 	if (!reg)
101b497fff6SWei Yongjun 		return -ENOMEM;
1027740d04dSHans de Goede 
1037740d04dSHans de Goede 	value = readl(reg + GP_RWREG1);
1047740d04dSHans de Goede 	if (!(value & GP_RWREG1_ULPI_REFCLK_DISABLE))
1057740d04dSHans de Goede 		goto unmap; /* ULPI refclk already enabled */
1067740d04dSHans de Goede 
1077740d04dSHans de Goede 	value &= ~GP_RWREG1_ULPI_REFCLK_DISABLE;
1087740d04dSHans de Goede 	writel(value, reg + GP_RWREG1);
1097740d04dSHans de Goede 	/* This comes from the Intel Android x86 tree w/o any explanation */
1107740d04dSHans de Goede 	msleep(100);
1117740d04dSHans de Goede unmap:
1127740d04dSHans de Goede 	pcim_iounmap(pci, reg);
1137740d04dSHans de Goede 	return 0;
1147740d04dSHans de Goede }
1157740d04dSHans de Goede 
1161a7b12f6SAndy Shevchenko static const struct property_entry dwc3_pci_intel_properties[] = {
1171a7b12f6SAndy Shevchenko 	PROPERTY_ENTRY_STRING("dr_mode", "peripheral"),
1181a7b12f6SAndy Shevchenko 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
1191a7b12f6SAndy Shevchenko 	{}
1201a7b12f6SAndy Shevchenko };
1210f817ae6SFelipe Balbi 
122c31d983bSAndy Shevchenko static const struct property_entry dwc3_pci_mrfld_properties[] = {
123c31d983bSAndy Shevchenko 	PROPERTY_ENTRY_STRING("dr_mode", "otg"),
124066c0959SAndy Shevchenko 	PROPERTY_ENTRY_STRING("linux,extcon-name", "mrfld_bcove_pwrsrc"),
125b522f830SAndy Shevchenko 	PROPERTY_ENTRY_BOOL("snps,dis_u3_susphy_quirk"),
126b522f830SAndy Shevchenko 	PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
12704357fafSFerry Toth 	PROPERTY_ENTRY_BOOL("snps,usb2-gadget-lpm-disable"),
128c31d983bSAndy Shevchenko 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
129c31d983bSAndy Shevchenko 	{}
130c31d983bSAndy Shevchenko };
131c31d983bSAndy Shevchenko 
1321a7b12f6SAndy Shevchenko static const struct property_entry dwc3_pci_amd_properties[] = {
133cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,has-lpm-erratum"),
134cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_U8("snps,lpm-nyet-threshold", 0xf),
135cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,u2exit_lfps_quirk"),
136cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,u2ss_inp3_quirk"),
137cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,req_p1p2p3_quirk"),
138cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,del_p1p2p3_quirk"),
139cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,del_phy_power_chg_quirk"),
140cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,lfps_filter_quirk"),
141cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,rx_detect_poll_quirk"),
142cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,tx_de_emphasis_quirk"),
143cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_U8("snps,tx_de_emphasis", 1),
1441a7b12f6SAndy Shevchenko 	/* FIXME these quirks should be removed when AMD NL tapes out */
145cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,disable_scramble_quirk"),
146cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,dis_u3_susphy_quirk"),
147cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
1480eae2fdeSJohn Youn 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
149e6fe66feSFelipe Balbi 	{}
150e6fe66feSFelipe Balbi };
151e6fe66feSFelipe Balbi 
152*1abade64SNehal Bakulchandra Shah static const struct property_entry dwc3_pci_mr_properties[] = {
153*1abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_STRING("dr_mode", "otg"),
154*1abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_BOOL("usb-role-switch"),
155*1abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_STRING("role-switch-default-mode", "host"),
156*1abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
157*1abade64SNehal Bakulchandra Shah 	{}
158*1abade64SNehal Bakulchandra Shah };
159*1abade64SNehal Bakulchandra Shah 
160e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_intel_swnode = {
161e492ce9bSHeikki Krogerus 	.properties = dwc3_pci_intel_properties,
162e492ce9bSHeikki Krogerus };
163e492ce9bSHeikki Krogerus 
164e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_intel_mrfld_swnode = {
165e492ce9bSHeikki Krogerus 	.properties = dwc3_pci_mrfld_properties,
166e492ce9bSHeikki Krogerus };
167e492ce9bSHeikki Krogerus 
168e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_amd_swnode = {
169e492ce9bSHeikki Krogerus 	.properties = dwc3_pci_amd_properties,
170e492ce9bSHeikki Krogerus };
171e492ce9bSHeikki Krogerus 
172*1abade64SNehal Bakulchandra Shah static const struct software_node dwc3_pci_amd_mr_swnode = {
173*1abade64SNehal Bakulchandra Shah 	.properties = dwc3_pci_mr_properties,
174*1abade64SNehal Bakulchandra Shah };
175*1abade64SNehal Bakulchandra Shah 
1761a7b12f6SAndy Shevchenko static int dwc3_pci_quirks(struct dwc3_pci *dwc)
1771a7b12f6SAndy Shevchenko {
1781a7b12f6SAndy Shevchenko 	struct pci_dev			*pdev = dwc->pci;
179e6fe66feSFelipe Balbi 
1801a7b12f6SAndy Shevchenko 	if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
1819cecca75SFelipe Balbi 		if (pdev->device == PCI_DEVICE_ID_INTEL_BXT ||
182a609ce2aSRaymond Tan 		    pdev->device == PCI_DEVICE_ID_INTEL_BXT_M ||
183457d2285SRaymond Tan 		    pdev->device == PCI_DEVICE_ID_INTEL_EHL) {
18494116f81SAndy Shevchenko 			guid_parse(PCI_INTEL_BXT_DSM_GUID, &dwc->guid);
1859cecca75SFelipe Balbi 			dwc->has_dsm_for_pm = true;
1869cecca75SFelipe Balbi 		}
1879cecca75SFelipe Balbi 
188e6fe66feSFelipe Balbi 		if (pdev->device == PCI_DEVICE_ID_INTEL_BYT) {
189a89d977cSHeikki Krogerus 			struct gpio_desc *gpio;
1901a7b12f6SAndy Shevchenko 			int ret;
191a89d977cSHeikki Krogerus 
1927740d04dSHans de Goede 			/* On BYT the FW does not always enable the refclock */
1937740d04dSHans de Goede 			ret = dwc3_byt_enable_ulpi_refclock(pdev);
1947740d04dSHans de Goede 			if (ret)
1957740d04dSHans de Goede 				return ret;
1967740d04dSHans de Goede 
1974a56e413SAndy Shevchenko 			ret = devm_acpi_dev_add_driver_gpios(&pdev->dev,
198a89d977cSHeikki Krogerus 					acpi_dwc3_byt_gpios);
1994a56e413SAndy Shevchenko 			if (ret)
2004a56e413SAndy Shevchenko 				dev_dbg(&pdev->dev, "failed to add mapping table\n");
201a89d977cSHeikki Krogerus 
2022df033caSUwe Kleine-König 			/*
2035741022cSHans de Goede 			 * A lot of BYT devices lack ACPI resource entries for
2045741022cSHans de Goede 			 * the GPIOs, add a fallback mapping to the reference
2055741022cSHans de Goede 			 * design GPIOs which all boards seem to use.
2065741022cSHans de Goede 			 */
2075741022cSHans de Goede 			gpiod_add_lookup_table(&platform_bytcr_gpios);
2085741022cSHans de Goede 
2095741022cSHans de Goede 			/*
2102df033caSUwe Kleine-König 			 * These GPIOs will turn on the USB2 PHY. Note that we have to
2112df033caSUwe Kleine-König 			 * put the gpio descriptors again here because the phy driver
2122df033caSUwe Kleine-König 			 * might want to grab them, too.
2132df033caSUwe Kleine-König 			 */
2143004cfd6SStephan Gerhold 			gpio = gpiod_get_optional(&pdev->dev, "cs", GPIOD_OUT_LOW);
2152df033caSUwe Kleine-König 			if (IS_ERR(gpio))
2162df033caSUwe Kleine-König 				return PTR_ERR(gpio);
2172df033caSUwe Kleine-König 
218a89d977cSHeikki Krogerus 			gpiod_set_value_cansleep(gpio, 1);
2193004cfd6SStephan Gerhold 			gpiod_put(gpio);
220a89d977cSHeikki Krogerus 
2213004cfd6SStephan Gerhold 			gpio = gpiod_get_optional(&pdev->dev, "reset", GPIOD_OUT_LOW);
2222df033caSUwe Kleine-König 			if (IS_ERR(gpio))
2232df033caSUwe Kleine-König 				return PTR_ERR(gpio);
2242df033caSUwe Kleine-König 
2252df033caSUwe Kleine-König 			if (gpio) {
226a89d977cSHeikki Krogerus 				gpiod_set_value_cansleep(gpio, 1);
2273004cfd6SStephan Gerhold 				gpiod_put(gpio);
228a89d977cSHeikki Krogerus 				usleep_range(10000, 11000);
229a89d977cSHeikki Krogerus 			}
230a89d977cSHeikki Krogerus 		}
231e6fe66feSFelipe Balbi 	}
232a89d977cSHeikki Krogerus 
2332cd9ddf7SHeikki Krogerus 	return 0;
2342cd9ddf7SHeikki Krogerus }
23572246da4SFelipe Balbi 
2368eed00b2SManu Gautam #ifdef CONFIG_PM
2378eed00b2SManu Gautam static void dwc3_pci_resume_work(struct work_struct *work)
2388eed00b2SManu Gautam {
2398eed00b2SManu Gautam 	struct dwc3_pci *dwc = container_of(work, struct dwc3_pci, wakeup_work);
2408eed00b2SManu Gautam 	struct platform_device *dwc3 = dwc->dwc3;
2418eed00b2SManu Gautam 	int ret;
2428eed00b2SManu Gautam 
2438eed00b2SManu Gautam 	ret = pm_runtime_get_sync(&dwc3->dev);
2442655971aSAditya Pakki 	if (ret) {
2452655971aSAditya Pakki 		pm_runtime_put_sync_autosuspend(&dwc3->dev);
2468eed00b2SManu Gautam 		return;
2472655971aSAditya Pakki 	}
2488eed00b2SManu Gautam 
2498eed00b2SManu Gautam 	pm_runtime_mark_last_busy(&dwc3->dev);
2508eed00b2SManu Gautam 	pm_runtime_put_sync_autosuspend(&dwc3->dev);
2518eed00b2SManu Gautam }
2528eed00b2SManu Gautam #endif
2538eed00b2SManu Gautam 
2541a7b12f6SAndy Shevchenko static int dwc3_pci_probe(struct pci_dev *pci, const struct pci_device_id *id)
25572246da4SFelipe Balbi {
2560f817ae6SFelipe Balbi 	struct dwc3_pci		*dwc;
25772246da4SFelipe Balbi 	struct resource		res[2];
258b09e99eeSAndy Shevchenko 	int			ret;
259802ca850SChanho Park 	struct device		*dev = &pci->dev;
26072246da4SFelipe Balbi 
261f1c7e710SAndy Shevchenko 	ret = pcim_enable_device(pci);
26272246da4SFelipe Balbi 	if (ret) {
263802ca850SChanho Park 		dev_err(dev, "failed to enable pci device\n");
264802ca850SChanho Park 		return -ENODEV;
26572246da4SFelipe Balbi 	}
26672246da4SFelipe Balbi 
26772246da4SFelipe Balbi 	pci_set_master(pci);
26872246da4SFelipe Balbi 
2690f817ae6SFelipe Balbi 	dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
2700f817ae6SFelipe Balbi 	if (!dwc)
271f1c7e710SAndy Shevchenko 		return -ENOMEM;
2720f817ae6SFelipe Balbi 
2730f817ae6SFelipe Balbi 	dwc->dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO);
2740f817ae6SFelipe Balbi 	if (!dwc->dwc3)
2750f817ae6SFelipe Balbi 		return -ENOMEM;
27672246da4SFelipe Balbi 
27772246da4SFelipe Balbi 	memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
27872246da4SFelipe Balbi 
27972246da4SFelipe Balbi 	res[0].start	= pci_resource_start(pci, 0);
28072246da4SFelipe Balbi 	res[0].end	= pci_resource_end(pci, 0);
28172246da4SFelipe Balbi 	res[0].name	= "dwc_usb3";
28272246da4SFelipe Balbi 	res[0].flags	= IORESOURCE_MEM;
28372246da4SFelipe Balbi 
28472246da4SFelipe Balbi 	res[1].start	= pci->irq;
28572246da4SFelipe Balbi 	res[1].name	= "dwc_usb3";
28672246da4SFelipe Balbi 	res[1].flags	= IORESOURCE_IRQ;
28772246da4SFelipe Balbi 
2880f817ae6SFelipe Balbi 	ret = platform_device_add_resources(dwc->dwc3, res, ARRAY_SIZE(res));
28972246da4SFelipe Balbi 	if (ret) {
290802ca850SChanho Park 		dev_err(dev, "couldn't add resources to dwc3 device\n");
291cabdf83dSThinh Nguyen 		goto err;
29272246da4SFelipe Balbi 	}
29372246da4SFelipe Balbi 
2940f817ae6SFelipe Balbi 	dwc->pci = pci;
2950f817ae6SFelipe Balbi 	dwc->dwc3->dev.parent = dev;
2960f817ae6SFelipe Balbi 	ACPI_COMPANION_SET(&dwc->dwc3->dev, ACPI_COMPANION(dev));
29772246da4SFelipe Balbi 
298e492ce9bSHeikki Krogerus 	ret = device_add_software_node(&dwc->dwc3->dev, (void *)id->driver_data);
2991a7b12f6SAndy Shevchenko 	if (ret < 0)
3009bbfceeaSNavid Emamdoost 		goto err;
3011a7b12f6SAndy Shevchenko 
3020f817ae6SFelipe Balbi 	ret = dwc3_pci_quirks(dwc);
303474799f0SHeikki Krogerus 	if (ret)
304474799f0SHeikki Krogerus 		goto err;
305474799f0SHeikki Krogerus 
3060f817ae6SFelipe Balbi 	ret = platform_device_add(dwc->dwc3);
30772246da4SFelipe Balbi 	if (ret) {
308802ca850SChanho Park 		dev_err(dev, "failed to register dwc3 device\n");
3092cd9ddf7SHeikki Krogerus 		goto err;
31072246da4SFelipe Balbi 	}
31172246da4SFelipe Balbi 
312e9af9229SFelipe Balbi 	device_init_wakeup(dev, true);
3130f817ae6SFelipe Balbi 	pci_set_drvdata(pci, dwc);
314e9af9229SFelipe Balbi 	pm_runtime_put(dev);
3158eed00b2SManu Gautam #ifdef CONFIG_PM
3168eed00b2SManu Gautam 	INIT_WORK(&dwc->wakeup_work, dwc3_pci_resume_work);
3178eed00b2SManu Gautam #endif
318e9af9229SFelipe Balbi 
31972246da4SFelipe Balbi 	return 0;
3202cd9ddf7SHeikki Krogerus err:
321e492ce9bSHeikki Krogerus 	device_remove_software_node(&dwc->dwc3->dev);
3220f817ae6SFelipe Balbi 	platform_device_put(dwc->dwc3);
32372246da4SFelipe Balbi 	return ret;
32472246da4SFelipe Balbi }
32572246da4SFelipe Balbi 
326fb4e98abSBill Pemberton static void dwc3_pci_remove(struct pci_dev *pci)
32772246da4SFelipe Balbi {
3280f817ae6SFelipe Balbi 	struct dwc3_pci		*dwc = pci_get_drvdata(pci);
3297b412b04SKuppuswamy Sathyanarayanan 	struct pci_dev		*pdev = dwc->pci;
3300f817ae6SFelipe Balbi 
3317b412b04SKuppuswamy Sathyanarayanan 	if (pdev->device == PCI_DEVICE_ID_INTEL_BYT)
3325741022cSHans de Goede 		gpiod_remove_lookup_table(&platform_bytcr_gpios);
3338eed00b2SManu Gautam #ifdef CONFIG_PM
3348eed00b2SManu Gautam 	cancel_work_sync(&dwc->wakeup_work);
3358eed00b2SManu Gautam #endif
336e9af9229SFelipe Balbi 	device_init_wakeup(&pci->dev, false);
337e9af9229SFelipe Balbi 	pm_runtime_get(&pci->dev);
338e492ce9bSHeikki Krogerus 	device_remove_software_node(&dwc->dwc3->dev);
3390f817ae6SFelipe Balbi 	platform_device_unregister(dwc->dwc3);
34072246da4SFelipe Balbi }
34172246da4SFelipe Balbi 
342782df20cSJingoo Han static const struct pci_device_id dwc3_pci_id_table[] = {
3431a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BSW),
344e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3451a7b12f6SAndy Shevchenko 
3461a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BYT),
347e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3481a7b12f6SAndy Shevchenko 
3491a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MRFLD),
350e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_mrfld_swnode, },
3511a7b12f6SAndy Shevchenko 
3523c3caae4SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CMLLP),
353e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3543c3caae4SHeikki Krogerus 
3557ae622c9SFelipe Balbi 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CMLH),
356e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3577ae622c9SFelipe Balbi 
3581a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SPTLP),
359e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3601a7b12f6SAndy Shevchenko 
3611a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SPTH),
362e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3631a7b12f6SAndy Shevchenko 
3641a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BXT),
365e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3661a7b12f6SAndy Shevchenko 
3671a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BXT_M),
368e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3691a7b12f6SAndy Shevchenko 
3701a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_APL),
371e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3721a7b12f6SAndy Shevchenko 
3731a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_KBP),
374e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3751a7b12f6SAndy Shevchenko 
3761a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_GLK),
377e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3781a7b12f6SAndy Shevchenko 
3791a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CNPLP),
380e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3811a7b12f6SAndy Shevchenko 
3821a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CNPH),
383e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3841a7b12f6SAndy Shevchenko 
385f5ae8869SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CNPV),
386e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
387f5ae8869SHeikki Krogerus 
3881a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICLLP),
389e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3901a7b12f6SAndy Shevchenko 
391457d2285SRaymond Tan 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_EHL),
392457d2285SRaymond Tan 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
393dbb0569dSFelipe Balbi 
394b3649deeSFelipe Balbi 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGPLP),
395e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
396b3649deeSFelipe Balbi 
397c3f595a8SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGPH),
398e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
399c3f595a8SHeikki Krogerus 
400e25d1e85SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_JSP),
401e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
402e25d1e85SHeikki Krogerus 
403f08fc2c3SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADLP),
404f08fc2c3SHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
405f08fc2c3SHeikki Krogerus 
406782de5e7SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADLM),
407782de5e7SHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
408782de5e7SHeikki Krogerus 
4091384ab4fSHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADLS),
410e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
4111384ab4fSHeikki Krogerus 
41273203bdeSHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL),
41373203bdeSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
41473203bdeSHeikki Krogerus 
4151a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_NL_USB),
416e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_amd_swnode, },
417*1abade64SNehal Bakulchandra Shah 
418*1abade64SNehal Bakulchandra Shah 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_MR),
419*1abade64SNehal Bakulchandra Shah 	  (kernel_ulong_t)&dwc3_pci_amd_mr_swnode, },
420*1abade64SNehal Bakulchandra Shah 
42172246da4SFelipe Balbi 	{  }	/* Terminating Entry */
42272246da4SFelipe Balbi };
42372246da4SFelipe Balbi MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
42472246da4SFelipe Balbi 
42536daf3aaSFelipe Balbi #if defined(CONFIG_PM) || defined(CONFIG_PM_SLEEP)
42636daf3aaSFelipe Balbi static int dwc3_pci_dsm(struct dwc3_pci *dwc, int param)
42736daf3aaSFelipe Balbi {
42836daf3aaSFelipe Balbi 	union acpi_object *obj;
42936daf3aaSFelipe Balbi 	union acpi_object tmp;
43036daf3aaSFelipe Balbi 	union acpi_object argv4 = ACPI_INIT_DSM_ARGV4(1, &tmp);
43136daf3aaSFelipe Balbi 
43236daf3aaSFelipe Balbi 	if (!dwc->has_dsm_for_pm)
43336daf3aaSFelipe Balbi 		return 0;
43436daf3aaSFelipe Balbi 
43536daf3aaSFelipe Balbi 	tmp.type = ACPI_TYPE_INTEGER;
43636daf3aaSFelipe Balbi 	tmp.integer.value = param;
43736daf3aaSFelipe Balbi 
43894116f81SAndy Shevchenko 	obj = acpi_evaluate_dsm(ACPI_HANDLE(&dwc->pci->dev), &dwc->guid,
43936daf3aaSFelipe Balbi 			1, PCI_INTEL_BXT_FUNC_PMU_PWR, &argv4);
44036daf3aaSFelipe Balbi 	if (!obj) {
44136daf3aaSFelipe Balbi 		dev_err(&dwc->pci->dev, "failed to evaluate _DSM\n");
44236daf3aaSFelipe Balbi 		return -EIO;
44336daf3aaSFelipe Balbi 	}
44436daf3aaSFelipe Balbi 
44536daf3aaSFelipe Balbi 	ACPI_FREE(obj);
44636daf3aaSFelipe Balbi 
44736daf3aaSFelipe Balbi 	return 0;
44836daf3aaSFelipe Balbi }
44936daf3aaSFelipe Balbi #endif /* CONFIG_PM || CONFIG_PM_SLEEP */
45036daf3aaSFelipe Balbi 
451e9af9229SFelipe Balbi #ifdef CONFIG_PM
452e9af9229SFelipe Balbi static int dwc3_pci_runtime_suspend(struct device *dev)
453e9af9229SFelipe Balbi {
4549cecca75SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
4559cecca75SFelipe Balbi 
456de3ef1ebSRafael J. Wysocki 	if (device_can_wakeup(dev))
4579cecca75SFelipe Balbi 		return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D3);
458e9af9229SFelipe Balbi 
459e9af9229SFelipe Balbi 	return -EBUSY;
460e9af9229SFelipe Balbi }
461e9af9229SFelipe Balbi 
462f6c274e1SFelipe Balbi static int dwc3_pci_runtime_resume(struct device *dev)
463f6c274e1SFelipe Balbi {
4640f817ae6SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
4659cecca75SFelipe Balbi 	int			ret;
4669cecca75SFelipe Balbi 
4679cecca75SFelipe Balbi 	ret = dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D0);
4689cecca75SFelipe Balbi 	if (ret)
4699cecca75SFelipe Balbi 		return ret;
470f6c274e1SFelipe Balbi 
4718eed00b2SManu Gautam 	queue_work(pm_wq, &dwc->wakeup_work);
4728eed00b2SManu Gautam 
4738eed00b2SManu Gautam 	return 0;
474f6c274e1SFelipe Balbi }
475696118c0SFelipe Balbi #endif /* CONFIG_PM */
476f6c274e1SFelipe Balbi 
477696118c0SFelipe Balbi #ifdef CONFIG_PM_SLEEP
4789cecca75SFelipe Balbi static int dwc3_pci_suspend(struct device *dev)
479e9af9229SFelipe Balbi {
4809cecca75SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
4819cecca75SFelipe Balbi 
4829cecca75SFelipe Balbi 	return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D3);
4839cecca75SFelipe Balbi }
4849cecca75SFelipe Balbi 
4859cecca75SFelipe Balbi static int dwc3_pci_resume(struct device *dev)
4869cecca75SFelipe Balbi {
4879cecca75SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
4889cecca75SFelipe Balbi 
4899cecca75SFelipe Balbi 	return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D0);
490e9af9229SFelipe Balbi }
491696118c0SFelipe Balbi #endif /* CONFIG_PM_SLEEP */
492e9af9229SFelipe Balbi 
49395aa932cSDoug Wilson static const struct dev_pm_ops dwc3_pci_dev_pm_ops = {
4949cecca75SFelipe Balbi 	SET_SYSTEM_SLEEP_PM_OPS(dwc3_pci_suspend, dwc3_pci_resume)
495f6c274e1SFelipe Balbi 	SET_RUNTIME_PM_OPS(dwc3_pci_runtime_suspend, dwc3_pci_runtime_resume,
496e9af9229SFelipe Balbi 		NULL)
497e9af9229SFelipe Balbi };
498e9af9229SFelipe Balbi 
49972246da4SFelipe Balbi static struct pci_driver dwc3_pci_driver = {
5000949e99bSFelipe Balbi 	.name		= "dwc3-pci",
50172246da4SFelipe Balbi 	.id_table	= dwc3_pci_id_table,
50272246da4SFelipe Balbi 	.probe		= dwc3_pci_probe,
5037690417dSBill Pemberton 	.remove		= dwc3_pci_remove,
504e9af9229SFelipe Balbi 	.driver		= {
505e9af9229SFelipe Balbi 		.pm	= &dwc3_pci_dev_pm_ops,
506e9af9229SFelipe Balbi 	}
50772246da4SFelipe Balbi };
50872246da4SFelipe Balbi 
50972246da4SFelipe Balbi MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
5105945f789SFelipe Balbi MODULE_LICENSE("GPL v2");
51172246da4SFelipe Balbi MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
51272246da4SFelipe Balbi 
51395656336SGreg Kroah-Hartman module_pci_driver(dwc3_pci_driver);
514