xref: /linux/drivers/usb/dwc3/dwc3-pci.c (revision f05f80f217bf52443a2582bca19fd78188333f25)
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
48*f05f80f2SShruthi Sanil #define PCI_DEVICE_ID_INTEL_RPL			0xa70e
49038438a2SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_RPLS		0x7a61
50973e0f7aSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_MTLP		0x7ec1
51973e0f7aSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_MTL			0x7e7e
5273203bdeSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_TGL			0x9a15
531abade64SNehal Bakulchandra Shah #define PCI_DEVICE_ID_AMD_MR			0x163a
5472246da4SFelipe Balbi 
5594116f81SAndy Shevchenko #define PCI_INTEL_BXT_DSM_GUID		"732b85d5-b7a7-4a1b-9ba0-4bbd00ffd511"
569cecca75SFelipe Balbi #define PCI_INTEL_BXT_FUNC_PMU_PWR	4
579cecca75SFelipe Balbi #define PCI_INTEL_BXT_STATE_D0		0
589cecca75SFelipe Balbi #define PCI_INTEL_BXT_STATE_D3		3
599cecca75SFelipe Balbi 
607740d04dSHans de Goede #define GP_RWBAR			1
617740d04dSHans de Goede #define GP_RWREG1			0xa0
627740d04dSHans de Goede #define GP_RWREG1_ULPI_REFCLK_DISABLE	(1 << 17)
637740d04dSHans de Goede 
640f817ae6SFelipe Balbi /**
650f817ae6SFelipe Balbi  * struct dwc3_pci - Driver private structure
660f817ae6SFelipe Balbi  * @dwc3: child dwc3 platform_device
670f817ae6SFelipe Balbi  * @pci: our link to PCI bus
6894116f81SAndy Shevchenko  * @guid: _DSM GUID
699cecca75SFelipe Balbi  * @has_dsm_for_pm: true for devices which need to run _DSM on runtime PM
7087d852deSAndy Shevchenko  * @wakeup_work: work for asynchronous resume
710f817ae6SFelipe Balbi  */
720f817ae6SFelipe Balbi struct dwc3_pci {
730f817ae6SFelipe Balbi 	struct platform_device *dwc3;
740f817ae6SFelipe Balbi 	struct pci_dev *pci;
759cecca75SFelipe Balbi 
7694116f81SAndy Shevchenko 	guid_t guid;
779cecca75SFelipe Balbi 
789cecca75SFelipe Balbi 	unsigned int has_dsm_for_pm:1;
798eed00b2SManu Gautam 	struct work_struct wakeup_work;
800f817ae6SFelipe Balbi };
810f817ae6SFelipe Balbi 
82a89d977cSHeikki Krogerus static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
83a89d977cSHeikki Krogerus static const struct acpi_gpio_params cs_gpios = { 1, 0, false };
84a89d977cSHeikki Krogerus 
85a89d977cSHeikki Krogerus static const struct acpi_gpio_mapping acpi_dwc3_byt_gpios[] = {
86a89d977cSHeikki Krogerus 	{ "reset-gpios", &reset_gpios, 1 },
87a89d977cSHeikki Krogerus 	{ "cs-gpios", &cs_gpios, 1 },
88a89d977cSHeikki Krogerus 	{ },
89a89d977cSHeikki Krogerus };
90a89d977cSHeikki Krogerus 
915741022cSHans de Goede static struct gpiod_lookup_table platform_bytcr_gpios = {
925741022cSHans de Goede 	.dev_id		= "0000:00:16.0",
935741022cSHans de Goede 	.table		= {
9462e3f0afSHans de Goede 		GPIO_LOOKUP("INT33FC:00", 54, "cs", GPIO_ACTIVE_HIGH),
9562e3f0afSHans de Goede 		GPIO_LOOKUP("INT33FC:02", 14, "reset", GPIO_ACTIVE_HIGH),
965741022cSHans de Goede 		{}
975741022cSHans de Goede 	},
985741022cSHans de Goede };
995741022cSHans de Goede 
1007740d04dSHans de Goede static int dwc3_byt_enable_ulpi_refclock(struct pci_dev *pci)
1017740d04dSHans de Goede {
1027740d04dSHans de Goede 	void __iomem	*reg;
1037740d04dSHans de Goede 	u32		value;
1047740d04dSHans de Goede 
1057740d04dSHans de Goede 	reg = pcim_iomap(pci, GP_RWBAR, 0);
106b497fff6SWei Yongjun 	if (!reg)
107b497fff6SWei Yongjun 		return -ENOMEM;
1087740d04dSHans de Goede 
1097740d04dSHans de Goede 	value = readl(reg + GP_RWREG1);
1107740d04dSHans de Goede 	if (!(value & GP_RWREG1_ULPI_REFCLK_DISABLE))
1117740d04dSHans de Goede 		goto unmap; /* ULPI refclk already enabled */
1127740d04dSHans de Goede 
1137740d04dSHans de Goede 	value &= ~GP_RWREG1_ULPI_REFCLK_DISABLE;
1147740d04dSHans de Goede 	writel(value, reg + GP_RWREG1);
1157740d04dSHans de Goede 	/* This comes from the Intel Android x86 tree w/o any explanation */
1167740d04dSHans de Goede 	msleep(100);
1177740d04dSHans de Goede unmap:
1187740d04dSHans de Goede 	pcim_iounmap(pci, reg);
1197740d04dSHans de Goede 	return 0;
1207740d04dSHans de Goede }
1217740d04dSHans de Goede 
1221a7b12f6SAndy Shevchenko static const struct property_entry dwc3_pci_intel_properties[] = {
1231a7b12f6SAndy Shevchenko 	PROPERTY_ENTRY_STRING("dr_mode", "peripheral"),
1241a7b12f6SAndy Shevchenko 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
1251a7b12f6SAndy Shevchenko 	{}
1261a7b12f6SAndy Shevchenko };
1270f817ae6SFelipe Balbi 
128582ab24eSHans de Goede static const struct property_entry dwc3_pci_intel_phy_charger_detect_properties[] = {
129582ab24eSHans de Goede 	PROPERTY_ENTRY_STRING("dr_mode", "peripheral"),
130582ab24eSHans de Goede 	PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
131582ab24eSHans de Goede 	PROPERTY_ENTRY_BOOL("linux,phy_charger_detect"),
1327ddda261SStephan Gerhold 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
133ca9400efSGreg Kroah-Hartman 	{}
134ca9400efSGreg Kroah-Hartman };
135ca9400efSGreg Kroah-Hartman 
136d7c93a90SHans de Goede static const struct property_entry dwc3_pci_intel_byt_properties[] = {
137d7c93a90SHans de Goede 	PROPERTY_ENTRY_STRING("dr_mode", "peripheral"),
138d7c93a90SHans de Goede 	PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
139582ab24eSHans de Goede 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
140582ab24eSHans de Goede 	{}
141582ab24eSHans de Goede };
142582ab24eSHans de Goede 
143c31d983bSAndy Shevchenko static const struct property_entry dwc3_pci_mrfld_properties[] = {
144c31d983bSAndy Shevchenko 	PROPERTY_ENTRY_STRING("dr_mode", "otg"),
145066c0959SAndy Shevchenko 	PROPERTY_ENTRY_STRING("linux,extcon-name", "mrfld_bcove_pwrsrc"),
146b522f830SAndy Shevchenko 	PROPERTY_ENTRY_BOOL("snps,dis_u3_susphy_quirk"),
147b522f830SAndy Shevchenko 	PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
14804357fafSFerry Toth 	PROPERTY_ENTRY_BOOL("snps,usb2-gadget-lpm-disable"),
149c31d983bSAndy Shevchenko 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
150c31d983bSAndy Shevchenko 	{}
151c31d983bSAndy Shevchenko };
152c31d983bSAndy Shevchenko 
1531a7b12f6SAndy Shevchenko static const struct property_entry dwc3_pci_amd_properties[] = {
154cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,has-lpm-erratum"),
155cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_U8("snps,lpm-nyet-threshold", 0xf),
156cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,u2exit_lfps_quirk"),
157cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,u2ss_inp3_quirk"),
158cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,req_p1p2p3_quirk"),
159cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,del_p1p2p3_quirk"),
160cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,del_phy_power_chg_quirk"),
161cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,lfps_filter_quirk"),
162cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,rx_detect_poll_quirk"),
163cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,tx_de_emphasis_quirk"),
164cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_U8("snps,tx_de_emphasis", 1),
1651a7b12f6SAndy Shevchenko 	/* FIXME these quirks should be removed when AMD NL tapes out */
166cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,disable_scramble_quirk"),
167cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,dis_u3_susphy_quirk"),
168cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
1690eae2fdeSJohn Youn 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
170e6fe66feSFelipe Balbi 	{}
171e6fe66feSFelipe Balbi };
172e6fe66feSFelipe Balbi 
1731abade64SNehal Bakulchandra Shah static const struct property_entry dwc3_pci_mr_properties[] = {
1741abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_STRING("dr_mode", "otg"),
1751abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_BOOL("usb-role-switch"),
1761abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_STRING("role-switch-default-mode", "host"),
1771abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
1781abade64SNehal Bakulchandra Shah 	{}
1791abade64SNehal Bakulchandra Shah };
1801abade64SNehal Bakulchandra Shah 
181e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_intel_swnode = {
182e492ce9bSHeikki Krogerus 	.properties = dwc3_pci_intel_properties,
183e492ce9bSHeikki Krogerus };
184e492ce9bSHeikki Krogerus 
185582ab24eSHans de Goede static const struct software_node dwc3_pci_intel_phy_charger_detect_swnode = {
186582ab24eSHans de Goede 	.properties = dwc3_pci_intel_phy_charger_detect_properties,
187582ab24eSHans de Goede };
188582ab24eSHans de Goede 
189d7c93a90SHans de Goede static const struct software_node dwc3_pci_intel_byt_swnode = {
190d7c93a90SHans de Goede 	.properties = dwc3_pci_intel_byt_properties,
191d7c93a90SHans de Goede };
192d7c93a90SHans de Goede 
193e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_intel_mrfld_swnode = {
194e492ce9bSHeikki Krogerus 	.properties = dwc3_pci_mrfld_properties,
195e492ce9bSHeikki Krogerus };
196e492ce9bSHeikki Krogerus 
197e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_amd_swnode = {
198e492ce9bSHeikki Krogerus 	.properties = dwc3_pci_amd_properties,
199e492ce9bSHeikki Krogerus };
200e492ce9bSHeikki Krogerus 
2011abade64SNehal Bakulchandra Shah static const struct software_node dwc3_pci_amd_mr_swnode = {
2021abade64SNehal Bakulchandra Shah 	.properties = dwc3_pci_mr_properties,
2031abade64SNehal Bakulchandra Shah };
2041abade64SNehal Bakulchandra Shah 
205e285cb40SHans de Goede static int dwc3_pci_quirks(struct dwc3_pci *dwc,
206e285cb40SHans de Goede 			   const struct software_node *swnode)
2071a7b12f6SAndy Shevchenko {
2081a7b12f6SAndy Shevchenko 	struct pci_dev			*pdev = dwc->pci;
209e6fe66feSFelipe Balbi 
2101a7b12f6SAndy Shevchenko 	if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
2119cecca75SFelipe Balbi 		if (pdev->device == PCI_DEVICE_ID_INTEL_BXT ||
212a609ce2aSRaymond Tan 		    pdev->device == PCI_DEVICE_ID_INTEL_BXT_M ||
213457d2285SRaymond Tan 		    pdev->device == PCI_DEVICE_ID_INTEL_EHL) {
21494116f81SAndy Shevchenko 			guid_parse(PCI_INTEL_BXT_DSM_GUID, &dwc->guid);
2159cecca75SFelipe Balbi 			dwc->has_dsm_for_pm = true;
2169cecca75SFelipe Balbi 		}
2179cecca75SFelipe Balbi 
218e6fe66feSFelipe Balbi 		if (pdev->device == PCI_DEVICE_ID_INTEL_BYT) {
219a89d977cSHeikki Krogerus 			struct gpio_desc *gpio;
2201a7b12f6SAndy Shevchenko 			int ret;
221a89d977cSHeikki Krogerus 
2227740d04dSHans de Goede 			/* On BYT the FW does not always enable the refclock */
2237740d04dSHans de Goede 			ret = dwc3_byt_enable_ulpi_refclock(pdev);
2247740d04dSHans de Goede 			if (ret)
2257740d04dSHans de Goede 				return ret;
2267740d04dSHans de Goede 
2274a56e413SAndy Shevchenko 			ret = devm_acpi_dev_add_driver_gpios(&pdev->dev,
228a89d977cSHeikki Krogerus 					acpi_dwc3_byt_gpios);
2294a56e413SAndy Shevchenko 			if (ret)
2304a56e413SAndy Shevchenko 				dev_dbg(&pdev->dev, "failed to add mapping table\n");
231a89d977cSHeikki Krogerus 
2322df033caSUwe Kleine-König 			/*
2335741022cSHans de Goede 			 * A lot of BYT devices lack ACPI resource entries for
2345741022cSHans de Goede 			 * the GPIOs, add a fallback mapping to the reference
2355741022cSHans de Goede 			 * design GPIOs which all boards seem to use.
2365741022cSHans de Goede 			 */
2375741022cSHans de Goede 			gpiod_add_lookup_table(&platform_bytcr_gpios);
2385741022cSHans de Goede 
2395741022cSHans de Goede 			/*
2402df033caSUwe Kleine-König 			 * These GPIOs will turn on the USB2 PHY. Note that we have to
2412df033caSUwe Kleine-König 			 * put the gpio descriptors again here because the phy driver
2422df033caSUwe Kleine-König 			 * might want to grab them, too.
2432df033caSUwe Kleine-König 			 */
2443004cfd6SStephan Gerhold 			gpio = gpiod_get_optional(&pdev->dev, "cs", GPIOD_OUT_LOW);
2452df033caSUwe Kleine-König 			if (IS_ERR(gpio))
2462df033caSUwe Kleine-König 				return PTR_ERR(gpio);
2472df033caSUwe Kleine-König 
248a89d977cSHeikki Krogerus 			gpiod_set_value_cansleep(gpio, 1);
2493004cfd6SStephan Gerhold 			gpiod_put(gpio);
250a89d977cSHeikki Krogerus 
2513004cfd6SStephan Gerhold 			gpio = gpiod_get_optional(&pdev->dev, "reset", GPIOD_OUT_LOW);
2522df033caSUwe Kleine-König 			if (IS_ERR(gpio))
2532df033caSUwe Kleine-König 				return PTR_ERR(gpio);
2542df033caSUwe Kleine-König 
2552df033caSUwe Kleine-König 			if (gpio) {
256a89d977cSHeikki Krogerus 				gpiod_set_value_cansleep(gpio, 1);
2573004cfd6SStephan Gerhold 				gpiod_put(gpio);
258a89d977cSHeikki Krogerus 				usleep_range(10000, 11000);
259a89d977cSHeikki Krogerus 			}
260582ab24eSHans de Goede 
261582ab24eSHans de Goede 			/*
262a5d847b0SHans de Goede 			 * Make the pdev name predictable (only 1 DWC3 on BYT)
263a5d847b0SHans de Goede 			 * and patch the phy dev-name into the lookup table so
264a5d847b0SHans de Goede 			 * that the phy-driver can get the GPIOs.
265a5d847b0SHans de Goede 			 */
266a5d847b0SHans de Goede 			dwc->dwc3->id = PLATFORM_DEVID_NONE;
267a5d847b0SHans de Goede 			platform_bytcr_gpios.dev_id = "dwc3.ulpi";
268a5d847b0SHans de Goede 
269a5d847b0SHans de Goede 			/*
270582ab24eSHans de Goede 			 * Some Android tablets with a Crystal Cove PMIC
271582ab24eSHans de Goede 			 * (INT33FD), rely on the TUSB1211 phy for charger
272582ab24eSHans de Goede 			 * detection. These can be identified by them _not_
273582ab24eSHans de Goede 			 * using the standard ACPI battery and ac drivers.
274582ab24eSHans de Goede 			 */
275582ab24eSHans de Goede 			if (acpi_dev_present("INT33FD", "1", 2) &&
276582ab24eSHans de Goede 			    acpi_quirk_skip_acpi_ac_and_battery()) {
277582ab24eSHans de Goede 				dev_info(&pdev->dev, "Using TUSB1211 phy for charger detection\n");
278582ab24eSHans de Goede 				swnode = &dwc3_pci_intel_phy_charger_detect_swnode;
279582ab24eSHans de Goede 			}
280a89d977cSHeikki Krogerus 		}
281e6fe66feSFelipe Balbi 	}
282a89d977cSHeikki Krogerus 
283e285cb40SHans de Goede 	return device_add_software_node(&dwc->dwc3->dev, swnode);
2842cd9ddf7SHeikki Krogerus }
28572246da4SFelipe Balbi 
2868eed00b2SManu Gautam #ifdef CONFIG_PM
2878eed00b2SManu Gautam static void dwc3_pci_resume_work(struct work_struct *work)
2888eed00b2SManu Gautam {
2898eed00b2SManu Gautam 	struct dwc3_pci *dwc = container_of(work, struct dwc3_pci, wakeup_work);
2908eed00b2SManu Gautam 	struct platform_device *dwc3 = dwc->dwc3;
2918eed00b2SManu Gautam 	int ret;
2928eed00b2SManu Gautam 
2938eed00b2SManu Gautam 	ret = pm_runtime_get_sync(&dwc3->dev);
294a03e2ddaSZheng Yongjun 	if (ret < 0) {
2952655971aSAditya Pakki 		pm_runtime_put_sync_autosuspend(&dwc3->dev);
2968eed00b2SManu Gautam 		return;
2972655971aSAditya Pakki 	}
2988eed00b2SManu Gautam 
2998eed00b2SManu Gautam 	pm_runtime_mark_last_busy(&dwc3->dev);
3008eed00b2SManu Gautam 	pm_runtime_put_sync_autosuspend(&dwc3->dev);
3018eed00b2SManu Gautam }
3028eed00b2SManu Gautam #endif
3038eed00b2SManu Gautam 
3041a7b12f6SAndy Shevchenko static int dwc3_pci_probe(struct pci_dev *pci, const struct pci_device_id *id)
30572246da4SFelipe Balbi {
3060f817ae6SFelipe Balbi 	struct dwc3_pci		*dwc;
30772246da4SFelipe Balbi 	struct resource		res[2];
308b09e99eeSAndy Shevchenko 	int			ret;
309802ca850SChanho Park 	struct device		*dev = &pci->dev;
31072246da4SFelipe Balbi 
311f1c7e710SAndy Shevchenko 	ret = pcim_enable_device(pci);
31272246da4SFelipe Balbi 	if (ret) {
313802ca850SChanho Park 		dev_err(dev, "failed to enable pci device\n");
314802ca850SChanho Park 		return -ENODEV;
31572246da4SFelipe Balbi 	}
31672246da4SFelipe Balbi 
31772246da4SFelipe Balbi 	pci_set_master(pci);
31872246da4SFelipe Balbi 
3190f817ae6SFelipe Balbi 	dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
3200f817ae6SFelipe Balbi 	if (!dwc)
321f1c7e710SAndy Shevchenko 		return -ENOMEM;
3220f817ae6SFelipe Balbi 
3230f817ae6SFelipe Balbi 	dwc->dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO);
3240f817ae6SFelipe Balbi 	if (!dwc->dwc3)
3250f817ae6SFelipe Balbi 		return -ENOMEM;
32672246da4SFelipe Balbi 
32772246da4SFelipe Balbi 	memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
32872246da4SFelipe Balbi 
32972246da4SFelipe Balbi 	res[0].start	= pci_resource_start(pci, 0);
33072246da4SFelipe Balbi 	res[0].end	= pci_resource_end(pci, 0);
33172246da4SFelipe Balbi 	res[0].name	= "dwc_usb3";
33272246da4SFelipe Balbi 	res[0].flags	= IORESOURCE_MEM;
33372246da4SFelipe Balbi 
33472246da4SFelipe Balbi 	res[1].start	= pci->irq;
33572246da4SFelipe Balbi 	res[1].name	= "dwc_usb3";
33672246da4SFelipe Balbi 	res[1].flags	= IORESOURCE_IRQ;
33772246da4SFelipe Balbi 
3380f817ae6SFelipe Balbi 	ret = platform_device_add_resources(dwc->dwc3, res, ARRAY_SIZE(res));
33972246da4SFelipe Balbi 	if (ret) {
340802ca850SChanho Park 		dev_err(dev, "couldn't add resources to dwc3 device\n");
341cabdf83dSThinh Nguyen 		goto err;
34272246da4SFelipe Balbi 	}
34372246da4SFelipe Balbi 
3440f817ae6SFelipe Balbi 	dwc->pci = pci;
3450f817ae6SFelipe Balbi 	dwc->dwc3->dev.parent = dev;
3460f817ae6SFelipe Balbi 	ACPI_COMPANION_SET(&dwc->dwc3->dev, ACPI_COMPANION(dev));
34772246da4SFelipe Balbi 
348e285cb40SHans de Goede 	ret = dwc3_pci_quirks(dwc, (void *)id->driver_data);
349474799f0SHeikki Krogerus 	if (ret)
350474799f0SHeikki Krogerus 		goto err;
351474799f0SHeikki Krogerus 
3520f817ae6SFelipe Balbi 	ret = platform_device_add(dwc->dwc3);
35372246da4SFelipe Balbi 	if (ret) {
354802ca850SChanho Park 		dev_err(dev, "failed to register dwc3 device\n");
3552cd9ddf7SHeikki Krogerus 		goto err;
35672246da4SFelipe Balbi 	}
35772246da4SFelipe Balbi 
358e9af9229SFelipe Balbi 	device_init_wakeup(dev, true);
3590f817ae6SFelipe Balbi 	pci_set_drvdata(pci, dwc);
360e9af9229SFelipe Balbi 	pm_runtime_put(dev);
3618eed00b2SManu Gautam #ifdef CONFIG_PM
3628eed00b2SManu Gautam 	INIT_WORK(&dwc->wakeup_work, dwc3_pci_resume_work);
3638eed00b2SManu Gautam #endif
364e9af9229SFelipe Balbi 
36572246da4SFelipe Balbi 	return 0;
3662cd9ddf7SHeikki Krogerus err:
367e492ce9bSHeikki Krogerus 	device_remove_software_node(&dwc->dwc3->dev);
3680f817ae6SFelipe Balbi 	platform_device_put(dwc->dwc3);
36972246da4SFelipe Balbi 	return ret;
37072246da4SFelipe Balbi }
37172246da4SFelipe Balbi 
372fb4e98abSBill Pemberton static void dwc3_pci_remove(struct pci_dev *pci)
37372246da4SFelipe Balbi {
3740f817ae6SFelipe Balbi 	struct dwc3_pci		*dwc = pci_get_drvdata(pci);
3757b412b04SKuppuswamy Sathyanarayanan 	struct pci_dev		*pdev = dwc->pci;
3760f817ae6SFelipe Balbi 
3777b412b04SKuppuswamy Sathyanarayanan 	if (pdev->device == PCI_DEVICE_ID_INTEL_BYT)
3785741022cSHans de Goede 		gpiod_remove_lookup_table(&platform_bytcr_gpios);
3798eed00b2SManu Gautam #ifdef CONFIG_PM
3808eed00b2SManu Gautam 	cancel_work_sync(&dwc->wakeup_work);
3818eed00b2SManu Gautam #endif
382e9af9229SFelipe Balbi 	device_init_wakeup(&pci->dev, false);
383e9af9229SFelipe Balbi 	pm_runtime_get(&pci->dev);
384e492ce9bSHeikki Krogerus 	device_remove_software_node(&dwc->dwc3->dev);
3850f817ae6SFelipe Balbi 	platform_device_unregister(dwc->dwc3);
38672246da4SFelipe Balbi }
38772246da4SFelipe Balbi 
388782df20cSJingoo Han static const struct pci_device_id dwc3_pci_id_table[] = {
3891a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BSW),
390e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
3911a7b12f6SAndy Shevchenko 
3921a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BYT),
393d7c93a90SHans de Goede 	  (kernel_ulong_t) &dwc3_pci_intel_byt_swnode, },
3941a7b12f6SAndy Shevchenko 
3951a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MRFLD),
396e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_mrfld_swnode, },
3971a7b12f6SAndy Shevchenko 
3983c3caae4SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CMLLP),
399e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
4003c3caae4SHeikki Krogerus 
4017ae622c9SFelipe Balbi 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CMLH),
402e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
4037ae622c9SFelipe Balbi 
4041a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SPTLP),
405e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
4061a7b12f6SAndy Shevchenko 
4071a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_SPTH),
408e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
4091a7b12f6SAndy Shevchenko 
4101a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BXT),
411e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
4121a7b12f6SAndy Shevchenko 
4131a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BXT_M),
414e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
4151a7b12f6SAndy Shevchenko 
4161a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_APL),
417e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
4181a7b12f6SAndy Shevchenko 
4191a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_KBP),
420e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
4211a7b12f6SAndy Shevchenko 
4221a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_GLK),
423e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
4241a7b12f6SAndy Shevchenko 
4251a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CNPLP),
426e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
4271a7b12f6SAndy Shevchenko 
4281a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CNPH),
429e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
4301a7b12f6SAndy Shevchenko 
431f5ae8869SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_CNPV),
432e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
433f5ae8869SHeikki Krogerus 
4341a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICLLP),
435e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
4361a7b12f6SAndy Shevchenko 
437457d2285SRaymond Tan 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_EHL),
438457d2285SRaymond Tan 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
439dbb0569dSFelipe Balbi 
440b3649deeSFelipe Balbi 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGPLP),
441e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
442b3649deeSFelipe Balbi 
443c3f595a8SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGPH),
444e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
445c3f595a8SHeikki Krogerus 
446e25d1e85SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_JSP),
447e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
448e25d1e85SHeikki Krogerus 
44993440d1fSShruthi Sanil 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL),
45093440d1fSShruthi Sanil 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
45193440d1fSShruthi Sanil 
452ff2d2beeSShruthi Sanil 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_PCH),
453ecb0a2f1SShruthi Sanil 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
454ecb0a2f1SShruthi Sanil 
455ff2d2beeSShruthi Sanil 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADLN),
456f08fc2c3SHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
457f08fc2c3SHeikki Krogerus 
458ff2d2beeSShruthi Sanil 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADLN_PCH),
459782de5e7SHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
460782de5e7SHeikki Krogerus 
4611384ab4fSHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADLS),
462e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
4631384ab4fSHeikki Krogerus 
464bad0d1d7SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPL),
465bad0d1d7SHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
466bad0d1d7SHeikki Krogerus 
467038438a2SHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPLS),
468038438a2SHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
469038438a2SHeikki Krogerus 
470973e0f7aSHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTLP),
471973e0f7aSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
472973e0f7aSHeikki Krogerus 
473973e0f7aSHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL),
474973e0f7aSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
475973e0f7aSHeikki Krogerus 
47673203bdeSHeikki Krogerus 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL),
47773203bdeSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_intel_swnode, },
47873203bdeSHeikki Krogerus 
4791a7b12f6SAndy Shevchenko 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_NL_USB),
480e492ce9bSHeikki Krogerus 	  (kernel_ulong_t) &dwc3_pci_amd_swnode, },
4811abade64SNehal Bakulchandra Shah 
4821abade64SNehal Bakulchandra Shah 	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_MR),
4831abade64SNehal Bakulchandra Shah 	  (kernel_ulong_t)&dwc3_pci_amd_mr_swnode, },
4841abade64SNehal Bakulchandra Shah 
48572246da4SFelipe Balbi 	{  }	/* Terminating Entry */
48672246da4SFelipe Balbi };
48772246da4SFelipe Balbi MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
48872246da4SFelipe Balbi 
48936daf3aaSFelipe Balbi #if defined(CONFIG_PM) || defined(CONFIG_PM_SLEEP)
49036daf3aaSFelipe Balbi static int dwc3_pci_dsm(struct dwc3_pci *dwc, int param)
49136daf3aaSFelipe Balbi {
49236daf3aaSFelipe Balbi 	union acpi_object *obj;
49336daf3aaSFelipe Balbi 	union acpi_object tmp;
49436daf3aaSFelipe Balbi 	union acpi_object argv4 = ACPI_INIT_DSM_ARGV4(1, &tmp);
49536daf3aaSFelipe Balbi 
49636daf3aaSFelipe Balbi 	if (!dwc->has_dsm_for_pm)
49736daf3aaSFelipe Balbi 		return 0;
49836daf3aaSFelipe Balbi 
49936daf3aaSFelipe Balbi 	tmp.type = ACPI_TYPE_INTEGER;
50036daf3aaSFelipe Balbi 	tmp.integer.value = param;
50136daf3aaSFelipe Balbi 
50294116f81SAndy Shevchenko 	obj = acpi_evaluate_dsm(ACPI_HANDLE(&dwc->pci->dev), &dwc->guid,
50336daf3aaSFelipe Balbi 			1, PCI_INTEL_BXT_FUNC_PMU_PWR, &argv4);
50436daf3aaSFelipe Balbi 	if (!obj) {
50536daf3aaSFelipe Balbi 		dev_err(&dwc->pci->dev, "failed to evaluate _DSM\n");
50636daf3aaSFelipe Balbi 		return -EIO;
50736daf3aaSFelipe Balbi 	}
50836daf3aaSFelipe Balbi 
50936daf3aaSFelipe Balbi 	ACPI_FREE(obj);
51036daf3aaSFelipe Balbi 
51136daf3aaSFelipe Balbi 	return 0;
51236daf3aaSFelipe Balbi }
51336daf3aaSFelipe Balbi #endif /* CONFIG_PM || CONFIG_PM_SLEEP */
51436daf3aaSFelipe Balbi 
515e9af9229SFelipe Balbi #ifdef CONFIG_PM
516e9af9229SFelipe Balbi static int dwc3_pci_runtime_suspend(struct device *dev)
517e9af9229SFelipe Balbi {
5189cecca75SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
5199cecca75SFelipe Balbi 
520de3ef1ebSRafael J. Wysocki 	if (device_can_wakeup(dev))
5219cecca75SFelipe Balbi 		return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D3);
522e9af9229SFelipe Balbi 
523e9af9229SFelipe Balbi 	return -EBUSY;
524e9af9229SFelipe Balbi }
525e9af9229SFelipe Balbi 
526f6c274e1SFelipe Balbi static int dwc3_pci_runtime_resume(struct device *dev)
527f6c274e1SFelipe Balbi {
5280f817ae6SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
5299cecca75SFelipe Balbi 	int			ret;
5309cecca75SFelipe Balbi 
5319cecca75SFelipe Balbi 	ret = dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D0);
5329cecca75SFelipe Balbi 	if (ret)
5339cecca75SFelipe Balbi 		return ret;
534f6c274e1SFelipe Balbi 
5358eed00b2SManu Gautam 	queue_work(pm_wq, &dwc->wakeup_work);
5368eed00b2SManu Gautam 
5378eed00b2SManu Gautam 	return 0;
538f6c274e1SFelipe Balbi }
539696118c0SFelipe Balbi #endif /* CONFIG_PM */
540f6c274e1SFelipe Balbi 
541696118c0SFelipe Balbi #ifdef CONFIG_PM_SLEEP
5429cecca75SFelipe Balbi static int dwc3_pci_suspend(struct device *dev)
543e9af9229SFelipe Balbi {
5449cecca75SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
5459cecca75SFelipe Balbi 
5469cecca75SFelipe Balbi 	return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D3);
5479cecca75SFelipe Balbi }
5489cecca75SFelipe Balbi 
5499cecca75SFelipe Balbi static int dwc3_pci_resume(struct device *dev)
5509cecca75SFelipe Balbi {
5519cecca75SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
5529cecca75SFelipe Balbi 
5539cecca75SFelipe Balbi 	return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D0);
554e9af9229SFelipe Balbi }
555696118c0SFelipe Balbi #endif /* CONFIG_PM_SLEEP */
556e9af9229SFelipe Balbi 
55795aa932cSDoug Wilson static const struct dev_pm_ops dwc3_pci_dev_pm_ops = {
5589cecca75SFelipe Balbi 	SET_SYSTEM_SLEEP_PM_OPS(dwc3_pci_suspend, dwc3_pci_resume)
559f6c274e1SFelipe Balbi 	SET_RUNTIME_PM_OPS(dwc3_pci_runtime_suspend, dwc3_pci_runtime_resume,
560e9af9229SFelipe Balbi 		NULL)
561e9af9229SFelipe Balbi };
562e9af9229SFelipe Balbi 
56372246da4SFelipe Balbi static struct pci_driver dwc3_pci_driver = {
5640949e99bSFelipe Balbi 	.name		= "dwc3-pci",
56572246da4SFelipe Balbi 	.id_table	= dwc3_pci_id_table,
56672246da4SFelipe Balbi 	.probe		= dwc3_pci_probe,
5677690417dSBill Pemberton 	.remove		= dwc3_pci_remove,
568e9af9229SFelipe Balbi 	.driver		= {
569e9af9229SFelipe Balbi 		.pm	= &dwc3_pci_dev_pm_ops,
570e9af9229SFelipe Balbi 	}
57172246da4SFelipe Balbi };
57272246da4SFelipe Balbi 
57372246da4SFelipe Balbi MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
5745945f789SFelipe Balbi MODULE_LICENSE("GPL v2");
57572246da4SFelipe Balbi MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
57672246da4SFelipe Balbi 
57795656336SGreg Kroah-Hartman module_pci_driver(dwc3_pci_driver);
578