1 // SPDX-License-Identifier: GPL-2.0+
3 * Intel XHCI (Cherry Trail, Broxton and others) USB OTG role switch driver
5 * Copyright (c) 2016-2017 Hans de Goede <hdegoede@redhat.com>
7 * Loosely based on android x86 kernel code which is:
9 * Copyright (C) 2014 Intel Corp.
14 #include <linux/acpi.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/usb/role.h>
24 /* register definition */
25 #define DUAL_ROLE_CFG0 0x68
26 #define SW_VBUS_VALID BIT(24)
27 #define SW_IDPIN_EN BIT(21)
28 #define SW_IDPIN BIT(20)
30 #define DUAL_ROLE_CFG1 0x6c
31 #define HOST_MODE BIT(29)
33 #define DUAL_ROLE_CFG1_POLL_TIMEOUT 1000
35 #define DRV_NAME "intel_xhci_usb_sw"
37 struct intel_xhci_usb_data
{
38 struct usb_role_switch
*role_sw
;
42 static int intel_xhci_usb_set_role(struct device
*dev
, enum usb_role role
)
44 struct intel_xhci_usb_data
*data
= dev_get_drvdata(dev
);
45 unsigned long timeout
;
50 * On many CHT devices ACPI event (_AEI) handlers read / modify /
51 * write the cfg0 register, just like we do. Take the ACPI lock
52 * to avoid us racing with the AML code.
54 status
= acpi_acquire_global_lock(ACPI_WAIT_FOREVER
, &glk
);
55 if (ACPI_FAILURE(status
) && status
!= AE_NOT_CONFIGURED
) {
56 dev_err(dev
, "Error could not acquire lock\n");
60 pm_runtime_get_sync(dev
);
62 /* Set idpin value as requested */
63 val
= readl(data
->base
+ DUAL_ROLE_CFG0
);
67 val
&= ~SW_VBUS_VALID
;
71 val
&= ~SW_VBUS_VALID
;
80 writel(val
, data
->base
+ DUAL_ROLE_CFG0
);
82 acpi_release_global_lock(glk
);
84 /* In most case it takes about 600ms to finish mode switching */
85 timeout
= jiffies
+ msecs_to_jiffies(DUAL_ROLE_CFG1_POLL_TIMEOUT
);
87 /* Polling on CFG1 register to confirm mode switch.*/
89 val
= readl(data
->base
+ DUAL_ROLE_CFG1
);
90 if (!!(val
& HOST_MODE
) == (role
== USB_ROLE_HOST
)) {
95 /* Interval for polling is set to about 5 - 10 ms */
96 usleep_range(5000, 10000);
97 } while (time_before(jiffies
, timeout
));
101 dev_warn(dev
, "Timeout waiting for role-switch\n");
105 static enum usb_role
intel_xhci_usb_get_role(struct device
*dev
)
107 struct intel_xhci_usb_data
*data
= dev_get_drvdata(dev
);
111 pm_runtime_get_sync(dev
);
112 val
= readl(data
->base
+ DUAL_ROLE_CFG0
);
115 if (!(val
& SW_IDPIN
))
116 role
= USB_ROLE_HOST
;
117 else if (val
& SW_VBUS_VALID
)
118 role
= USB_ROLE_DEVICE
;
120 role
= USB_ROLE_NONE
;
125 static const struct usb_role_switch_desc sw_desc
= {
126 .set
= intel_xhci_usb_set_role
,
127 .get
= intel_xhci_usb_get_role
,
128 .allow_userspace_control
= true,
131 static int intel_xhci_usb_probe(struct platform_device
*pdev
)
133 struct device
*dev
= &pdev
->dev
;
134 struct intel_xhci_usb_data
*data
;
135 struct resource
*res
;
137 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
141 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
144 data
->base
= devm_ioremap_nocache(dev
, res
->start
, resource_size(res
));
148 platform_set_drvdata(pdev
, data
);
150 data
->role_sw
= usb_role_switch_register(dev
, &sw_desc
);
151 if (IS_ERR(data
->role_sw
))
152 return PTR_ERR(data
->role_sw
);
154 pm_runtime_set_active(dev
);
155 pm_runtime_enable(dev
);
160 static int intel_xhci_usb_remove(struct platform_device
*pdev
)
162 struct intel_xhci_usb_data
*data
= platform_get_drvdata(pdev
);
164 pm_runtime_disable(&pdev
->dev
);
166 usb_role_switch_unregister(data
->role_sw
);
170 static const struct platform_device_id intel_xhci_usb_table
[] = {
171 { .name
= DRV_NAME
},
174 MODULE_DEVICE_TABLE(platform
, intel_xhci_usb_table
);
176 static struct platform_driver intel_xhci_usb_driver
= {
180 .id_table
= intel_xhci_usb_table
,
181 .probe
= intel_xhci_usb_probe
,
182 .remove
= intel_xhci_usb_remove
,
185 module_platform_driver(intel_xhci_usb_driver
);
187 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
188 MODULE_DESCRIPTION("Intel XHCI USB role switch driver");
189 MODULE_LICENSE("GPL");