xref: /linux/drivers/usb/dwc3/dwc3-pci.c (revision 1293147aa8c79381de155ef480e5b5769725182a)
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 
110fb782b5SHans de Goede #include <linux/dmi.h>
1272246da4SFelipe Balbi #include <linux/kernel.h>
1346a57283SStephen Rothwell #include <linux/module.h>
1472246da4SFelipe Balbi #include <linux/slab.h>
1572246da4SFelipe Balbi #include <linux/pci.h>
168eed00b2SManu Gautam #include <linux/workqueue.h>
17e9af9229SFelipe Balbi #include <linux/pm_runtime.h>
1872246da4SFelipe Balbi #include <linux/platform_device.h>
19a89d977cSHeikki Krogerus #include <linux/gpio/consumer.h>
205741022cSHans de Goede #include <linux/gpio/machine.h>
21a89d977cSHeikki Krogerus #include <linux/acpi.h>
22cf48305dSHeikki Krogerus #include <linux/delay.h>
238f317b47SHuang Rui 
24b62cd96dSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_BYT			0x0f37
2585601f8cSDavid Cohen #define PCI_DEVICE_ID_INTEL_MRFLD		0x119e
269a5a0783SJohn Youn #define PCI_DEVICE_ID_INTEL_BSW			0x22b7
2784a2b61bSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_SPTLP		0x9d30
2884a2b61bSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_SPTH		0xa130
29b4c580a4SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_BXT			0x0aaa
301ffb4d5cSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_BXT_M		0x1aaa
31b4c580a4SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_APL			0x5aaa
324491ed50SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_KBP			0xa2b0
333c3caae4SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_CMLLP		0x02ee
343c3caae4SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_CMLH		0x06ee
358f8983a5SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_GLK			0x31aa
3668217959SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_CNPLP		0x9dee
3768217959SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_CNPH		0xa36e
38f5ae8869SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_CNPV		0xa3b0
3900908693SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_ICLLP		0x34ee
40457d2285SRaymond Tan #define PCI_DEVICE_ID_INTEL_EHL			0x4b7e
41b3649deeSFelipe Balbi #define PCI_DEVICE_ID_INTEL_TGPLP		0xa0ee
42c3f595a8SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_TGPH		0x43ee
43e25d1e85SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_JSP			0x4dee
4493440d1fSShruthi Sanil #define PCI_DEVICE_ID_INTEL_ADL			0x460e
45ff2d2beeSShruthi Sanil #define PCI_DEVICE_ID_INTEL_ADL_PCH		0x51ee
46ff2d2beeSShruthi Sanil #define PCI_DEVICE_ID_INTEL_ADLN		0x465e
47ff2d2beeSShruthi Sanil #define PCI_DEVICE_ID_INTEL_ADLN_PCH		0x54ee
481384ab4fSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_ADLS		0x7ae1
49f05f80f2SShruthi Sanil #define PCI_DEVICE_ID_INTEL_RPL			0xa70e
50038438a2SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_RPLS		0x7a61
518e5248c3SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_MTLM		0x7eb1
52973e0f7aSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_MTLP		0x7ec1
53ec799c8aSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_MTLS		0x7f6f
54973e0f7aSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_MTL			0x7e7e
55de4b5b28SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_ARLH_PCH		0x777e
5673203bdeSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_TGL			0x9a15
57*2bf35ea4SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_PTLH		0xe332
58*2bf35ea4SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_PTLH_PCH		0xe37e
59*2bf35ea4SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_PTLU		0xe432
60*2bf35ea4SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_PTLU_PCH		0xe47e
611abade64SNehal Bakulchandra Shah #define PCI_DEVICE_ID_AMD_MR			0x163a
6272246da4SFelipe Balbi 
6394116f81SAndy Shevchenko #define PCI_INTEL_BXT_DSM_GUID		"732b85d5-b7a7-4a1b-9ba0-4bbd00ffd511"
649cecca75SFelipe Balbi #define PCI_INTEL_BXT_FUNC_PMU_PWR	4
659cecca75SFelipe Balbi #define PCI_INTEL_BXT_STATE_D0		0
669cecca75SFelipe Balbi #define PCI_INTEL_BXT_STATE_D3		3
679cecca75SFelipe Balbi 
687740d04dSHans de Goede #define GP_RWBAR			1
697740d04dSHans de Goede #define GP_RWREG1			0xa0
707740d04dSHans de Goede #define GP_RWREG1_ULPI_REFCLK_DISABLE	(1 << 17)
717740d04dSHans de Goede 
720f817ae6SFelipe Balbi /**
730f817ae6SFelipe Balbi  * struct dwc3_pci - Driver private structure
740f817ae6SFelipe Balbi  * @dwc3: child dwc3 platform_device
750f817ae6SFelipe Balbi  * @pci: our link to PCI bus
7694116f81SAndy Shevchenko  * @guid: _DSM GUID
779cecca75SFelipe Balbi  * @has_dsm_for_pm: true for devices which need to run _DSM on runtime PM
7887d852deSAndy Shevchenko  * @wakeup_work: work for asynchronous resume
790f817ae6SFelipe Balbi  */
800f817ae6SFelipe Balbi struct dwc3_pci {
810f817ae6SFelipe Balbi 	struct platform_device *dwc3;
820f817ae6SFelipe Balbi 	struct pci_dev *pci;
839cecca75SFelipe Balbi 
8494116f81SAndy Shevchenko 	guid_t guid;
859cecca75SFelipe Balbi 
869cecca75SFelipe Balbi 	unsigned int has_dsm_for_pm:1;
878eed00b2SManu Gautam 	struct work_struct wakeup_work;
880f817ae6SFelipe Balbi };
890f817ae6SFelipe Balbi 
90a89d977cSHeikki Krogerus static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
91a89d977cSHeikki Krogerus static const struct acpi_gpio_params cs_gpios = { 1, 0, false };
92a89d977cSHeikki Krogerus 
93a89d977cSHeikki Krogerus static const struct acpi_gpio_mapping acpi_dwc3_byt_gpios[] = {
94a89d977cSHeikki Krogerus 	{ "reset-gpios", &reset_gpios, 1 },
95a89d977cSHeikki Krogerus 	{ "cs-gpios", &cs_gpios, 1 },
96a89d977cSHeikki Krogerus 	{ },
97a89d977cSHeikki Krogerus };
98a89d977cSHeikki Krogerus 
995741022cSHans de Goede static struct gpiod_lookup_table platform_bytcr_gpios = {
1005741022cSHans de Goede 	.dev_id		= "0000:00:16.0",
1015741022cSHans de Goede 	.table		= {
10262e3f0afSHans de Goede 		GPIO_LOOKUP("INT33FC:00", 54, "cs", GPIO_ACTIVE_HIGH),
10362e3f0afSHans de Goede 		GPIO_LOOKUP("INT33FC:02", 14, "reset", GPIO_ACTIVE_HIGH),
1045741022cSHans de Goede 		{}
1055741022cSHans de Goede 	},
1065741022cSHans de Goede };
1075741022cSHans de Goede 
dwc3_byt_enable_ulpi_refclock(struct pci_dev * pci)1087740d04dSHans de Goede static int dwc3_byt_enable_ulpi_refclock(struct pci_dev *pci)
1097740d04dSHans de Goede {
1107740d04dSHans de Goede 	void __iomem	*reg;
1117740d04dSHans de Goede 	u32		value;
1127740d04dSHans de Goede 
1137740d04dSHans de Goede 	reg = pcim_iomap(pci, GP_RWBAR, 0);
114b497fff6SWei Yongjun 	if (!reg)
115b497fff6SWei Yongjun 		return -ENOMEM;
1167740d04dSHans de Goede 
1177740d04dSHans de Goede 	value = readl(reg + GP_RWREG1);
1187740d04dSHans de Goede 	if (!(value & GP_RWREG1_ULPI_REFCLK_DISABLE))
1197740d04dSHans de Goede 		goto unmap; /* ULPI refclk already enabled */
1207740d04dSHans de Goede 
1217740d04dSHans de Goede 	value &= ~GP_RWREG1_ULPI_REFCLK_DISABLE;
1227740d04dSHans de Goede 	writel(value, reg + GP_RWREG1);
1237740d04dSHans de Goede 	/* This comes from the Intel Android x86 tree w/o any explanation */
1247740d04dSHans de Goede 	msleep(100);
1257740d04dSHans de Goede unmap:
1267740d04dSHans de Goede 	pcim_iounmap(pci, reg);
1277740d04dSHans de Goede 	return 0;
1287740d04dSHans de Goede }
1297740d04dSHans de Goede 
1301a7b12f6SAndy Shevchenko static const struct property_entry dwc3_pci_intel_properties[] = {
1311a7b12f6SAndy Shevchenko 	PROPERTY_ENTRY_STRING("dr_mode", "peripheral"),
1321a7b12f6SAndy Shevchenko 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
1331a7b12f6SAndy Shevchenko 	{}
1341a7b12f6SAndy Shevchenko };
1350f817ae6SFelipe Balbi 
136582ab24eSHans de Goede static const struct property_entry dwc3_pci_intel_phy_charger_detect_properties[] = {
137582ab24eSHans de Goede 	PROPERTY_ENTRY_STRING("dr_mode", "peripheral"),
138582ab24eSHans de Goede 	PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
139582ab24eSHans de Goede 	PROPERTY_ENTRY_BOOL("linux,phy_charger_detect"),
1407ddda261SStephan Gerhold 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
141ca9400efSGreg Kroah-Hartman 	{}
142ca9400efSGreg Kroah-Hartman };
143ca9400efSGreg Kroah-Hartman 
144d7c93a90SHans de Goede static const struct property_entry dwc3_pci_intel_byt_properties[] = {
145d7c93a90SHans de Goede 	PROPERTY_ENTRY_STRING("dr_mode", "peripheral"),
146d7c93a90SHans de Goede 	PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
147582ab24eSHans de Goede 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
148582ab24eSHans de Goede 	{}
149582ab24eSHans de Goede };
150582ab24eSHans de Goede 
151c31d983bSAndy Shevchenko static const struct property_entry dwc3_pci_mrfld_properties[] = {
152c31d983bSAndy Shevchenko 	PROPERTY_ENTRY_STRING("dr_mode", "otg"),
153066c0959SAndy Shevchenko 	PROPERTY_ENTRY_STRING("linux,extcon-name", "mrfld_bcove_pwrsrc"),
154b522f830SAndy Shevchenko 	PROPERTY_ENTRY_BOOL("snps,dis_u3_susphy_quirk"),
155b522f830SAndy Shevchenko 	PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
15604357fafSFerry Toth 	PROPERTY_ENTRY_BOOL("snps,usb2-gadget-lpm-disable"),
157c31d983bSAndy Shevchenko 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
158c31d983bSAndy Shevchenko 	{}
159c31d983bSAndy Shevchenko };
160c31d983bSAndy Shevchenko 
1611a7b12f6SAndy Shevchenko static const struct property_entry dwc3_pci_amd_properties[] = {
162cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,has-lpm-erratum"),
163cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_U8("snps,lpm-nyet-threshold", 0xf),
164cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,u2exit_lfps_quirk"),
165cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,u2ss_inp3_quirk"),
166cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,req_p1p2p3_quirk"),
167cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,del_p1p2p3_quirk"),
168cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,del_phy_power_chg_quirk"),
169cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,lfps_filter_quirk"),
170cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,rx_detect_poll_quirk"),
171cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,tx_de_emphasis_quirk"),
172cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_U8("snps,tx_de_emphasis", 1),
1731a7b12f6SAndy Shevchenko 	/* FIXME these quirks should be removed when AMD NL tapes out */
174cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,disable_scramble_quirk"),
175cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,dis_u3_susphy_quirk"),
176cf48305dSHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
1770eae2fdeSJohn Youn 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
178e6fe66feSFelipe Balbi 	{}
179e6fe66feSFelipe Balbi };
180e6fe66feSFelipe Balbi 
1811abade64SNehal Bakulchandra Shah static const struct property_entry dwc3_pci_mr_properties[] = {
1821abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_STRING("dr_mode", "otg"),
1831abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_BOOL("usb-role-switch"),
1841abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_STRING("role-switch-default-mode", "host"),
1851abade64SNehal Bakulchandra Shah 	PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
1861abade64SNehal Bakulchandra Shah 	{}
1871abade64SNehal Bakulchandra Shah };
1881abade64SNehal Bakulchandra Shah 
189e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_intel_swnode = {
190e492ce9bSHeikki Krogerus 	.properties = dwc3_pci_intel_properties,
191e492ce9bSHeikki Krogerus };
192e492ce9bSHeikki Krogerus 
193582ab24eSHans de Goede static const struct software_node dwc3_pci_intel_phy_charger_detect_swnode = {
194582ab24eSHans de Goede 	.properties = dwc3_pci_intel_phy_charger_detect_properties,
195582ab24eSHans de Goede };
196582ab24eSHans de Goede 
197d7c93a90SHans de Goede static const struct software_node dwc3_pci_intel_byt_swnode = {
198d7c93a90SHans de Goede 	.properties = dwc3_pci_intel_byt_properties,
199d7c93a90SHans de Goede };
200d7c93a90SHans de Goede 
201e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_intel_mrfld_swnode = {
202e492ce9bSHeikki Krogerus 	.properties = dwc3_pci_mrfld_properties,
203e492ce9bSHeikki Krogerus };
204e492ce9bSHeikki Krogerus 
205e492ce9bSHeikki Krogerus static const struct software_node dwc3_pci_amd_swnode = {
206e492ce9bSHeikki Krogerus 	.properties = dwc3_pci_amd_properties,
207e492ce9bSHeikki Krogerus };
208e492ce9bSHeikki Krogerus 
2091abade64SNehal Bakulchandra Shah static const struct software_node dwc3_pci_amd_mr_swnode = {
2101abade64SNehal Bakulchandra Shah 	.properties = dwc3_pci_mr_properties,
2111abade64SNehal Bakulchandra Shah };
2121abade64SNehal Bakulchandra Shah 
dwc3_pci_quirks(struct dwc3_pci * dwc,const struct software_node * swnode)213e285cb40SHans de Goede static int dwc3_pci_quirks(struct dwc3_pci *dwc,
214e285cb40SHans de Goede 			   const struct software_node *swnode)
2151a7b12f6SAndy Shevchenko {
2161a7b12f6SAndy Shevchenko 	struct pci_dev			*pdev = dwc->pci;
217e6fe66feSFelipe Balbi 
2181a7b12f6SAndy Shevchenko 	if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
2199cecca75SFelipe Balbi 		if (pdev->device == PCI_DEVICE_ID_INTEL_BXT ||
220a609ce2aSRaymond Tan 		    pdev->device == PCI_DEVICE_ID_INTEL_BXT_M ||
221457d2285SRaymond Tan 		    pdev->device == PCI_DEVICE_ID_INTEL_EHL) {
22294116f81SAndy Shevchenko 			guid_parse(PCI_INTEL_BXT_DSM_GUID, &dwc->guid);
2239cecca75SFelipe Balbi 			dwc->has_dsm_for_pm = true;
2249cecca75SFelipe Balbi 		}
2259cecca75SFelipe Balbi 
226e6fe66feSFelipe Balbi 		if (pdev->device == PCI_DEVICE_ID_INTEL_BYT) {
227a89d977cSHeikki Krogerus 			struct gpio_desc *gpio;
2280fb782b5SHans de Goede 			const char *bios_ver;
2291a7b12f6SAndy Shevchenko 			int ret;
230a89d977cSHeikki Krogerus 
2317740d04dSHans de Goede 			/* On BYT the FW does not always enable the refclock */
2327740d04dSHans de Goede 			ret = dwc3_byt_enable_ulpi_refclock(pdev);
2337740d04dSHans de Goede 			if (ret)
2347740d04dSHans de Goede 				return ret;
2357740d04dSHans de Goede 
2364a56e413SAndy Shevchenko 			ret = devm_acpi_dev_add_driver_gpios(&pdev->dev,
237a89d977cSHeikki Krogerus 					acpi_dwc3_byt_gpios);
2384a56e413SAndy Shevchenko 			if (ret)
2394a56e413SAndy Shevchenko 				dev_dbg(&pdev->dev, "failed to add mapping table\n");
240a89d977cSHeikki Krogerus 
2412df033caSUwe Kleine-König 			/*
2425741022cSHans de Goede 			 * A lot of BYT devices lack ACPI resource entries for
243b32b8f2bSGratian Crisan 			 * the GPIOs. If the ACPI entry for the GPIO controller
244b32b8f2bSGratian Crisan 			 * is present add a fallback mapping to the reference
2455741022cSHans de Goede 			 * design GPIOs which all boards seem to use.
2465741022cSHans de Goede 			 */
247b32b8f2bSGratian Crisan 			if (acpi_dev_present("INT33FC", NULL, -1))
2485741022cSHans de Goede 				gpiod_add_lookup_table(&platform_bytcr_gpios);
2495741022cSHans de Goede 
2505741022cSHans de Goede 			/*
2512df033caSUwe Kleine-König 			 * These GPIOs will turn on the USB2 PHY. Note that we have to
2522df033caSUwe Kleine-König 			 * put the gpio descriptors again here because the phy driver
2532df033caSUwe Kleine-König 			 * might want to grab them, too.
2542df033caSUwe Kleine-König 			 */
2553004cfd6SStephan Gerhold 			gpio = gpiod_get_optional(&pdev->dev, "cs", GPIOD_OUT_LOW);
2562df033caSUwe Kleine-König 			if (IS_ERR(gpio))
2572df033caSUwe Kleine-König 				return PTR_ERR(gpio);
2582df033caSUwe Kleine-König 
259a89d977cSHeikki Krogerus 			gpiod_set_value_cansleep(gpio, 1);
2603004cfd6SStephan Gerhold 			gpiod_put(gpio);
261a89d977cSHeikki Krogerus 
2623004cfd6SStephan Gerhold 			gpio = gpiod_get_optional(&pdev->dev, "reset", GPIOD_OUT_LOW);
2632df033caSUwe Kleine-König 			if (IS_ERR(gpio))
2642df033caSUwe Kleine-König 				return PTR_ERR(gpio);
2652df033caSUwe Kleine-König 
2662df033caSUwe Kleine-König 			if (gpio) {
267a89d977cSHeikki Krogerus 				gpiod_set_value_cansleep(gpio, 1);
2683004cfd6SStephan Gerhold 				gpiod_put(gpio);
269a89d977cSHeikki Krogerus 				usleep_range(10000, 11000);
270a89d977cSHeikki Krogerus 			}
271582ab24eSHans de Goede 
272582ab24eSHans de Goede 			/*
273a5d847b0SHans de Goede 			 * Make the pdev name predictable (only 1 DWC3 on BYT)
274a5d847b0SHans de Goede 			 * and patch the phy dev-name into the lookup table so
275a5d847b0SHans de Goede 			 * that the phy-driver can get the GPIOs.
276a5d847b0SHans de Goede 			 */
277a5d847b0SHans de Goede 			dwc->dwc3->id = PLATFORM_DEVID_NONE;
278a5d847b0SHans de Goede 			platform_bytcr_gpios.dev_id = "dwc3.ulpi";
279a5d847b0SHans de Goede 
280a5d847b0SHans de Goede 			/*
281582ab24eSHans de Goede 			 * Some Android tablets with a Crystal Cove PMIC
282582ab24eSHans de Goede 			 * (INT33FD), rely on the TUSB1211 phy for charger
283582ab24eSHans de Goede 			 * detection. These can be identified by them _not_
284582ab24eSHans de Goede 			 * using the standard ACPI battery and ac drivers.
285582ab24eSHans de Goede 			 */
2860fb782b5SHans de Goede 			bios_ver = dmi_get_system_info(DMI_BIOS_VERSION);
287582ab24eSHans de Goede 			if (acpi_dev_present("INT33FD", "1", 2) &&
2880fb782b5SHans de Goede 			    acpi_quirk_skip_acpi_ac_and_battery() &&
2890fb782b5SHans de Goede 			    /* Lenovo Yoga Tablet 2 Pro 1380 uses LC824206XA instead */
2900fb782b5SHans de Goede 			    !(bios_ver &&
2910fb782b5SHans de Goede 			      strstarts(bios_ver, "BLADE_21.X64.0005.R00.1504101516"))) {
292582ab24eSHans de Goede 				dev_info(&pdev->dev, "Using TUSB1211 phy for charger detection\n");
293582ab24eSHans de Goede 				swnode = &dwc3_pci_intel_phy_charger_detect_swnode;
294582ab24eSHans de Goede 			}
295a89d977cSHeikki Krogerus 		}
296e6fe66feSFelipe Balbi 	}
297a89d977cSHeikki Krogerus 
298e285cb40SHans de Goede 	return device_add_software_node(&dwc->dwc3->dev, swnode);
2992cd9ddf7SHeikki Krogerus }
30072246da4SFelipe Balbi 
3018eed00b2SManu Gautam #ifdef CONFIG_PM
dwc3_pci_resume_work(struct work_struct * work)3028eed00b2SManu Gautam static void dwc3_pci_resume_work(struct work_struct *work)
3038eed00b2SManu Gautam {
3048eed00b2SManu Gautam 	struct dwc3_pci *dwc = container_of(work, struct dwc3_pci, wakeup_work);
3058eed00b2SManu Gautam 	struct platform_device *dwc3 = dwc->dwc3;
3068eed00b2SManu Gautam 	int ret;
3078eed00b2SManu Gautam 
3088eed00b2SManu Gautam 	ret = pm_runtime_get_sync(&dwc3->dev);
309a03e2ddaSZheng Yongjun 	if (ret < 0) {
3102655971aSAditya Pakki 		pm_runtime_put_sync_autosuspend(&dwc3->dev);
3118eed00b2SManu Gautam 		return;
3122655971aSAditya Pakki 	}
3138eed00b2SManu Gautam 
3148eed00b2SManu Gautam 	pm_runtime_mark_last_busy(&dwc3->dev);
3158eed00b2SManu Gautam 	pm_runtime_put_sync_autosuspend(&dwc3->dev);
3168eed00b2SManu Gautam }
3178eed00b2SManu Gautam #endif
3188eed00b2SManu Gautam 
dwc3_pci_probe(struct pci_dev * pci,const struct pci_device_id * id)3191a7b12f6SAndy Shevchenko static int dwc3_pci_probe(struct pci_dev *pci, const struct pci_device_id *id)
32072246da4SFelipe Balbi {
3210f817ae6SFelipe Balbi 	struct dwc3_pci		*dwc;
32272246da4SFelipe Balbi 	struct resource		res[2];
323b09e99eeSAndy Shevchenko 	int			ret;
324802ca850SChanho Park 	struct device		*dev = &pci->dev;
32572246da4SFelipe Balbi 
326f1c7e710SAndy Shevchenko 	ret = pcim_enable_device(pci);
32772246da4SFelipe Balbi 	if (ret) {
328802ca850SChanho Park 		dev_err(dev, "failed to enable pci device\n");
329802ca850SChanho Park 		return -ENODEV;
33072246da4SFelipe Balbi 	}
33172246da4SFelipe Balbi 
33272246da4SFelipe Balbi 	pci_set_master(pci);
33372246da4SFelipe Balbi 
3340f817ae6SFelipe Balbi 	dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
3350f817ae6SFelipe Balbi 	if (!dwc)
336f1c7e710SAndy Shevchenko 		return -ENOMEM;
3370f817ae6SFelipe Balbi 
3380f817ae6SFelipe Balbi 	dwc->dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO);
3390f817ae6SFelipe Balbi 	if (!dwc->dwc3)
3400f817ae6SFelipe Balbi 		return -ENOMEM;
34172246da4SFelipe Balbi 
34272246da4SFelipe Balbi 	memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
34372246da4SFelipe Balbi 
34472246da4SFelipe Balbi 	res[0].start	= pci_resource_start(pci, 0);
34572246da4SFelipe Balbi 	res[0].end	= pci_resource_end(pci, 0);
34672246da4SFelipe Balbi 	res[0].name	= "dwc_usb3";
34772246da4SFelipe Balbi 	res[0].flags	= IORESOURCE_MEM;
34872246da4SFelipe Balbi 
34972246da4SFelipe Balbi 	res[1].start	= pci->irq;
35072246da4SFelipe Balbi 	res[1].name	= "dwc_usb3";
35172246da4SFelipe Balbi 	res[1].flags	= IORESOURCE_IRQ;
35272246da4SFelipe Balbi 
3530f817ae6SFelipe Balbi 	ret = platform_device_add_resources(dwc->dwc3, res, ARRAY_SIZE(res));
35472246da4SFelipe Balbi 	if (ret) {
355802ca850SChanho Park 		dev_err(dev, "couldn't add resources to dwc3 device\n");
356cabdf83dSThinh Nguyen 		goto err;
35772246da4SFelipe Balbi 	}
35872246da4SFelipe Balbi 
3590f817ae6SFelipe Balbi 	dwc->pci = pci;
3600f817ae6SFelipe Balbi 	dwc->dwc3->dev.parent = dev;
3610f817ae6SFelipe Balbi 	ACPI_COMPANION_SET(&dwc->dwc3->dev, ACPI_COMPANION(dev));
36272246da4SFelipe Balbi 
363e285cb40SHans de Goede 	ret = dwc3_pci_quirks(dwc, (void *)id->driver_data);
364474799f0SHeikki Krogerus 	if (ret)
365474799f0SHeikki Krogerus 		goto err;
366474799f0SHeikki Krogerus 
3670f817ae6SFelipe Balbi 	ret = platform_device_add(dwc->dwc3);
36872246da4SFelipe Balbi 	if (ret) {
369802ca850SChanho Park 		dev_err(dev, "failed to register dwc3 device\n");
3702cd9ddf7SHeikki Krogerus 		goto err;
37172246da4SFelipe Balbi 	}
37272246da4SFelipe Balbi 
373e9af9229SFelipe Balbi 	device_init_wakeup(dev, true);
3740f817ae6SFelipe Balbi 	pci_set_drvdata(pci, dwc);
375e9af9229SFelipe Balbi 	pm_runtime_put(dev);
3768eed00b2SManu Gautam #ifdef CONFIG_PM
3778eed00b2SManu Gautam 	INIT_WORK(&dwc->wakeup_work, dwc3_pci_resume_work);
3788eed00b2SManu Gautam #endif
379e9af9229SFelipe Balbi 
38072246da4SFelipe Balbi 	return 0;
3812cd9ddf7SHeikki Krogerus err:
382e492ce9bSHeikki Krogerus 	device_remove_software_node(&dwc->dwc3->dev);
3830f817ae6SFelipe Balbi 	platform_device_put(dwc->dwc3);
38472246da4SFelipe Balbi 	return ret;
38572246da4SFelipe Balbi }
38672246da4SFelipe Balbi 
dwc3_pci_remove(struct pci_dev * pci)387fb4e98abSBill Pemberton static void dwc3_pci_remove(struct pci_dev *pci)
38872246da4SFelipe Balbi {
3890f817ae6SFelipe Balbi 	struct dwc3_pci		*dwc = pci_get_drvdata(pci);
3907b412b04SKuppuswamy Sathyanarayanan 	struct pci_dev		*pdev = dwc->pci;
3910f817ae6SFelipe Balbi 
3927b412b04SKuppuswamy Sathyanarayanan 	if (pdev->device == PCI_DEVICE_ID_INTEL_BYT)
3935741022cSHans de Goede 		gpiod_remove_lookup_table(&platform_bytcr_gpios);
3948eed00b2SManu Gautam #ifdef CONFIG_PM
3958eed00b2SManu Gautam 	cancel_work_sync(&dwc->wakeup_work);
3968eed00b2SManu Gautam #endif
397e9af9229SFelipe Balbi 	device_init_wakeup(&pci->dev, false);
398e9af9229SFelipe Balbi 	pm_runtime_get(&pci->dev);
399e492ce9bSHeikki Krogerus 	device_remove_software_node(&dwc->dwc3->dev);
4000f817ae6SFelipe Balbi 	platform_device_unregister(dwc->dwc3);
40172246da4SFelipe Balbi }
40272246da4SFelipe Balbi 
403782df20cSJingoo Han static const struct pci_device_id dwc3_pci_id_table[] = {
404917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, BSW, &dwc3_pci_intel_swnode) },
405917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, BYT, &dwc3_pci_intel_byt_swnode) },
406917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, MRFLD, &dwc3_pci_intel_mrfld_swnode) },
407917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, CMLLP, &dwc3_pci_intel_swnode) },
408917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, CMLH, &dwc3_pci_intel_swnode) },
409917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, SPTLP, &dwc3_pci_intel_swnode) },
410917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, SPTH, &dwc3_pci_intel_swnode) },
411917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, BXT, &dwc3_pci_intel_swnode) },
412917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, BXT_M, &dwc3_pci_intel_swnode) },
413917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, APL, &dwc3_pci_intel_swnode) },
414917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, KBP, &dwc3_pci_intel_swnode) },
415917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, GLK, &dwc3_pci_intel_swnode) },
416917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, CNPLP, &dwc3_pci_intel_swnode) },
417917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, CNPH, &dwc3_pci_intel_swnode) },
418917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, CNPV, &dwc3_pci_intel_swnode) },
419917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, ICLLP, &dwc3_pci_intel_swnode) },
420917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, EHL, &dwc3_pci_intel_swnode) },
421917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, TGPLP, &dwc3_pci_intel_swnode) },
422917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, TGPH, &dwc3_pci_intel_swnode) },
423917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, JSP, &dwc3_pci_intel_swnode) },
424917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, ADL, &dwc3_pci_intel_swnode) },
425917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, ADL_PCH, &dwc3_pci_intel_swnode) },
426917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, ADLN, &dwc3_pci_intel_swnode) },
427917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, ADLN_PCH, &dwc3_pci_intel_swnode) },
428917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, ADLS, &dwc3_pci_intel_swnode) },
429917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, RPL, &dwc3_pci_intel_swnode) },
430917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, RPLS, &dwc3_pci_intel_swnode) },
431917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, MTLM, &dwc3_pci_intel_swnode) },
432917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, MTLP, &dwc3_pci_intel_swnode) },
433917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, MTL, &dwc3_pci_intel_swnode) },
4348e86652eSGreg Kroah-Hartman 	{ PCI_DEVICE_DATA(INTEL, MTLS, &dwc3_pci_intel_swnode) },
435de4b5b28SHeikki Krogerus 	{ PCI_DEVICE_DATA(INTEL, ARLH_PCH, &dwc3_pci_intel_swnode) },
436917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(INTEL, TGL, &dwc3_pci_intel_swnode) },
437*2bf35ea4SHeikki Krogerus 	{ PCI_DEVICE_DATA(INTEL, PTLH, &dwc3_pci_intel_swnode) },
438*2bf35ea4SHeikki Krogerus 	{ PCI_DEVICE_DATA(INTEL, PTLH_PCH, &dwc3_pci_intel_swnode) },
439*2bf35ea4SHeikki Krogerus 	{ PCI_DEVICE_DATA(INTEL, PTLU, &dwc3_pci_intel_swnode) },
440*2bf35ea4SHeikki Krogerus 	{ PCI_DEVICE_DATA(INTEL, PTLU_PCH, &dwc3_pci_intel_swnode) },
4411a7b12f6SAndy Shevchenko 
442917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(AMD, NL_USB, &dwc3_pci_amd_swnode) },
443917dc99bSAndy Shevchenko 	{ PCI_DEVICE_DATA(AMD, MR, &dwc3_pci_amd_mr_swnode) },
4441abade64SNehal Bakulchandra Shah 
44572246da4SFelipe Balbi 	{  }	/* Terminating Entry */
44672246da4SFelipe Balbi };
44772246da4SFelipe Balbi MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
44872246da4SFelipe Balbi 
44936daf3aaSFelipe Balbi #if defined(CONFIG_PM) || defined(CONFIG_PM_SLEEP)
dwc3_pci_dsm(struct dwc3_pci * dwc,int param)45036daf3aaSFelipe Balbi static int dwc3_pci_dsm(struct dwc3_pci *dwc, int param)
45136daf3aaSFelipe Balbi {
45236daf3aaSFelipe Balbi 	union acpi_object *obj;
45336daf3aaSFelipe Balbi 	union acpi_object tmp;
45436daf3aaSFelipe Balbi 	union acpi_object argv4 = ACPI_INIT_DSM_ARGV4(1, &tmp);
45536daf3aaSFelipe Balbi 
45636daf3aaSFelipe Balbi 	if (!dwc->has_dsm_for_pm)
45736daf3aaSFelipe Balbi 		return 0;
45836daf3aaSFelipe Balbi 
45936daf3aaSFelipe Balbi 	tmp.type = ACPI_TYPE_INTEGER;
46036daf3aaSFelipe Balbi 	tmp.integer.value = param;
46136daf3aaSFelipe Balbi 
46294116f81SAndy Shevchenko 	obj = acpi_evaluate_dsm(ACPI_HANDLE(&dwc->pci->dev), &dwc->guid,
46336daf3aaSFelipe Balbi 			1, PCI_INTEL_BXT_FUNC_PMU_PWR, &argv4);
46436daf3aaSFelipe Balbi 	if (!obj) {
46536daf3aaSFelipe Balbi 		dev_err(&dwc->pci->dev, "failed to evaluate _DSM\n");
46636daf3aaSFelipe Balbi 		return -EIO;
46736daf3aaSFelipe Balbi 	}
46836daf3aaSFelipe Balbi 
46936daf3aaSFelipe Balbi 	ACPI_FREE(obj);
47036daf3aaSFelipe Balbi 
47136daf3aaSFelipe Balbi 	return 0;
47236daf3aaSFelipe Balbi }
47336daf3aaSFelipe Balbi #endif /* CONFIG_PM || CONFIG_PM_SLEEP */
47436daf3aaSFelipe Balbi 
475e9af9229SFelipe Balbi #ifdef CONFIG_PM
dwc3_pci_runtime_suspend(struct device * dev)476e9af9229SFelipe Balbi static int dwc3_pci_runtime_suspend(struct device *dev)
477e9af9229SFelipe Balbi {
4789cecca75SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
4799cecca75SFelipe Balbi 
480de3ef1ebSRafael J. Wysocki 	if (device_can_wakeup(dev))
4819cecca75SFelipe Balbi 		return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D3);
482e9af9229SFelipe Balbi 
483e9af9229SFelipe Balbi 	return -EBUSY;
484e9af9229SFelipe Balbi }
485e9af9229SFelipe Balbi 
dwc3_pci_runtime_resume(struct device * dev)486f6c274e1SFelipe Balbi static int dwc3_pci_runtime_resume(struct device *dev)
487f6c274e1SFelipe Balbi {
4880f817ae6SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
4899cecca75SFelipe Balbi 	int			ret;
4909cecca75SFelipe Balbi 
4919cecca75SFelipe Balbi 	ret = dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D0);
4929cecca75SFelipe Balbi 	if (ret)
4939cecca75SFelipe Balbi 		return ret;
494f6c274e1SFelipe Balbi 
4958eed00b2SManu Gautam 	queue_work(pm_wq, &dwc->wakeup_work);
4968eed00b2SManu Gautam 
4978eed00b2SManu Gautam 	return 0;
498f6c274e1SFelipe Balbi }
499696118c0SFelipe Balbi #endif /* CONFIG_PM */
500f6c274e1SFelipe Balbi 
501696118c0SFelipe Balbi #ifdef CONFIG_PM_SLEEP
dwc3_pci_suspend(struct device * dev)5029cecca75SFelipe Balbi static int dwc3_pci_suspend(struct device *dev)
503e9af9229SFelipe Balbi {
5049cecca75SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
5059cecca75SFelipe Balbi 
5069cecca75SFelipe Balbi 	return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D3);
5079cecca75SFelipe Balbi }
5089cecca75SFelipe Balbi 
dwc3_pci_resume(struct device * dev)5099cecca75SFelipe Balbi static int dwc3_pci_resume(struct device *dev)
5109cecca75SFelipe Balbi {
5119cecca75SFelipe Balbi 	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
5129cecca75SFelipe Balbi 
5139cecca75SFelipe Balbi 	return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D0);
514e9af9229SFelipe Balbi }
515696118c0SFelipe Balbi #endif /* CONFIG_PM_SLEEP */
516e9af9229SFelipe Balbi 
51795aa932cSDoug Wilson static const struct dev_pm_ops dwc3_pci_dev_pm_ops = {
5189cecca75SFelipe Balbi 	SET_SYSTEM_SLEEP_PM_OPS(dwc3_pci_suspend, dwc3_pci_resume)
519f6c274e1SFelipe Balbi 	SET_RUNTIME_PM_OPS(dwc3_pci_runtime_suspend, dwc3_pci_runtime_resume,
520e9af9229SFelipe Balbi 		NULL)
521e9af9229SFelipe Balbi };
522e9af9229SFelipe Balbi 
52372246da4SFelipe Balbi static struct pci_driver dwc3_pci_driver = {
5240949e99bSFelipe Balbi 	.name		= "dwc3-pci",
52572246da4SFelipe Balbi 	.id_table	= dwc3_pci_id_table,
52672246da4SFelipe Balbi 	.probe		= dwc3_pci_probe,
5277690417dSBill Pemberton 	.remove		= dwc3_pci_remove,
528e9af9229SFelipe Balbi 	.driver		= {
529e9af9229SFelipe Balbi 		.pm	= &dwc3_pci_dev_pm_ops,
530e9af9229SFelipe Balbi 	}
53172246da4SFelipe Balbi };
53272246da4SFelipe Balbi 
53372246da4SFelipe Balbi MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
5345945f789SFelipe Balbi MODULE_LICENSE("GPL v2");
53572246da4SFelipe Balbi MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
53672246da4SFelipe Balbi 
53795656336SGreg Kroah-Hartman module_pci_driver(dwc3_pci_driver);
538