intel-xhci-usb-role-switch.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Intel XHCI (Cherry Trail, Broxton and others) USB OTG role switch driver
  4. *
  5. * Copyright (c) 2016-2017 Hans de Goede <[email protected]>
  6. *
  7. * Loosely based on android x86 kernel code which is:
  8. *
  9. * Copyright (C) 2014 Intel Corp.
  10. *
  11. * Author: Wu, Hao
  12. */
  13. #include <linux/acpi.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/property.h>
  22. #include <linux/usb/role.h>
  23. /* register definition */
  24. #define DUAL_ROLE_CFG0 0x68
  25. #define SW_VBUS_VALID BIT(24)
  26. #define SW_IDPIN_EN BIT(21)
  27. #define SW_IDPIN BIT(20)
  28. #define SW_SWITCH_EN BIT(16)
  29. #define DRD_CONFIG_DYNAMIC 0
  30. #define DRD_CONFIG_STATIC_HOST 1
  31. #define DRD_CONFIG_STATIC_DEVICE 2
  32. #define DRD_CONFIG_MASK 3
  33. #define DUAL_ROLE_CFG1 0x6c
  34. #define HOST_MODE BIT(29)
  35. #define DUAL_ROLE_CFG1_POLL_TIMEOUT 1000
  36. #define DRV_NAME "intel_xhci_usb_sw"
  37. struct intel_xhci_usb_data {
  38. struct device *dev;
  39. struct usb_role_switch *role_sw;
  40. void __iomem *base;
  41. bool enable_sw_switch;
  42. };
  43. static const struct software_node intel_xhci_usb_node = {
  44. "intel-xhci-usb-sw",
  45. };
  46. static int intel_xhci_usb_set_role(struct usb_role_switch *sw,
  47. enum usb_role role)
  48. {
  49. struct intel_xhci_usb_data *data = usb_role_switch_get_drvdata(sw);
  50. unsigned long timeout;
  51. acpi_status status;
  52. u32 glk, val;
  53. u32 drd_config = DRD_CONFIG_DYNAMIC;
  54. /*
  55. * On many CHT devices ACPI event (_AEI) handlers read / modify /
  56. * write the cfg0 register, just like we do. Take the ACPI lock
  57. * to avoid us racing with the AML code.
  58. */
  59. status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk);
  60. if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) {
  61. dev_err(data->dev, "Error could not acquire lock\n");
  62. return -EIO;
  63. }
  64. pm_runtime_get_sync(data->dev);
  65. /*
  66. * Set idpin value as requested.
  67. * Since some devices rely on firmware setting DRD_CONFIG and
  68. * SW_SWITCH_EN bits to be zero for role switch,
  69. * do not set these bits for those devices.
  70. */
  71. val = readl(data->base + DUAL_ROLE_CFG0);
  72. switch (role) {
  73. case USB_ROLE_NONE:
  74. val |= SW_IDPIN;
  75. val &= ~SW_VBUS_VALID;
  76. drd_config = DRD_CONFIG_DYNAMIC;
  77. break;
  78. case USB_ROLE_HOST:
  79. val &= ~SW_IDPIN;
  80. val &= ~SW_VBUS_VALID;
  81. drd_config = DRD_CONFIG_STATIC_HOST;
  82. break;
  83. case USB_ROLE_DEVICE:
  84. val |= SW_IDPIN;
  85. val |= SW_VBUS_VALID;
  86. drd_config = DRD_CONFIG_STATIC_DEVICE;
  87. break;
  88. }
  89. val |= SW_IDPIN_EN;
  90. if (data->enable_sw_switch) {
  91. val &= ~DRD_CONFIG_MASK;
  92. val |= SW_SWITCH_EN | drd_config;
  93. }
  94. writel(val, data->base + DUAL_ROLE_CFG0);
  95. acpi_release_global_lock(glk);
  96. /* In most case it takes about 600ms to finish mode switching */
  97. timeout = jiffies + msecs_to_jiffies(DUAL_ROLE_CFG1_POLL_TIMEOUT);
  98. /* Polling on CFG1 register to confirm mode switch.*/
  99. do {
  100. val = readl(data->base + DUAL_ROLE_CFG1);
  101. if (!!(val & HOST_MODE) == (role == USB_ROLE_HOST)) {
  102. pm_runtime_put(data->dev);
  103. return 0;
  104. }
  105. /* Interval for polling is set to about 5 - 10 ms */
  106. usleep_range(5000, 10000);
  107. } while (time_before(jiffies, timeout));
  108. pm_runtime_put(data->dev);
  109. dev_warn(data->dev, "Timeout waiting for role-switch\n");
  110. return -ETIMEDOUT;
  111. }
  112. static enum usb_role intel_xhci_usb_get_role(struct usb_role_switch *sw)
  113. {
  114. struct intel_xhci_usb_data *data = usb_role_switch_get_drvdata(sw);
  115. enum usb_role role;
  116. u32 val;
  117. pm_runtime_get_sync(data->dev);
  118. val = readl(data->base + DUAL_ROLE_CFG0);
  119. pm_runtime_put(data->dev);
  120. if (!(val & SW_IDPIN))
  121. role = USB_ROLE_HOST;
  122. else if (val & SW_VBUS_VALID)
  123. role = USB_ROLE_DEVICE;
  124. else
  125. role = USB_ROLE_NONE;
  126. return role;
  127. }
  128. static int intel_xhci_usb_probe(struct platform_device *pdev)
  129. {
  130. struct usb_role_switch_desc sw_desc = { };
  131. struct device *dev = &pdev->dev;
  132. struct intel_xhci_usb_data *data;
  133. struct resource *res;
  134. int ret;
  135. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  136. if (!data)
  137. return -ENOMEM;
  138. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  139. if (!res)
  140. return -EINVAL;
  141. data->base = devm_ioremap(dev, res->start, resource_size(res));
  142. if (!data->base)
  143. return -ENOMEM;
  144. platform_set_drvdata(pdev, data);
  145. ret = software_node_register(&intel_xhci_usb_node);
  146. if (ret)
  147. return ret;
  148. sw_desc.set = intel_xhci_usb_set_role,
  149. sw_desc.get = intel_xhci_usb_get_role,
  150. sw_desc.allow_userspace_control = true,
  151. sw_desc.fwnode = software_node_fwnode(&intel_xhci_usb_node);
  152. sw_desc.driver_data = data;
  153. data->dev = dev;
  154. data->enable_sw_switch = !device_property_read_bool(dev,
  155. "sw_switch_disable");
  156. data->role_sw = usb_role_switch_register(dev, &sw_desc);
  157. if (IS_ERR(data->role_sw)) {
  158. fwnode_handle_put(sw_desc.fwnode);
  159. return PTR_ERR(data->role_sw);
  160. }
  161. pm_runtime_set_active(dev);
  162. pm_runtime_enable(dev);
  163. return 0;
  164. }
  165. static int intel_xhci_usb_remove(struct platform_device *pdev)
  166. {
  167. struct intel_xhci_usb_data *data = platform_get_drvdata(pdev);
  168. pm_runtime_disable(&pdev->dev);
  169. usb_role_switch_unregister(data->role_sw);
  170. fwnode_handle_put(software_node_fwnode(&intel_xhci_usb_node));
  171. return 0;
  172. }
  173. static const struct platform_device_id intel_xhci_usb_table[] = {
  174. { .name = DRV_NAME },
  175. {}
  176. };
  177. MODULE_DEVICE_TABLE(platform, intel_xhci_usb_table);
  178. static struct platform_driver intel_xhci_usb_driver = {
  179. .driver = {
  180. .name = DRV_NAME,
  181. },
  182. .id_table = intel_xhci_usb_table,
  183. .probe = intel_xhci_usb_probe,
  184. .remove = intel_xhci_usb_remove,
  185. };
  186. module_platform_driver(intel_xhci_usb_driver);
  187. MODULE_AUTHOR("Hans de Goede <[email protected]>");
  188. MODULE_DESCRIPTION("Intel XHCI USB role switch driver");
  189. MODULE_LICENSE("GPL");