lkdtm: Add Control Flow Integrity test
[linux/fpc-iii.git] / drivers / usb / roles / intel-xhci-usb-role-switch.c
blob277de96181f9a056091aaca586831ece879f1d60
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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.
11 * Author: Wu, Hao
14 #include <linux/acpi.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/io.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;
39 void __iomem *base;
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;
46 acpi_status status;
47 u32 glk, val;
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");
57 return -EIO;
60 pm_runtime_get_sync(dev);
62 /* Set idpin value as requested */
63 val = readl(data->base + DUAL_ROLE_CFG0);
64 switch (role) {
65 case USB_ROLE_NONE:
66 val |= SW_IDPIN;
67 val &= ~SW_VBUS_VALID;
68 break;
69 case USB_ROLE_HOST:
70 val &= ~SW_IDPIN;
71 val &= ~SW_VBUS_VALID;
72 break;
73 case USB_ROLE_DEVICE:
74 val |= SW_IDPIN;
75 val |= SW_VBUS_VALID;
76 break;
78 val |= SW_IDPIN_EN;
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.*/
88 do {
89 val = readl(data->base + DUAL_ROLE_CFG1);
90 if (!!(val & HOST_MODE) == (role == USB_ROLE_HOST)) {
91 pm_runtime_put(dev);
92 return 0;
95 /* Interval for polling is set to about 5 - 10 ms */
96 usleep_range(5000, 10000);
97 } while (time_before(jiffies, timeout));
99 pm_runtime_put(dev);
101 dev_warn(dev, "Timeout waiting for role-switch\n");
102 return -ETIMEDOUT;
105 static enum usb_role intel_xhci_usb_get_role(struct device *dev)
107 struct intel_xhci_usb_data *data = dev_get_drvdata(dev);
108 enum usb_role role;
109 u32 val;
111 pm_runtime_get_sync(dev);
112 val = readl(data->base + DUAL_ROLE_CFG0);
113 pm_runtime_put(dev);
115 if (!(val & SW_IDPIN))
116 role = USB_ROLE_HOST;
117 else if (val & SW_VBUS_VALID)
118 role = USB_ROLE_DEVICE;
119 else
120 role = USB_ROLE_NONE;
122 return role;
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);
138 if (!data)
139 return -ENOMEM;
141 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
142 if (!res)
143 return -EINVAL;
144 data->base = devm_ioremap_nocache(dev, res->start, resource_size(res));
145 if (!data->base)
146 return -ENOMEM;
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);
157 return 0;
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);
167 return 0;
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 = {
177 .driver = {
178 .name = DRV_NAME,
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");