xref: /linux/drivers/usb/dwc3/dwc3-pci.c (revision 27088e00b623a9581bcd4e443a6f9380524edfce)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2*27088e00SAditya 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
39dbb0569dSFelipe Balbi #define PCI_DEVICE_ID_INTEL_EHLLP		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
441384ab4fSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_ADLS		0x7ae1
4573203bdeSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_TGL			0x9a15
4672246da4SFelipe Balbi 
4794116f81SAndy Shevchenko #define PCI_INTEL_BXT_DSM_GUID		"732b85d5-b7a7-4a1b-9ba0-4bbd00ffd511"
489cecca75SFelipe Balbi #define PCI_INTEL_BXT_FUNC_PMU_PWR	4
499cecca75SFelipe Balbi #define PCI_INTEL_BXT_STATE_D0		0
509cecca75SFelipe Balbi #define PCI_INTEL_BXT_STATE_D3		3
519cecca75SFelipe Balbi 
527740d04dSHans de Goede #define GP_RWBAR			1
537740d04dSHans de Goede #define GP_RWREG1			0xa0
547740d04dSHans de Goede #define GP_RWREG1_ULPI_REFCLK_DISABLE	(1 << 17)
557740d04dSHans de Goede 
560f817ae6SFelipe Balbi /**
570f817ae6SFelipe Balbi  * struct dwc3_pci - Driver private structure
580f817ae6SFelipe Balbi  * @dwc3: child dwc3 platform_device
590f817ae6SFelipe Balbi  * @pci: our link to PCI bus
6094116f81SAndy Shevchenko  * @guid: _DSM GUID
619cecca75SFelipe Balbi  * @has_dsm_for_pm: true for devices which need to run _DSM on runtime PM
6287d852deSAndy Shevchenko  * @wakeup_work: work for asynchronous resume
630f817ae6SFelipe Balbi  */
640f817ae6SFelipe Balbi struct dwc3_pci {
650f817ae6SFelipe Balbi 	struct platform_device *dwc3;
660f817ae6SFelipe Balbi 	struct pci_dev *pci;
679cecca75SFelipe Balbi 
6894116f81SAndy Shevchenko 	guid_t guid;
699cecca75SFelipe Balbi 
709cecca75SFelipe Balbi 	unsigned int has_dsm_for_pm:1;
718eed00b2SManu Gautam 	struct work_struct wakeup_work;
720f817ae6SFelipe Balbi };
730f817ae6SFelipe Balbi 
74a89d977cSHeikki Krogerus static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
75a89d977cSHeikki Krogerus static const struct acpi_gpio_params cs_gpios = { 1, 0, false };
76a89d977cSHeikki Krogerus 
77a89d977cSHeikki Krogerus static const struct acpi_gpio_mapping acpi_dwc3_byt_gpios[] = {
78a89d977cSHeikki Krogerus 	{ "reset-gpios", &reset_gpios, 1 },
79a89d977cSHeikki Krogerus 	{ "cs-gpios", &cs_gpios, 1 },
80a89d977cSHeikki Krogerus 	{ },
81a89d977cSHeikki Krogerus };
82a89d977cSHeikki Krogerus 
835741022cSHans de Goede static struct gpiod_lookup_table platform_bytcr_gpios = {
845741022cSHans de Goede 	.dev_id		= "0000:00:16.0",
855741022cSHans de Goede 	.table		= {
865741022cSHans de Goede 		GPIO_LOOKUP("INT33FC:00", 54, "reset", GPIO_ACTIVE_HIGH),
875741022cSHans de Goede 		GPIO_LOOKUP("INT33FC:02", 14, "cs", GPIO_ACTIVE_HIGH),
885741022cSHans de Goede 		{}
895741022cSHans de Goede 	},
905741022cSHans de Goede };
915741022cSHans de Goede 
927740d04dSHans de Goede static int dwc3_byt_enable_ulpi_refclock(struct pci_dev *pci)
937740d04dSHans de Goede {
947740d04dSHans de Goede 	void __iomem	*reg;
957740d04dSHans de Goede 	u32		value;
967740d04dSHans de Goede 
977740d04dSHans de Goede 	reg = pcim_iomap(pci, GP_RWBAR, 0);
98b497fff6SWei Yongjun 	if (!reg)
99b497fff6SWei Yongjun 		return -ENOMEM;
1007740d04dSHans de Goede 
1017740d04dSHans de Goede 	value = readl(reg + GP_RWREG1);
1027740d04dSHans de Goede 	if (!(value & GP_RWREG1_ULPI_REFCLK_DISABLE))
1037740d04dSHans de Goede 		goto unmap; /* ULPI refclk already enabled */
1047740d04dSHans de Goede 
1057740d04dSHans de Goede 	value &= ~GP_RWREG1_ULPI_REFCLK_DISABLE;
1067740d04dSHans de Goede 	writel(value, reg + GP_RWREG1);
1077740d04dSHans de Goede 	/* This comes from the Intel Android x86 tree w/o any explanation */
1087740d04dSHans de Goede 	msleep(100);
1097740d04dSHans de Goede unmap:
1107740d04dSHans de Goede 	pcim_iounmap(pci, reg);
1117740d04dSHans de Goede 	return 0;
1127740d04dSHans de Goede }
1137740d04dSHans de Goede 
1141a7b12f6SAndy Shevchenko static const struct property_entry dwc3_pci_intel_properties[] = {
1151a7b12f6SAndy Shevchenko 	PROPERTY_ENTRY_STRING("dr_mode", "peripheral"),
1161a7b12f6SAndy Shevchenko 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
1171a7b12f6SAndy Shevchenko 	{}
1181a7b12f6SAndy Shevchenko };
1190f817ae6SFelipe Balbi 
120c31d983bSAndy Shevchenko static const struct property_entry dwc3_pci_mrfld_properties[] = {
121c31d983bSAndy Shevchenko 	PROPERTY_ENTRY_STRING("dr_mode", "otg"),
122066c0959SAndy Shevchenko 	PROPERTY_ENTRY_STRING("linux,extcon-name", "mrfld_bcove_pwrsrc"),
123c31d983bSAndy Shevchenko 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
124c31d983bSAndy Shevchenko 	{}
125c31d983bSAndy Shevchenko };
126c31d983bSAndy Shevchenko 
1271a7b12f6SAndy Shevchenko static const struct property_entry dwc3_pci_amd_properties[] = {
128cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,has-lpm-erratum"),
129cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_U8("snps,lpm-nyet-threshold", 0xf),
130cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,u2exit_lfps_quirk"),
131cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,u2ss_inp3_quirk"),
132cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,req_p1p2p3_quirk"),
133cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,del_p1p2p3_quirk"),
134cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,del_phy_power_chg_quirk"),
135cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,lfps_filter_quirk"),
136cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,rx_detect_poll_quirk"),
137cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,tx_de_emphasis_quirk"),
138cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_U8("snps,tx_de_emphasis", 1),
1391a7b12f6SAndy Shevchenko 	/* FIXME these quirks should be removed when AMD NL tapes out */
140cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,disable_scramble_quirk"),
141cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,dis_u3_susphy_quirk"),
142cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
1430eae2fdeSJohn Youn 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
144e6fe66feSFelipe Balbi 	{}
145e6fe66feSFelipe Balbi };
146e6fe66feSFelipe Balbi 
147e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_intel_swnode = {
148e492ce9bSHeikki Krogerus 	.properties = dwc3_pci_intel_properties,
149e492ce9bSHeikki Krogerus };
150e492ce9bSHeikki Krogerus 
151e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_intel_mrfld_swnode = {
152e492ce9bSHeikki Krogerus 	.properties = dwc3_pci_mrfld_properties,
153e492ce9bSHeikki Krogerus };
154e492ce9bSHeikki Krogerus 
155e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_amd_swnode = {
156e492ce9bSHeikki Krogerus 	.properties = dwc3_pci_amd_properties,
157e492ce9bSHeikki Krogerus };
158e492ce9bSHeikki Krogerus 
1591a7b12f6SAndy Shevchenko static int dwc3_pci_quirks(struct dwc3_pci *dwc)
1601a7b12f6SAndy Shevchenko {
1611a7b12f6SAndy Shevchenko 	struct pci_dev			*pdev = dwc->pci;
162e6fe66feSFelipe Balbi 
1631a7b12f6SAndy Shevchenko 	if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
1649cecca75SFelipe Balbi 		if (pdev->device == PCI_DEVICE_ID_INTEL_BXT ||
165a609ce2aSRaymond Tan 		    pdev->device == PCI_DEVICE_ID_INTEL_BXT_M ||
166a609ce2aSRaymond Tan 		    pdev->device == PCI_DEVICE_ID_INTEL_EHLLP) {
16794116f81SAndy Shevchenko 			guid_parse(PCI_INTEL_BXT_DSM_GUID, &dwc->guid);
1689cecca75SFelipe Balbi 			dwc->has_dsm_for_pm = true;
1699cecca75SFelipe Balbi 		}
1709cecca75SFelipe Balbi 
171e6fe66feSFelipe Balbi 		if (pdev->device == PCI_DEVICE_ID_INTEL_BYT) {
172a89d977cSHeikki Krogerus 			struct gpio_desc *gpio;
1731a7b12f6SAndy Shevchenko 			int ret;
174a89d977cSHeikki Krogerus 
1757740d04dSHans de Goede 			/* On BYT the FW does not always enable the refclock */
1767740d04dSHans de Goede 			ret = dwc3_byt_enable_ulpi_refclock(pdev);
1777740d04dSHans de Goede 			if (ret)
1787740d04dSHans de Goede 				return ret;
1797740d04dSHans de Goede 
1804a56e413SAndy Shevchenko 			ret = devm_acpi_dev_add_driver_gpios(&pdev->dev,
181a89d977cSHeikki Krogerus 					acpi_dwc3_byt_gpios);
1824a56e413SAndy Shevchenko 			if (ret)
1834a56e413SAndy Shevchenko 				dev_dbg(&pdev->dev, "failed to add mapping table\n");
184a89d977cSHeikki Krogerus 
1852df033caSUwe Kleine-König 			/*
1865741022cSHans de Goede 			 * A lot of BYT devices lack ACPI resource entries for
1875741022cSHans de Goede 			 * the GPIOs, add a fallback mapping to the reference
1885741022cSHans de Goede 			 * design GPIOs which all boards seem to use.
1895741022cSHans de Goede 			 */
1905741022cSHans de Goede 			gpiod_add_lookup_table(&platform_bytcr_gpios);
1915741022cSHans de Goede 
1925741022cSHans de Goede 			/*
1932df033caSUwe Kleine-König 			 * These GPIOs will turn on the USB2 PHY. Note that we have to
1942df033caSUwe Kleine-König 			 * put the gpio descriptors again here because the phy driver
1952df033caSUwe Kleine-König 			 * might want to grab them, too.
1962df033caSUwe Kleine-König 			 */
1973004cfd6SStephan Gerhold 			gpio = gpiod_get_optional(&pdev->dev, "cs", GPIOD_OUT_LOW);
1982df033caSUwe Kleine-König 			if (IS_ERR(gpio))
1992df033caSUwe Kleine-König 				return PTR_ERR(gpio);
2002df033caSUwe Kleine-König 
201a89d977cSHeikki Krogerus 			gpiod_set_value_cansleep(gpio, 1);
2023004cfd6SStephan Gerhold 			gpiod_put(gpio);
203a89d977cSHeikki Krogerus 
2043004cfd6SStephan Gerhold 			gpio = gpiod_get_optional(&pdev->dev, "reset", GPIOD_OUT_LOW);
2052df033caSUwe Kleine-König 			if (IS_ERR(gpio))
2062df033caSUwe Kleine-König 				return PTR_ERR(gpio);
2072df033caSUwe Kleine-König 
2082df033caSUwe Kleine-König 			if (gpio) {
209a89d977cSHeikki Krogerus 				gpiod_set_value_cansleep(gpio, 1);
2103004cfd6SStephan Gerhold 				gpiod_put(gpio);
211a89d977cSHeikki Krogerus 				usleep_range(10000, 11000);
212a89d977cSHeikki Krogerus 			}
213a89d977cSHeikki Krogerus 		}
214e6fe66feSFelipe Balbi 	}
215a89d977cSHeikki Krogerus 
2162cd9ddf7SHeikki Krogerus 	return 0;
2172cd9ddf7SHeikki Krogerus }
21872246da4SFelipe Balbi 
2198eed00b2SManu Gautam #ifdef CONFIG_PM
2208eed00b2SManu Gautam static void dwc3_pci_resume_work(struct work_struct *work)
2218eed00b2SManu Gautam {
2228eed00b2SManu Gautam 	struct dwc3_pci *dwc = container_of(work, struct dwc3_pci, wakeup_work);
2238eed00b2SManu Gautam 	struct platform_device *dwc3 = dwc->dwc3;
2248eed00b2SManu Gautam 	int ret;
2258eed00b2SManu Gautam 
2268eed00b2SManu Gautam 	ret = pm_runtime_get_sync(&dwc3->dev);
2272655971aSAditya Pakki 	if (ret) {
2282655971aSAditya Pakki 		pm_runtime_put_sync_autosuspend(&dwc3->dev);
2298eed00b2SManu Gautam 		return;
2302655971aSAditya Pakki 	}
2318eed00b2SManu Gautam 
2328eed00b2SManu Gautam 	pm_runtime_mark_last_busy(&dwc3->dev);
2338eed00b2SManu Gautam 	pm_runtime_put_sync_autosuspend(&dwc3->dev);
2348eed00b2SManu Gautam }
2358eed00b2SManu Gautam #endif
2368eed00b2SManu Gautam 
2371a7b12f6SAndy Shevchenko static int dwc3_pci_probe(struct pci_dev *pci, const struct pci_device_id *id)
23872246da4SFelipe Balbi {
2390f817ae6SFelipe Balbi 	struct dwc3_pci		*dwc;
24072246da4SFelipe Balbi 	struct resource		res[2];
241b09e99eeSAndy Shevchenko 	int			ret;
242802ca850SChanho Park 	struct device		*dev = &pci->dev;
24372246da4SFelipe Balbi 
244f1c7e710SAndy Shevchenko 	ret = pcim_enable_device(pci);
24572246da4SFelipe Balbi 	if (ret) {
246802ca850SChanho Park 		dev_err(dev, "failed to enable pci device\n");
247802ca850SChanho Park 		return -ENODEV;
24872246da4SFelipe Balbi 	}
24972246da4SFelipe Balbi 
25072246da4SFelipe Balbi 	pci_set_master(pci);
25172246da4SFelipe Balbi 
2520f817ae6SFelipe Balbi 	dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
2530f817ae6SFelipe Balbi 	if (!dwc)
254f1c7e710SAndy Shevchenko 		return -ENOMEM;
2550f817ae6SFelipe Balbi 
2560f817ae6SFelipe Balbi 	dwc->dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO);
2570f817ae6SFelipe Balbi 	if (!dwc->dwc3)
2580f817ae6SFelipe Balbi 		return -ENOMEM;
25972246da4SFelipe Balbi 
26072246da4SFelipe Balbi 	memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
26172246da4SFelipe Balbi 
26272246da4SFelipe Balbi 	res[0].start	= pci_resource_start(pci, 0);
26372246da4SFelipe Balbi 	res[0].end	= pci_resource_end(pci, 0);
26472246da4SFelipe Balbi 	res[0].name	= "dwc_usb3";
26572246da4SFelipe Balbi 	res[0].flags	= IORESOURCE_MEM;
26672246da4SFelipe Balbi 
26772246da4SFelipe Balbi 	res[1].start	= pci->irq;
26872246da4SFelipe Balbi 	res[1].name	= "dwc_usb3";
26972246da4SFelipe Balbi 	res[1].flags	= IORESOURCE_IRQ;
27072246da4SFelipe Balbi 
2710f817ae6SFelipe Balbi 	ret = platform_device_add_resources(dwc->dwc3, res, ARRAY_SIZE(res));
27272246da4SFelipe Balbi 	if (ret) {
273802ca850SChanho Park 		dev_err(dev, "couldn't add resources to dwc3 device\n");
274cabdf83dSThinh Nguyen 		goto err;
27572246da4SFelipe Balbi 	}
27672246da4SFelipe Balbi 
2770f817ae6SFelipe Balbi 	dwc->pci = pci;
2780f817ae6SFelipe Balbi 	dwc->dwc3->dev.parent = dev;
2790f817ae6SFelipe Balbi 	ACPI_COMPANION_SET(&dwc->dwc3->dev, ACPI_COMPANION(dev));
28072246da4SFelipe Balbi 
281e492ce9bSHeikki Krogerus 	ret = device_add_software_node(&dwc->dwc3->dev, (void *)id->driver_data);
2821a7b12f6SAndy Shevchenko 	if (ret < 0)
2839bbfceeaSNavid Emamdoost 		goto err;
2841a7b12f6SAndy Shevchenko 
2850f817ae6SFelipe Balbi 	ret = dwc3_pci_quirks(dwc);
286474799f0SHeikki Krogerus 	if (ret)
287474799f0SHeikki Krogerus 		goto err;
288474799f0SHeikki Krogerus 
2890f817ae6SFelipe Balbi 	ret = platform_device_add(dwc->dwc3);
29072246da4SFelipe Balbi 	if (ret) {
291802ca850SChanho Park 		dev_err(dev, "failed to register dwc3 device\n");
2922cd9ddf7SHeikki Krogerus 		goto err;
29372246da4SFelipe Balbi 	}
29472246da4SFelipe Balbi 
295e9af9229SFelipe Balbi 	device_init_wakeup(dev, true);
2960f817ae6SFelipe Balbi 	pci_set_drvdata(pci, dwc);
297e9af9229SFelipe Balbi 	pm_runtime_put(dev);
2988eed00b2SManu Gautam #ifdef CONFIG_PM
2998eed00b2SManu Gautam 	INIT_WORK(&dwc->wakeup_work, dwc3_pci_resume_work);
3008eed00b2SManu Gautam #endif
301e9af9229SFelipe Balbi 
30272246da4SFelipe Balbi 	return 0;
3032cd9ddf7SHeikki Krogerus err:
304e492ce9bSHeikki Krogerus 	device_remove_software_node(&dwc->dwc3->dev);
3050f817ae6SFelipe Balbi 	platform_device_put(dwc->dwc3);
30672246da4SFelipe Balbi 	return ret;
30772246da4SFelipe Balbi }
30872246da4SFelipe Balbi 
309fb4e98abSBill Pemberton static void dwc3_pci_remove(struct pci_dev *pci)
31072246da4SFelipe Balbi {
3110f817ae6SFelipe Balbi 	struct dwc3_pci		*dwc = pci_get_drvdata(pci);
3127b412b04SKuppuswamy Sathyanarayanan 	struct pci_dev		*pdev = dwc->pci;
3130f817ae6SFelipe Balbi 
3147b412b04SKuppuswamy Sathyanarayanan 	if (pdev->device == PCI_DEVICE_ID_INTEL_BYT)
3155741022cSHans de Goede 		gpiod_remove_lookup_table(&platform_bytcr_gpios);
3168eed00b2SManu Gautam #ifdef CONFIG_PM
3178eed00b2SManu Gautam 	cancel_work_sync(&dwc->wakeup_work);
3188eed00b2SManu Gautam #endif
319e9af9229SFelipe Balbi 	device_init_wakeup(&pci->dev, false);
320e9af9229SFelipe Balbi 	pm_runtime_get(&pci->dev);
321e492ce9bSHeikki Krogerus 	device_remove_software_node(&dwc->dwc3->dev);
3220f817ae6SFelipe Balbi 	platform_device_unregister(dwc->dwc3);
32372246da4SFelipe Balbi }
32472246da4SFelipe Balbi 
325782df20cSJingoo Han static const struct pci_device_id dwc3_pci_id_table[] = {
3261a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BSW),
327e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3281a7b12f6SAndy Shevchenko 
3291a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BYT),
330e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3311a7b12f6SAndy Shevchenko 
3321a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MRFLD),
333e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_mrfld_swnode, },
3341a7b12f6SAndy Shevchenko 
3353c3caae4SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CMLLP),
336e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3373c3caae4SHeikki Krogerus 
3387ae622c9SFelipe Balbi 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CMLH),
339e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3407ae622c9SFelipe Balbi 
3411a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SPTLP),
342e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3431a7b12f6SAndy Shevchenko 
3441a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SPTH),
345e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3461a7b12f6SAndy Shevchenko 
3471a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BXT),
348e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3491a7b12f6SAndy Shevchenko 
3501a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BXT_M),
351e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3521a7b12f6SAndy Shevchenko 
3531a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_APL),
354e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3551a7b12f6SAndy Shevchenko 
3561a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_KBP),
357e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3581a7b12f6SAndy Shevchenko 
3591a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_GLK),
360e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3611a7b12f6SAndy Shevchenko 
3621a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CNPLP),
363e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3641a7b12f6SAndy Shevchenko 
3651a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CNPH),
366e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3671a7b12f6SAndy Shevchenko 
368f5ae8869SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CNPV),
369e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
370f5ae8869SHeikki Krogerus 
3711a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICLLP),
372e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3731a7b12f6SAndy Shevchenko 
374dbb0569dSFelipe Balbi 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_EHLLP),
375e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode },
376dbb0569dSFelipe Balbi 
377b3649deeSFelipe Balbi 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGPLP),
378e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
379b3649deeSFelipe Balbi 
380c3f595a8SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGPH),
381e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
382c3f595a8SHeikki Krogerus 
383e25d1e85SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_JSP),
384e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
385e25d1e85SHeikki Krogerus 
386f08fc2c3SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADLP),
387f08fc2c3SHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
388f08fc2c3SHeikki Krogerus 
3891384ab4fSHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADLS),
390e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3911384ab4fSHeikki Krogerus 
39273203bdeSHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL),
39373203bdeSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
39473203bdeSHeikki Krogerus 
3951a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_NL_USB),
396e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_amd_swnode, },
39772246da4SFelipe Balbi 	{  }	/* Terminating Entry */
39872246da4SFelipe Balbi };
39972246da4SFelipe Balbi MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
40072246da4SFelipe Balbi 
40136daf3aaSFelipe Balbi #if defined(CONFIG_PM) || defined(CONFIG_PM_SLEEP)
40236daf3aaSFelipe Balbi static int dwc3_pci_dsm(struct dwc3_pci *dwc, int param)
40336daf3aaSFelipe Balbi {
40436daf3aaSFelipe Balbi 	union acpi_object *obj;
40536daf3aaSFelipe Balbi 	union acpi_object tmp;
40636daf3aaSFelipe Balbi 	union acpi_object argv4 = ACPI_INIT_DSM_ARGV4(1, &tmp);
40736daf3aaSFelipe Balbi 
40836daf3aaSFelipe Balbi 	if (!dwc->has_dsm_for_pm)
40936daf3aaSFelipe Balbi 		return 0;
41036daf3aaSFelipe Balbi 
41136daf3aaSFelipe Balbi 	tmp.type = ACPI_TYPE_INTEGER;
41236daf3aaSFelipe Balbi 	tmp.integer.value = param;
41336daf3aaSFelipe Balbi 
41494116f81SAndy Shevchenko 	obj = acpi_evaluate_dsm(ACPI_HANDLE(&dwc->pci->dev), &dwc->guid,
41536daf3aaSFelipe Balbi 			1, PCI_INTEL_BXT_FUNC_PMU_PWR, &argv4);
41636daf3aaSFelipe Balbi 	if (!obj) {
41736daf3aaSFelipe Balbi 		dev_err(&dwc->pci->dev, "failed to evaluate _DSM\n");
41836daf3aaSFelipe Balbi 		return -EIO;
41936daf3aaSFelipe Balbi 	}
42036daf3aaSFelipe Balbi 
42136daf3aaSFelipe Balbi 	ACPI_FREE(obj);
42236daf3aaSFelipe Balbi 
42336daf3aaSFelipe Balbi 	return 0;
42436daf3aaSFelipe Balbi }
42536daf3aaSFelipe Balbi #endif /* CONFIG_PM || CONFIG_PM_SLEEP */
42636daf3aaSFelipe Balbi 
427e9af9229SFelipe Balbi #ifdef CONFIG_PM
428e9af9229SFelipe Balbi static int dwc3_pci_runtime_suspend(struct device *dev)
429e9af9229SFelipe Balbi {
4309cecca75SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
4319cecca75SFelipe Balbi 
432de3ef1ebSRafael J. Wysocki 	if (device_can_wakeup(dev))
4339cecca75SFelipe Balbi 		return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D3);
434e9af9229SFelipe Balbi 
435e9af9229SFelipe Balbi 	return -EBUSY;
436e9af9229SFelipe Balbi }
437e9af9229SFelipe Balbi 
438f6c274e1SFelipe Balbi static int dwc3_pci_runtime_resume(struct device *dev)
439f6c274e1SFelipe Balbi {
4400f817ae6SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
4419cecca75SFelipe Balbi 	int			ret;
4429cecca75SFelipe Balbi 
4439cecca75SFelipe Balbi 	ret = dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D0);
4449cecca75SFelipe Balbi 	if (ret)
4459cecca75SFelipe Balbi 		return ret;
446f6c274e1SFelipe Balbi 
4478eed00b2SManu Gautam 	queue_work(pm_wq, &dwc->wakeup_work);
4488eed00b2SManu Gautam 
4498eed00b2SManu Gautam 	return 0;
450f6c274e1SFelipe Balbi }
451696118c0SFelipe Balbi #endif /* CONFIG_PM */
452f6c274e1SFelipe Balbi 
453696118c0SFelipe Balbi #ifdef CONFIG_PM_SLEEP
4549cecca75SFelipe Balbi static int dwc3_pci_suspend(struct device *dev)
455e9af9229SFelipe Balbi {
4569cecca75SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
4579cecca75SFelipe Balbi 
4589cecca75SFelipe Balbi 	return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D3);
4599cecca75SFelipe Balbi }
4609cecca75SFelipe Balbi 
4619cecca75SFelipe Balbi static int dwc3_pci_resume(struct device *dev)
4629cecca75SFelipe Balbi {
4639cecca75SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
4649cecca75SFelipe Balbi 
4659cecca75SFelipe Balbi 	return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D0);
466e9af9229SFelipe Balbi }
467696118c0SFelipe Balbi #endif /* CONFIG_PM_SLEEP */
468e9af9229SFelipe Balbi 
46995aa932cSDoug Wilson static const struct dev_pm_ops dwc3_pci_dev_pm_ops = {
4709cecca75SFelipe Balbi 	SET_SYSTEM_SLEEP_PM_OPS(dwc3_pci_suspend, dwc3_pci_resume)
471f6c274e1SFelipe Balbi 	SET_RUNTIME_PM_OPS(dwc3_pci_runtime_suspend, dwc3_pci_runtime_resume,
472e9af9229SFelipe Balbi 		NULL)
473e9af9229SFelipe Balbi };
474e9af9229SFelipe Balbi 
47572246da4SFelipe Balbi static struct pci_driver dwc3_pci_driver = {
4760949e99bSFelipe Balbi 	.name		= "dwc3-pci",
47772246da4SFelipe Balbi 	.id_table	= dwc3_pci_id_table,
47872246da4SFelipe Balbi 	.probe		= dwc3_pci_probe,
4797690417dSBill Pemberton 	.remove		= dwc3_pci_remove,
480e9af9229SFelipe Balbi 	.driver		= {
481e9af9229SFelipe Balbi 		.pm	= &dwc3_pci_dev_pm_ops,
482e9af9229SFelipe Balbi 	}
48372246da4SFelipe Balbi };
48472246da4SFelipe Balbi 
48572246da4SFelipe Balbi MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
4865945f789SFelipe Balbi MODULE_LICENSE("GPL v2");
48772246da4SFelipe Balbi MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
48872246da4SFelipe Balbi 
48995656336SGreg Kroah-Hartman module_pci_driver(dwc3_pci_driver);
490