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/property.h>
23 #include <linux/usb/role.h>
25 /* register definition */
26 #define DUAL_ROLE_CFG0 0x68
27 #define SW_VBUS_VALID BIT(24)
28 #define SW_IDPIN_EN BIT(21)
29 #define SW_IDPIN BIT(20)
30 #define SW_SWITCH_EN BIT(16)
32 #define DRD_CONFIG_DYNAMIC 0
33 #define DRD_CONFIG_STATIC_HOST 1
34 #define DRD_CONFIG_STATIC_DEVICE 2
35 #define DRD_CONFIG_MASK 3
37 #define DUAL_ROLE_CFG1 0x6c
38 #define HOST_MODE BIT(29)
40 #define DUAL_ROLE_CFG1_POLL_TIMEOUT 1000
42 #define DRV_NAME "intel_xhci_usb_sw"
44 struct intel_xhci_usb_data {
46 struct usb_role_switch *role_sw;
48 bool enable_sw_switch;
51 static const struct software_node intel_xhci_usb_node = {
55 static int intel_xhci_usb_set_role(struct usb_role_switch *sw,
58 struct intel_xhci_usb_data *data = usb_role_switch_get_drvdata(sw);
59 unsigned long timeout;
62 u32 drd_config = DRD_CONFIG_DYNAMIC;
65 * On many CHT devices ACPI event (_AEI) handlers read / modify /
66 * write the cfg0 register, just like we do. Take the ACPI lock
67 * to avoid us racing with the AML code.
69 status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk);
70 if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) {
71 dev_err(data->dev, "Error could not acquire lock\n");
75 pm_runtime_get_sync(data->dev);
78 * Set idpin value as requested.
79 * Since some devices rely on firmware setting DRD_CONFIG and
80 * SW_SWITCH_EN bits to be zero for role switch,
81 * do not set these bits for those devices.
83 val = readl(data->base + DUAL_ROLE_CFG0);
87 val &= ~SW_VBUS_VALID;
88 drd_config = DRD_CONFIG_DYNAMIC;
92 val &= ~SW_VBUS_VALID;
93 drd_config = DRD_CONFIG_STATIC_HOST;
98 drd_config = DRD_CONFIG_STATIC_DEVICE;
102 if (data->enable_sw_switch) {
103 val &= ~DRD_CONFIG_MASK;
104 val |= SW_SWITCH_EN | drd_config;
106 writel(val, data->base + DUAL_ROLE_CFG0);
108 acpi_release_global_lock(glk);
110 /* In most case it takes about 600ms to finish mode switching */
111 timeout = jiffies + msecs_to_jiffies(DUAL_ROLE_CFG1_POLL_TIMEOUT);
113 /* Polling on CFG1 register to confirm mode switch.*/
115 val = readl(data->base + DUAL_ROLE_CFG1);
116 if (!!(val & HOST_MODE) == (role == USB_ROLE_HOST)) {
117 pm_runtime_put(data->dev);
121 /* Interval for polling is set to about 5 - 10 ms */
122 usleep_range(5000, 10000);
123 } while (time_before(jiffies, timeout));
125 pm_runtime_put(data->dev);
127 dev_warn(data->dev, "Timeout waiting for role-switch\n");
131 static enum usb_role intel_xhci_usb_get_role(struct usb_role_switch *sw)
133 struct intel_xhci_usb_data *data = usb_role_switch_get_drvdata(sw);
137 pm_runtime_get_sync(data->dev);
138 val = readl(data->base + DUAL_ROLE_CFG0);
139 pm_runtime_put(data->dev);
141 if (!(val & SW_IDPIN))
142 role = USB_ROLE_HOST;
143 else if (val & SW_VBUS_VALID)
144 role = USB_ROLE_DEVICE;
146 role = USB_ROLE_NONE;
151 static int intel_xhci_usb_probe(struct platform_device *pdev)
153 struct usb_role_switch_desc sw_desc = { };
154 struct device *dev = &pdev->dev;
155 struct intel_xhci_usb_data *data;
156 struct resource *res;
159 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
163 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
166 data->base = devm_ioremap(dev, res->start, resource_size(res));
170 platform_set_drvdata(pdev, data);
172 ret = software_node_register(&intel_xhci_usb_node);
176 sw_desc.set = intel_xhci_usb_set_role,
177 sw_desc.get = intel_xhci_usb_get_role,
178 sw_desc.allow_userspace_control = true,
179 sw_desc.fwnode = software_node_fwnode(&intel_xhci_usb_node);
180 sw_desc.driver_data = data;
183 data->enable_sw_switch = !device_property_read_bool(dev,
184 "sw_switch_disable");
186 data->role_sw = usb_role_switch_register(dev, &sw_desc);
187 if (IS_ERR(data->role_sw)) {
188 fwnode_handle_put(sw_desc.fwnode);
189 return PTR_ERR(data->role_sw);
192 pm_runtime_set_active(dev);
193 pm_runtime_enable(dev);
198 static int intel_xhci_usb_remove(struct platform_device *pdev)
200 struct intel_xhci_usb_data *data = platform_get_drvdata(pdev);
202 pm_runtime_disable(&pdev->dev);
204 usb_role_switch_unregister(data->role_sw);
205 fwnode_handle_put(software_node_fwnode(&intel_xhci_usb_node));
210 static const struct platform_device_id intel_xhci_usb_table[] = {
211 { .name = DRV_NAME },
214 MODULE_DEVICE_TABLE(platform, intel_xhci_usb_table);
216 static struct platform_driver intel_xhci_usb_driver = {
220 .id_table = intel_xhci_usb_table,
221 .probe = intel_xhci_usb_probe,
222 .remove = intel_xhci_usb_remove,
225 module_platform_driver(intel_xhci_usb_driver);
227 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
228 MODULE_DESCRIPTION("Intel XHCI USB role switch driver");
229 MODULE_LICENSE("GPL");