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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 <hdegoede@redhat.com>
  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/usb/role.h>
  22. /* register definition */
  23. #define DUAL_ROLE_CFG0 0x68
  24. #define SW_VBUS_VALID BIT(24)
  25. #define SW_IDPIN_EN BIT(21)
  26. #define SW_IDPIN BIT(20)
  27. #define DUAL_ROLE_CFG1 0x6c
  28. #define HOST_MODE BIT(29)
  29. #define DUAL_ROLE_CFG1_POLL_TIMEOUT 1000
  30. #define DRV_NAME "intel_xhci_usb_sw"
  31. struct intel_xhci_usb_data {
  32. struct usb_role_switch *role_sw;
  33. void __iomem *base;
  34. };
  35. static int intel_xhci_usb_set_role(struct device *dev, enum usb_role role)
  36. {
  37. struct intel_xhci_usb_data *data = dev_get_drvdata(dev);
  38. unsigned long timeout;
  39. acpi_status status;
  40. u32 glk, val;
  41. /*
  42. * On many CHT devices ACPI event (_AEI) handlers read / modify /
  43. * write the cfg0 register, just like we do. Take the ACPI lock
  44. * to avoid us racing with the AML code.
  45. */
  46. status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk);
  47. if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) {
  48. dev_err(dev, "Error could not acquire lock\n");
  49. return -EIO;
  50. }
  51. pm_runtime_get_sync(dev);
  52. /* Set idpin value as requested */
  53. val = readl(data->base + DUAL_ROLE_CFG0);
  54. switch (role) {
  55. case USB_ROLE_NONE:
  56. val |= SW_IDPIN;
  57. val &= ~SW_VBUS_VALID;
  58. break;
  59. case USB_ROLE_HOST:
  60. val &= ~SW_IDPIN;
  61. val &= ~SW_VBUS_VALID;
  62. break;
  63. case USB_ROLE_DEVICE:
  64. val |= SW_IDPIN;
  65. val |= SW_VBUS_VALID;
  66. break;
  67. }
  68. val |= SW_IDPIN_EN;
  69. writel(val, data->base + DUAL_ROLE_CFG0);
  70. acpi_release_global_lock(glk);
  71. /* In most case it takes about 600ms to finish mode switching */
  72. timeout = jiffies + msecs_to_jiffies(DUAL_ROLE_CFG1_POLL_TIMEOUT);
  73. /* Polling on CFG1 register to confirm mode switch.*/
  74. do {
  75. val = readl(data->base + DUAL_ROLE_CFG1);
  76. if (!!(val & HOST_MODE) == (role == USB_ROLE_HOST)) {
  77. pm_runtime_put(dev);
  78. return 0;
  79. }
  80. /* Interval for polling is set to about 5 - 10 ms */
  81. usleep_range(5000, 10000);
  82. } while (time_before(jiffies, timeout));
  83. pm_runtime_put(dev);
  84. dev_warn(dev, "Timeout waiting for role-switch\n");
  85. return -ETIMEDOUT;
  86. }
  87. static enum usb_role intel_xhci_usb_get_role(struct device *dev)
  88. {
  89. struct intel_xhci_usb_data *data = dev_get_drvdata(dev);
  90. enum usb_role role;
  91. u32 val;
  92. pm_runtime_get_sync(dev);
  93. val = readl(data->base + DUAL_ROLE_CFG0);
  94. pm_runtime_put(dev);
  95. if (!(val & SW_IDPIN))
  96. role = USB_ROLE_HOST;
  97. else if (val & SW_VBUS_VALID)
  98. role = USB_ROLE_DEVICE;
  99. else
  100. role = USB_ROLE_NONE;
  101. return role;
  102. }
  103. static const struct usb_role_switch_desc sw_desc = {
  104. .set = intel_xhci_usb_set_role,
  105. .get = intel_xhci_usb_get_role,
  106. .allow_userspace_control = true,
  107. };
  108. static int intel_xhci_usb_probe(struct platform_device *pdev)
  109. {
  110. struct device *dev = &pdev->dev;
  111. struct intel_xhci_usb_data *data;
  112. struct resource *res;
  113. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  114. if (!data)
  115. return -ENOMEM;
  116. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  117. if (!res)
  118. return -EINVAL;
  119. data->base = devm_ioremap_nocache(dev, res->start, resource_size(res));
  120. if (!data->base)
  121. return -ENOMEM;
  122. platform_set_drvdata(pdev, data);
  123. data->role_sw = usb_role_switch_register(dev, &sw_desc);
  124. if (IS_ERR(data->role_sw))
  125. return PTR_ERR(data->role_sw);
  126. pm_runtime_set_active(dev);
  127. pm_runtime_enable(dev);
  128. return 0;
  129. }
  130. static int intel_xhci_usb_remove(struct platform_device *pdev)
  131. {
  132. struct intel_xhci_usb_data *data = platform_get_drvdata(pdev);
  133. pm_runtime_disable(&pdev->dev);
  134. usb_role_switch_unregister(data->role_sw);
  135. return 0;
  136. }
  137. static const struct platform_device_id intel_xhci_usb_table[] = {
  138. { .name = DRV_NAME },
  139. {}
  140. };
  141. MODULE_DEVICE_TABLE(platform, intel_xhci_usb_table);
  142. static struct platform_driver intel_xhci_usb_driver = {
  143. .driver = {
  144. .name = DRV_NAME,
  145. },
  146. .id_table = intel_xhci_usb_table,
  147. .probe = intel_xhci_usb_probe,
  148. .remove = intel_xhci_usb_remove,
  149. };
  150. module_platform_driver(intel_xhci_usb_driver);
  151. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  152. MODULE_DESCRIPTION("Intel XHCI USB role switch driver");
  153. MODULE_LICENSE("GPL");