xref: /linux/drivers/usb/dwc3/dwc3-pci.c (revision b32b8f2b9542d8039f5468303a6ca78c1b5611a5)
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
4393440d1fSShruthi Sanil #define PCI_DEVICE_ID_INTEL_ADL			0x460e
44ff2d2beeSShruthi Sanil #define PCI_DEVICE_ID_INTEL_ADL_PCH		0x51ee
45ff2d2beeSShruthi Sanil #define PCI_DEVICE_ID_INTEL_ADLN		0x465e
46ff2d2beeSShruthi Sanil #define PCI_DEVICE_ID_INTEL_ADLN_PCH		0x54ee
471384ab4fSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_ADLS		0x7ae1
48f05f80f2SShruthi Sanil #define PCI_DEVICE_ID_INTEL_RPL			0xa70e
49038438a2SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_RPLS		0x7a61
508e5248c3SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_MTLM		0x7eb1
51973e0f7aSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_MTLP		0x7ec1
52ec799c8aSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_MTLS		0x7f6f
53973e0f7aSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_MTL			0x7e7e
5473203bdeSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_TGL			0x9a15
551abade64SNehal Bakulchandra Shah #define PCI_DEVICE_ID_AMD_MR			0x163a
5672246da4SFelipe Balbi 
5794116f81SAndy Shevchenko #define PCI_INTEL_BXT_DSM_GUID		"732b85d5-b7a7-4a1b-9ba0-4bbd00ffd511"
589cecca75SFelipe Balbi #define PCI_INTEL_BXT_FUNC_PMU_PWR	4
599cecca75SFelipe Balbi #define PCI_INTEL_BXT_STATE_D0		0
609cecca75SFelipe Balbi #define PCI_INTEL_BXT_STATE_D3		3
619cecca75SFelipe Balbi 
627740d04dSHans de Goede #define GP_RWBAR			1
637740d04dSHans de Goede #define GP_RWREG1			0xa0
647740d04dSHans de Goede #define GP_RWREG1_ULPI_REFCLK_DISABLE	(1 << 17)
657740d04dSHans de Goede 
660f817ae6SFelipe Balbi /**
670f817ae6SFelipe Balbi  * struct dwc3_pci - Driver private structure
680f817ae6SFelipe Balbi  * @dwc3: child dwc3 platform_device
690f817ae6SFelipe Balbi  * @pci: our link to PCI bus
7094116f81SAndy Shevchenko  * @guid: _DSM GUID
719cecca75SFelipe Balbi  * @has_dsm_for_pm: true for devices which need to run _DSM on runtime PM
7287d852deSAndy Shevchenko  * @wakeup_work: work for asynchronous resume
730f817ae6SFelipe Balbi  */
740f817ae6SFelipe Balbi struct dwc3_pci {
750f817ae6SFelipe Balbi 	struct platform_device *dwc3;
760f817ae6SFelipe Balbi 	struct pci_dev *pci;
779cecca75SFelipe Balbi 
7894116f81SAndy Shevchenko 	guid_t guid;
799cecca75SFelipe Balbi 
809cecca75SFelipe Balbi 	unsigned int has_dsm_for_pm:1;
818eed00b2SManu Gautam 	struct work_struct wakeup_work;
820f817ae6SFelipe Balbi };
830f817ae6SFelipe Balbi 
84a89d977cSHeikki Krogerus static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
85a89d977cSHeikki Krogerus static const struct acpi_gpio_params cs_gpios = { 1, 0, false };
86a89d977cSHeikki Krogerus 
87a89d977cSHeikki Krogerus static const struct acpi_gpio_mapping acpi_dwc3_byt_gpios[] = {
88a89d977cSHeikki Krogerus 	{ "reset-gpios", &reset_gpios, 1 },
89a89d977cSHeikki Krogerus 	{ "cs-gpios", &cs_gpios, 1 },
90a89d977cSHeikki Krogerus 	{ },
91a89d977cSHeikki Krogerus };
92a89d977cSHeikki Krogerus 
935741022cSHans de Goede static struct gpiod_lookup_table platform_bytcr_gpios = {
945741022cSHans de Goede 	.dev_id		= "0000:00:16.0",
955741022cSHans de Goede 	.table		= {
9662e3f0afSHans de Goede 		GPIO_LOOKUP("INT33FC:00", 54, "cs", GPIO_ACTIVE_HIGH),
9762e3f0afSHans de Goede 		GPIO_LOOKUP("INT33FC:02", 14, "reset", GPIO_ACTIVE_HIGH),
985741022cSHans de Goede 		{}
995741022cSHans de Goede 	},
1005741022cSHans de Goede };
1015741022cSHans de Goede 
1027740d04dSHans de Goede static int dwc3_byt_enable_ulpi_refclock(struct pci_dev *pci)
1037740d04dSHans de Goede {
1047740d04dSHans de Goede 	void __iomem	*reg;
1057740d04dSHans de Goede 	u32		value;
1067740d04dSHans de Goede 
1077740d04dSHans de Goede 	reg = pcim_iomap(pci, GP_RWBAR, 0);
108b497fff6SWei Yongjun 	if (!reg)
109b497fff6SWei Yongjun 		return -ENOMEM;
1107740d04dSHans de Goede 
1117740d04dSHans de Goede 	value = readl(reg + GP_RWREG1);
1127740d04dSHans de Goede 	if (!(value & GP_RWREG1_ULPI_REFCLK_DISABLE))
1137740d04dSHans de Goede 		goto unmap; /* ULPI refclk already enabled */
1147740d04dSHans de Goede 
1157740d04dSHans de Goede 	value &= ~GP_RWREG1_ULPI_REFCLK_DISABLE;
1167740d04dSHans de Goede 	writel(value, reg + GP_RWREG1);
1177740d04dSHans de Goede 	/* This comes from the Intel Android x86 tree w/o any explanation */
1187740d04dSHans de Goede 	msleep(100);
1197740d04dSHans de Goede unmap:
1207740d04dSHans de Goede 	pcim_iounmap(pci, reg);
1217740d04dSHans de Goede 	return 0;
1227740d04dSHans de Goede }
1237740d04dSHans de Goede 
1241a7b12f6SAndy Shevchenko static const struct property_entry dwc3_pci_intel_properties[] = {
1251a7b12f6SAndy Shevchenko 	PROPERTY_ENTRY_STRING("dr_mode", "peripheral"),
1261a7b12f6SAndy Shevchenko 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
1271a7b12f6SAndy Shevchenko 	{}
1281a7b12f6SAndy Shevchenko };
1290f817ae6SFelipe Balbi 
130582ab24eSHans de Goede static const struct property_entry dwc3_pci_intel_phy_charger_detect_properties[] = {
131582ab24eSHans de Goede 	PROPERTY_ENTRY_STRING("dr_mode", "peripheral"),
132582ab24eSHans de Goede 	PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
133582ab24eSHans de Goede 	PROPERTY_ENTRY_BOOL("linux,phy_charger_detect"),
1347ddda261SStephan Gerhold 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
135ca9400efSGreg Kroah-Hartman 	{}
136ca9400efSGreg Kroah-Hartman };
137ca9400efSGreg Kroah-Hartman 
138d7c93a90SHans de Goede static const struct property_entry dwc3_pci_intel_byt_properties[] = {
139d7c93a90SHans de Goede 	PROPERTY_ENTRY_STRING("dr_mode", "peripheral"),
140d7c93a90SHans de Goede 	PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
141582ab24eSHans de Goede 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
142582ab24eSHans de Goede 	{}
143582ab24eSHans de Goede };
144582ab24eSHans de Goede 
145c31d983bSAndy Shevchenko static const struct property_entry dwc3_pci_mrfld_properties[] = {
146c31d983bSAndy Shevchenko 	PROPERTY_ENTRY_STRING("dr_mode", "otg"),
147066c0959SAndy Shevchenko 	PROPERTY_ENTRY_STRING("linux,extcon-name", "mrfld_bcove_pwrsrc"),
148b522f830SAndy Shevchenko 	PROPERTY_ENTRY_BOOL("snps,dis_u3_susphy_quirk"),
149b522f830SAndy Shevchenko 	PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
15004357fafSFerry Toth 	PROPERTY_ENTRY_BOOL("snps,usb2-gadget-lpm-disable"),
151c31d983bSAndy Shevchenko 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
152c31d983bSAndy Shevchenko 	{}
153c31d983bSAndy Shevchenko };
154c31d983bSAndy Shevchenko 
1551a7b12f6SAndy Shevchenko static const struct property_entry dwc3_pci_amd_properties[] = {
156cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,has-lpm-erratum"),
157cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_U8("snps,lpm-nyet-threshold", 0xf),
158cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,u2exit_lfps_quirk"),
159cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,u2ss_inp3_quirk"),
160cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,req_p1p2p3_quirk"),
161cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,del_p1p2p3_quirk"),
162cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,del_phy_power_chg_quirk"),
163cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,lfps_filter_quirk"),
164cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,rx_detect_poll_quirk"),
165cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,tx_de_emphasis_quirk"),
166cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_U8("snps,tx_de_emphasis", 1),
1671a7b12f6SAndy Shevchenko 	/* FIXME these quirks should be removed when AMD NL tapes out */
168cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,disable_scramble_quirk"),
169cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,dis_u3_susphy_quirk"),
170cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
1710eae2fdeSJohn Youn 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
172e6fe66feSFelipe Balbi 	{}
173e6fe66feSFelipe Balbi };
174e6fe66feSFelipe Balbi 
1751abade64SNehal Bakulchandra Shah static const struct property_entry dwc3_pci_mr_properties[] = {
1761abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_STRING("dr_mode", "otg"),
1771abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_BOOL("usb-role-switch"),
1781abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_STRING("role-switch-default-mode", "host"),
1791abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
1801abade64SNehal Bakulchandra Shah 	{}
1811abade64SNehal Bakulchandra Shah };
1821abade64SNehal Bakulchandra Shah 
183e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_intel_swnode = {
184e492ce9bSHeikki Krogerus 	.properties = dwc3_pci_intel_properties,
185e492ce9bSHeikki Krogerus };
186e492ce9bSHeikki Krogerus 
187582ab24eSHans de Goede static const struct software_node dwc3_pci_intel_phy_charger_detect_swnode = {
188582ab24eSHans de Goede 	.properties = dwc3_pci_intel_phy_charger_detect_properties,
189582ab24eSHans de Goede };
190582ab24eSHans de Goede 
191d7c93a90SHans de Goede static const struct software_node dwc3_pci_intel_byt_swnode = {
192d7c93a90SHans de Goede 	.properties = dwc3_pci_intel_byt_properties,
193d7c93a90SHans de Goede };
194d7c93a90SHans de Goede 
195e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_intel_mrfld_swnode = {
196e492ce9bSHeikki Krogerus 	.properties = dwc3_pci_mrfld_properties,
197e492ce9bSHeikki Krogerus };
198e492ce9bSHeikki Krogerus 
199e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_amd_swnode = {
200e492ce9bSHeikki Krogerus 	.properties = dwc3_pci_amd_properties,
201e492ce9bSHeikki Krogerus };
202e492ce9bSHeikki Krogerus 
2031abade64SNehal Bakulchandra Shah static const struct software_node dwc3_pci_amd_mr_swnode = {
2041abade64SNehal Bakulchandra Shah 	.properties = dwc3_pci_mr_properties,
2051abade64SNehal Bakulchandra Shah };
2061abade64SNehal Bakulchandra Shah 
207e285cb40SHans de Goede static int dwc3_pci_quirks(struct dwc3_pci *dwc,
208e285cb40SHans de Goede 			   const struct software_node *swnode)
2091a7b12f6SAndy Shevchenko {
2101a7b12f6SAndy Shevchenko 	struct pci_dev			*pdev = dwc->pci;
211e6fe66feSFelipe Balbi 
2121a7b12f6SAndy Shevchenko 	if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
2139cecca75SFelipe Balbi 		if (pdev->device == PCI_DEVICE_ID_INTEL_BXT ||
214a609ce2aSRaymond Tan 		    pdev->device == PCI_DEVICE_ID_INTEL_BXT_M ||
215457d2285SRaymond Tan 		    pdev->device == PCI_DEVICE_ID_INTEL_EHL) {
21694116f81SAndy Shevchenko 			guid_parse(PCI_INTEL_BXT_DSM_GUID, &dwc->guid);
2179cecca75SFelipe Balbi 			dwc->has_dsm_for_pm = true;
2189cecca75SFelipe Balbi 		}
2199cecca75SFelipe Balbi 
220e6fe66feSFelipe Balbi 		if (pdev->device == PCI_DEVICE_ID_INTEL_BYT) {
221a89d977cSHeikki Krogerus 			struct gpio_desc *gpio;
2221a7b12f6SAndy Shevchenko 			int ret;
223a89d977cSHeikki Krogerus 
2247740d04dSHans de Goede 			/* On BYT the FW does not always enable the refclock */
2257740d04dSHans de Goede 			ret = dwc3_byt_enable_ulpi_refclock(pdev);
2267740d04dSHans de Goede 			if (ret)
2277740d04dSHans de Goede 				return ret;
2287740d04dSHans de Goede 
2294a56e413SAndy Shevchenko 			ret = devm_acpi_dev_add_driver_gpios(&pdev->dev,
230a89d977cSHeikki Krogerus 					acpi_dwc3_byt_gpios);
2314a56e413SAndy Shevchenko 			if (ret)
2324a56e413SAndy Shevchenko 				dev_dbg(&pdev->dev, "failed to add mapping table\n");
233a89d977cSHeikki Krogerus 
2342df033caSUwe Kleine-König 			/*
2355741022cSHans de Goede 			 * A lot of BYT devices lack ACPI resource entries for
236*b32b8f2bSGratian Crisan 			 * the GPIOs. If the ACPI entry for the GPIO controller
237*b32b8f2bSGratian Crisan 			 * is present add a fallback mapping to the reference
2385741022cSHans de Goede 			 * design GPIOs which all boards seem to use.
2395741022cSHans de Goede 			 */
240*b32b8f2bSGratian Crisan 			if (acpi_dev_present("INT33FC", NULL, -1))
2415741022cSHans de Goede 				gpiod_add_lookup_table(&platform_bytcr_gpios);
2425741022cSHans de Goede 
2435741022cSHans de Goede 			/*
2442df033caSUwe Kleine-König 			 * These GPIOs will turn on the USB2 PHY. Note that we have to
2452df033caSUwe Kleine-König 			 * put the gpio descriptors again here because the phy driver
2462df033caSUwe Kleine-König 			 * might want to grab them, too.
2472df033caSUwe Kleine-König 			 */
2483004cfd6SStephan Gerhold 			gpio = gpiod_get_optional(&pdev->dev, "cs", GPIOD_OUT_LOW);
2492df033caSUwe Kleine-König 			if (IS_ERR(gpio))
2502df033caSUwe Kleine-König 				return PTR_ERR(gpio);
2512df033caSUwe Kleine-König 
252a89d977cSHeikki Krogerus 			gpiod_set_value_cansleep(gpio, 1);
2533004cfd6SStephan Gerhold 			gpiod_put(gpio);
254a89d977cSHeikki Krogerus 
2553004cfd6SStephan Gerhold 			gpio = gpiod_get_optional(&pdev->dev, "reset", GPIOD_OUT_LOW);
2562df033caSUwe Kleine-König 			if (IS_ERR(gpio))
2572df033caSUwe Kleine-König 				return PTR_ERR(gpio);
2582df033caSUwe Kleine-König 
2592df033caSUwe Kleine-König 			if (gpio) {
260a89d977cSHeikki Krogerus 				gpiod_set_value_cansleep(gpio, 1);
2613004cfd6SStephan Gerhold 				gpiod_put(gpio);
262a89d977cSHeikki Krogerus 				usleep_range(10000, 11000);
263a89d977cSHeikki Krogerus 			}
264582ab24eSHans de Goede 
265582ab24eSHans de Goede 			/*
266a5d847b0SHans de Goede 			 * Make the pdev name predictable (only 1 DWC3 on BYT)
267a5d847b0SHans de Goede 			 * and patch the phy dev-name into the lookup table so
268a5d847b0SHans de Goede 			 * that the phy-driver can get the GPIOs.
269a5d847b0SHans de Goede 			 */
270a5d847b0SHans de Goede 			dwc->dwc3->id = PLATFORM_DEVID_NONE;
271a5d847b0SHans de Goede 			platform_bytcr_gpios.dev_id = "dwc3.ulpi";
272a5d847b0SHans de Goede 
273a5d847b0SHans de Goede 			/*
274582ab24eSHans de Goede 			 * Some Android tablets with a Crystal Cove PMIC
275582ab24eSHans de Goede 			 * (INT33FD), rely on the TUSB1211 phy for charger
276582ab24eSHans de Goede 			 * detection. These can be identified by them _not_
277582ab24eSHans de Goede 			 * using the standard ACPI battery and ac drivers.
278582ab24eSHans de Goede 			 */
279582ab24eSHans de Goede 			if (acpi_dev_present("INT33FD", "1", 2) &&
280582ab24eSHans de Goede 			    acpi_quirk_skip_acpi_ac_and_battery()) {
281582ab24eSHans de Goede 				dev_info(&pdev->dev, "Using TUSB1211 phy for charger detection\n");
282582ab24eSHans de Goede 				swnode = &dwc3_pci_intel_phy_charger_detect_swnode;
283582ab24eSHans de Goede 			}
284a89d977cSHeikki Krogerus 		}
285e6fe66feSFelipe Balbi 	}
286a89d977cSHeikki Krogerus 
287e285cb40SHans de Goede 	return device_add_software_node(&dwc->dwc3->dev, swnode);
2882cd9ddf7SHeikki Krogerus }
28972246da4SFelipe Balbi 
2908eed00b2SManu Gautam #ifdef CONFIG_PM
2918eed00b2SManu Gautam static void dwc3_pci_resume_work(struct work_struct *work)
2928eed00b2SManu Gautam {
2938eed00b2SManu Gautam 	struct dwc3_pci *dwc = container_of(work, struct dwc3_pci, wakeup_work);
2948eed00b2SManu Gautam 	struct platform_device *dwc3 = dwc->dwc3;
2958eed00b2SManu Gautam 	int ret;
2968eed00b2SManu Gautam 
2978eed00b2SManu Gautam 	ret = pm_runtime_get_sync(&dwc3->dev);
298a03e2ddaSZheng Yongjun 	if (ret < 0) {
2992655971aSAditya Pakki 		pm_runtime_put_sync_autosuspend(&dwc3->dev);
3008eed00b2SManu Gautam 		return;
3012655971aSAditya Pakki 	}
3028eed00b2SManu Gautam 
3038eed00b2SManu Gautam 	pm_runtime_mark_last_busy(&dwc3->dev);
3048eed00b2SManu Gautam 	pm_runtime_put_sync_autosuspend(&dwc3->dev);
3058eed00b2SManu Gautam }
3068eed00b2SManu Gautam #endif
3078eed00b2SManu Gautam 
3081a7b12f6SAndy Shevchenko static int dwc3_pci_probe(struct pci_dev *pci, const struct pci_device_id *id)
30972246da4SFelipe Balbi {
3100f817ae6SFelipe Balbi 	struct dwc3_pci		*dwc;
31172246da4SFelipe Balbi 	struct resource		res[2];
312b09e99eeSAndy Shevchenko 	int			ret;
313802ca850SChanho Park 	struct device		*dev = &pci->dev;
31472246da4SFelipe Balbi 
315f1c7e710SAndy Shevchenko 	ret = pcim_enable_device(pci);
31672246da4SFelipe Balbi 	if (ret) {
317802ca850SChanho Park 		dev_err(dev, "failed to enable pci device\n");
318802ca850SChanho Park 		return -ENODEV;
31972246da4SFelipe Balbi 	}
32072246da4SFelipe Balbi 
32172246da4SFelipe Balbi 	pci_set_master(pci);
32272246da4SFelipe Balbi 
3230f817ae6SFelipe Balbi 	dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
3240f817ae6SFelipe Balbi 	if (!dwc)
325f1c7e710SAndy Shevchenko 		return -ENOMEM;
3260f817ae6SFelipe Balbi 
3270f817ae6SFelipe Balbi 	dwc->dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO);
3280f817ae6SFelipe Balbi 	if (!dwc->dwc3)
3290f817ae6SFelipe Balbi 		return -ENOMEM;
33072246da4SFelipe Balbi 
33172246da4SFelipe Balbi 	memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
33272246da4SFelipe Balbi 
33372246da4SFelipe Balbi 	res[0].start	= pci_resource_start(pci, 0);
33472246da4SFelipe Balbi 	res[0].end	= pci_resource_end(pci, 0);
33572246da4SFelipe Balbi 	res[0].name	= "dwc_usb3";
33672246da4SFelipe Balbi 	res[0].flags	= IORESOURCE_MEM;
33772246da4SFelipe Balbi 
33872246da4SFelipe Balbi 	res[1].start	= pci->irq;
33972246da4SFelipe Balbi 	res[1].name	= "dwc_usb3";
34072246da4SFelipe Balbi 	res[1].flags	= IORESOURCE_IRQ;
34172246da4SFelipe Balbi 
3420f817ae6SFelipe Balbi 	ret = platform_device_add_resources(dwc->dwc3, res, ARRAY_SIZE(res));
34372246da4SFelipe Balbi 	if (ret) {
344802ca850SChanho Park 		dev_err(dev, "couldn't add resources to dwc3 device\n");
345cabdf83dSThinh Nguyen 		goto err;
34672246da4SFelipe Balbi 	}
34772246da4SFelipe Balbi 
3480f817ae6SFelipe Balbi 	dwc->pci = pci;
3490f817ae6SFelipe Balbi 	dwc->dwc3->dev.parent = dev;
3500f817ae6SFelipe Balbi 	ACPI_COMPANION_SET(&dwc->dwc3->dev, ACPI_COMPANION(dev));
35172246da4SFelipe Balbi 
352e285cb40SHans de Goede 	ret = dwc3_pci_quirks(dwc, (void *)id->driver_data);
353474799f0SHeikki Krogerus 	if (ret)
354474799f0SHeikki Krogerus 		goto err;
355474799f0SHeikki Krogerus 
3560f817ae6SFelipe Balbi 	ret = platform_device_add(dwc->dwc3);
35772246da4SFelipe Balbi 	if (ret) {
358802ca850SChanho Park 		dev_err(dev, "failed to register dwc3 device\n");
3592cd9ddf7SHeikki Krogerus 		goto err;
36072246da4SFelipe Balbi 	}
36172246da4SFelipe Balbi 
362e9af9229SFelipe Balbi 	device_init_wakeup(dev, true);
3630f817ae6SFelipe Balbi 	pci_set_drvdata(pci, dwc);
364e9af9229SFelipe Balbi 	pm_runtime_put(dev);
3658eed00b2SManu Gautam #ifdef CONFIG_PM
3668eed00b2SManu Gautam 	INIT_WORK(&dwc->wakeup_work, dwc3_pci_resume_work);
3678eed00b2SManu Gautam #endif
368e9af9229SFelipe Balbi 
36972246da4SFelipe Balbi 	return 0;
3702cd9ddf7SHeikki Krogerus err:
371e492ce9bSHeikki Krogerus 	device_remove_software_node(&dwc->dwc3->dev);
3720f817ae6SFelipe Balbi 	platform_device_put(dwc->dwc3);
37372246da4SFelipe Balbi 	return ret;
37472246da4SFelipe Balbi }
37572246da4SFelipe Balbi 
376fb4e98abSBill Pemberton static void dwc3_pci_remove(struct pci_dev *pci)
37772246da4SFelipe Balbi {
3780f817ae6SFelipe Balbi 	struct dwc3_pci		*dwc = pci_get_drvdata(pci);
3797b412b04SKuppuswamy Sathyanarayanan 	struct pci_dev		*pdev = dwc->pci;
3800f817ae6SFelipe Balbi 
3817b412b04SKuppuswamy Sathyanarayanan 	if (pdev->device == PCI_DEVICE_ID_INTEL_BYT)
3825741022cSHans de Goede 		gpiod_remove_lookup_table(&platform_bytcr_gpios);
3838eed00b2SManu Gautam #ifdef CONFIG_PM
3848eed00b2SManu Gautam 	cancel_work_sync(&dwc->wakeup_work);
3858eed00b2SManu Gautam #endif
386e9af9229SFelipe Balbi 	device_init_wakeup(&pci->dev, false);
387e9af9229SFelipe Balbi 	pm_runtime_get(&pci->dev);
388e492ce9bSHeikki Krogerus 	device_remove_software_node(&dwc->dwc3->dev);
3890f817ae6SFelipe Balbi 	platform_device_unregister(dwc->dwc3);
39072246da4SFelipe Balbi }
39172246da4SFelipe Balbi 
392782df20cSJingoo Han static const struct pci_device_id dwc3_pci_id_table[] = {
393917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, BSW, &dwc3_pci_intel_swnode) },
394917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, BYT, &dwc3_pci_intel_byt_swnode) },
395917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, MRFLD, &dwc3_pci_intel_mrfld_swnode) },
396917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, CMLLP, &dwc3_pci_intel_swnode) },
397917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, CMLH, &dwc3_pci_intel_swnode) },
398917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, SPTLP, &dwc3_pci_intel_swnode) },
399917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, SPTH, &dwc3_pci_intel_swnode) },
400917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, BXT, &dwc3_pci_intel_swnode) },
401917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, BXT_M, &dwc3_pci_intel_swnode) },
402917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, APL, &dwc3_pci_intel_swnode) },
403917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, KBP, &dwc3_pci_intel_swnode) },
404917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, GLK, &dwc3_pci_intel_swnode) },
405917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, CNPLP, &dwc3_pci_intel_swnode) },
406917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, CNPH, &dwc3_pci_intel_swnode) },
407917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, CNPV, &dwc3_pci_intel_swnode) },
408917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, ICLLP, &dwc3_pci_intel_swnode) },
409917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, EHL, &dwc3_pci_intel_swnode) },
410917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, TGPLP, &dwc3_pci_intel_swnode) },
411917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, TGPH, &dwc3_pci_intel_swnode) },
412917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, JSP, &dwc3_pci_intel_swnode) },
413917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, ADL, &dwc3_pci_intel_swnode) },
414917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, ADL_PCH, &dwc3_pci_intel_swnode) },
415917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, ADLN, &dwc3_pci_intel_swnode) },
416917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, ADLN_PCH, &dwc3_pci_intel_swnode) },
417917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, ADLS, &dwc3_pci_intel_swnode) },
418917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, RPL, &dwc3_pci_intel_swnode) },
419917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, RPLS, &dwc3_pci_intel_swnode) },
420917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, MTLM, &dwc3_pci_intel_swnode) },
421917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, MTLP, &dwc3_pci_intel_swnode) },
422917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, MTL, &dwc3_pci_intel_swnode) },
4238e86652eSGreg Kroah-Hartman 	{ PCI_DEVICE_DATA(INTEL, MTLS, &dwc3_pci_intel_swnode) },
424917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, TGL, &dwc3_pci_intel_swnode) },
4251a7b12f6SAndy Shevchenko 
426917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(AMD, NL_USB, &dwc3_pci_amd_swnode) },
427917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(AMD, MR, &dwc3_pci_amd_mr_swnode) },
4281abade64SNehal Bakulchandra Shah 
42972246da4SFelipe Balbi 	{  }	/* Terminating Entry */
43072246da4SFelipe Balbi };
43172246da4SFelipe Balbi MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
43272246da4SFelipe Balbi 
43336daf3aaSFelipe Balbi #if defined(CONFIG_PM) || defined(CONFIG_PM_SLEEP)
43436daf3aaSFelipe Balbi static int dwc3_pci_dsm(struct dwc3_pci *dwc, int param)
43536daf3aaSFelipe Balbi {
43636daf3aaSFelipe Balbi 	union acpi_object *obj;
43736daf3aaSFelipe Balbi 	union acpi_object tmp;
43836daf3aaSFelipe Balbi 	union acpi_object argv4 = ACPI_INIT_DSM_ARGV4(1, &tmp);
43936daf3aaSFelipe Balbi 
44036daf3aaSFelipe Balbi 	if (!dwc->has_dsm_for_pm)
44136daf3aaSFelipe Balbi 		return 0;
44236daf3aaSFelipe Balbi 
44336daf3aaSFelipe Balbi 	tmp.type = ACPI_TYPE_INTEGER;
44436daf3aaSFelipe Balbi 	tmp.integer.value = param;
44536daf3aaSFelipe Balbi 
44694116f81SAndy Shevchenko 	obj = acpi_evaluate_dsm(ACPI_HANDLE(&dwc->pci->dev), &dwc->guid,
44736daf3aaSFelipe Balbi 			1, PCI_INTEL_BXT_FUNC_PMU_PWR, &argv4);
44836daf3aaSFelipe Balbi 	if (!obj) {
44936daf3aaSFelipe Balbi 		dev_err(&dwc->pci->dev, "failed to evaluate _DSM\n");
45036daf3aaSFelipe Balbi 		return -EIO;
45136daf3aaSFelipe Balbi 	}
45236daf3aaSFelipe Balbi 
45336daf3aaSFelipe Balbi 	ACPI_FREE(obj);
45436daf3aaSFelipe Balbi 
45536daf3aaSFelipe Balbi 	return 0;
45636daf3aaSFelipe Balbi }
45736daf3aaSFelipe Balbi #endif /* CONFIG_PM || CONFIG_PM_SLEEP */
45836daf3aaSFelipe Balbi 
459e9af9229SFelipe Balbi #ifdef CONFIG_PM
460e9af9229SFelipe Balbi static int dwc3_pci_runtime_suspend(struct device *dev)
461e9af9229SFelipe Balbi {
4629cecca75SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
4639cecca75SFelipe Balbi 
464de3ef1ebSRafael J. Wysocki 	if (device_can_wakeup(dev))
4659cecca75SFelipe Balbi 		return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D3);
466e9af9229SFelipe Balbi 
467e9af9229SFelipe Balbi 	return -EBUSY;
468e9af9229SFelipe Balbi }
469e9af9229SFelipe Balbi 
470f6c274e1SFelipe Balbi static int dwc3_pci_runtime_resume(struct device *dev)
471f6c274e1SFelipe Balbi {
4720f817ae6SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
4739cecca75SFelipe Balbi 	int			ret;
4749cecca75SFelipe Balbi 
4759cecca75SFelipe Balbi 	ret = dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D0);
4769cecca75SFelipe Balbi 	if (ret)
4779cecca75SFelipe Balbi 		return ret;
478f6c274e1SFelipe Balbi 
4798eed00b2SManu Gautam 	queue_work(pm_wq, &dwc->wakeup_work);
4808eed00b2SManu Gautam 
4818eed00b2SManu Gautam 	return 0;
482f6c274e1SFelipe Balbi }
483696118c0SFelipe Balbi #endif /* CONFIG_PM */
484f6c274e1SFelipe Balbi 
485696118c0SFelipe Balbi #ifdef CONFIG_PM_SLEEP
4869cecca75SFelipe Balbi static int dwc3_pci_suspend(struct device *dev)
487e9af9229SFelipe Balbi {
4889cecca75SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
4899cecca75SFelipe Balbi 
4909cecca75SFelipe Balbi 	return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D3);
4919cecca75SFelipe Balbi }
4929cecca75SFelipe Balbi 
4939cecca75SFelipe Balbi static int dwc3_pci_resume(struct device *dev)
4949cecca75SFelipe Balbi {
4959cecca75SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
4969cecca75SFelipe Balbi 
4979cecca75SFelipe Balbi 	return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D0);
498e9af9229SFelipe Balbi }
499696118c0SFelipe Balbi #endif /* CONFIG_PM_SLEEP */
500e9af9229SFelipe Balbi 
50195aa932cSDoug Wilson static const struct dev_pm_ops dwc3_pci_dev_pm_ops = {
5029cecca75SFelipe Balbi 	SET_SYSTEM_SLEEP_PM_OPS(dwc3_pci_suspend, dwc3_pci_resume)
503f6c274e1SFelipe Balbi 	SET_RUNTIME_PM_OPS(dwc3_pci_runtime_suspend, dwc3_pci_runtime_resume,
504e9af9229SFelipe Balbi 		NULL)
505e9af9229SFelipe Balbi };
506e9af9229SFelipe Balbi 
50772246da4SFelipe Balbi static struct pci_driver dwc3_pci_driver = {
5080949e99bSFelipe Balbi 	.name		= "dwc3-pci",
50972246da4SFelipe Balbi 	.id_table	= dwc3_pci_id_table,
51072246da4SFelipe Balbi 	.probe		= dwc3_pci_probe,
5117690417dSBill Pemberton 	.remove		= dwc3_pci_remove,
512e9af9229SFelipe Balbi 	.driver		= {
513e9af9229SFelipe Balbi 		.pm	= &dwc3_pci_dev_pm_ops,
514e9af9229SFelipe Balbi 	}
51572246da4SFelipe Balbi };
51672246da4SFelipe Balbi 
51772246da4SFelipe Balbi MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
5185945f789SFelipe Balbi MODULE_LICENSE("GPL v2");
51972246da4SFelipe Balbi MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
52072246da4SFelipe Balbi 
52195656336SGreg Kroah-Hartman module_pci_driver(dwc3_pci_driver);
522