phy-am335x-control.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/module.h>
  3. #include <linux/platform_device.h>
  4. #include <linux/err.h>
  5. #include <linux/of.h>
  6. #include <linux/io.h>
  7. #include <linux/delay.h>
  8. #include <linux/usb/otg.h>
  9. #include "phy-am335x-control.h"
  10. struct am335x_control_usb {
  11. struct device *dev;
  12. void __iomem *phy_reg;
  13. void __iomem *wkup;
  14. spinlock_t lock;
  15. struct phy_control phy_ctrl;
  16. };
  17. #define AM335X_USB0_CTRL 0x0
  18. #define AM335X_USB1_CTRL 0x8
  19. #define AM335x_USB_WKUP 0x0
  20. #define USBPHY_CM_PWRDN (1 << 0)
  21. #define USBPHY_OTG_PWRDN (1 << 1)
  22. #define USBPHY_OTGVDET_EN (1 << 19)
  23. #define USBPHY_OTGSESSEND_EN (1 << 20)
  24. #define AM335X_PHY0_WK_EN (1 << 0)
  25. #define AM335X_PHY1_WK_EN (1 << 8)
  26. static void am335x_phy_wkup(struct phy_control *phy_ctrl, u32 id, bool on)
  27. {
  28. struct am335x_control_usb *usb_ctrl;
  29. u32 val;
  30. u32 reg;
  31. usb_ctrl = container_of(phy_ctrl, struct am335x_control_usb, phy_ctrl);
  32. switch (id) {
  33. case 0:
  34. reg = AM335X_PHY0_WK_EN;
  35. break;
  36. case 1:
  37. reg = AM335X_PHY1_WK_EN;
  38. break;
  39. default:
  40. WARN_ON(1);
  41. return;
  42. }
  43. spin_lock(&usb_ctrl->lock);
  44. val = readl(usb_ctrl->wkup);
  45. if (on)
  46. val |= reg;
  47. else
  48. val &= ~reg;
  49. writel(val, usb_ctrl->wkup);
  50. spin_unlock(&usb_ctrl->lock);
  51. }
  52. static void am335x_phy_power(struct phy_control *phy_ctrl, u32 id,
  53. enum usb_dr_mode dr_mode, bool on)
  54. {
  55. struct am335x_control_usb *usb_ctrl;
  56. u32 val;
  57. u32 reg;
  58. usb_ctrl = container_of(phy_ctrl, struct am335x_control_usb, phy_ctrl);
  59. switch (id) {
  60. case 0:
  61. reg = AM335X_USB0_CTRL;
  62. break;
  63. case 1:
  64. reg = AM335X_USB1_CTRL;
  65. break;
  66. default:
  67. WARN_ON(1);
  68. return;
  69. }
  70. val = readl(usb_ctrl->phy_reg + reg);
  71. if (on) {
  72. if (dr_mode == USB_DR_MODE_HOST) {
  73. val &= ~(USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN |
  74. USBPHY_OTGVDET_EN);
  75. val |= USBPHY_OTGSESSEND_EN;
  76. } else {
  77. val &= ~(USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN);
  78. val |= USBPHY_OTGVDET_EN | USBPHY_OTGSESSEND_EN;
  79. }
  80. } else {
  81. val |= USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN;
  82. }
  83. writel(val, usb_ctrl->phy_reg + reg);
  84. /*
  85. * Give the PHY ~1ms to complete the power up operation.
  86. * Tests have shown unstable behaviour if other USB PHY related
  87. * registers are written too shortly after such a transition.
  88. */
  89. if (on)
  90. mdelay(1);
  91. }
  92. static const struct phy_control ctrl_am335x = {
  93. .phy_power = am335x_phy_power,
  94. .phy_wkup = am335x_phy_wkup,
  95. };
  96. static const struct of_device_id omap_control_usb_id_table[] = {
  97. { .compatible = "ti,am335x-usb-ctrl-module", .data = &ctrl_am335x },
  98. {}
  99. };
  100. MODULE_DEVICE_TABLE(of, omap_control_usb_id_table);
  101. static struct platform_driver am335x_control_driver;
  102. static int match(struct device *dev, void *data)
  103. {
  104. struct device_node *node = (struct device_node *)data;
  105. return dev->of_node == node &&
  106. dev->driver == &am335x_control_driver.driver;
  107. }
  108. struct phy_control *am335x_get_phy_control(struct device *dev)
  109. {
  110. struct device_node *node;
  111. struct am335x_control_usb *ctrl_usb;
  112. node = of_parse_phandle(dev->of_node, "ti,ctrl_mod", 0);
  113. if (!node)
  114. return NULL;
  115. dev = bus_find_device(&platform_bus_type, NULL, node, match);
  116. of_node_put(node);
  117. if (!dev)
  118. return NULL;
  119. ctrl_usb = dev_get_drvdata(dev);
  120. put_device(dev);
  121. if (!ctrl_usb)
  122. return NULL;
  123. return &ctrl_usb->phy_ctrl;
  124. }
  125. EXPORT_SYMBOL_GPL(am335x_get_phy_control);
  126. static int am335x_control_usb_probe(struct platform_device *pdev)
  127. {
  128. struct resource *res;
  129. struct am335x_control_usb *ctrl_usb;
  130. const struct of_device_id *of_id;
  131. const struct phy_control *phy_ctrl;
  132. of_id = of_match_node(omap_control_usb_id_table, pdev->dev.of_node);
  133. if (!of_id)
  134. return -EINVAL;
  135. phy_ctrl = of_id->data;
  136. ctrl_usb = devm_kzalloc(&pdev->dev, sizeof(*ctrl_usb), GFP_KERNEL);
  137. if (!ctrl_usb)
  138. return -ENOMEM;
  139. ctrl_usb->dev = &pdev->dev;
  140. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
  141. ctrl_usb->phy_reg = devm_ioremap_resource(&pdev->dev, res);
  142. if (IS_ERR(ctrl_usb->phy_reg))
  143. return PTR_ERR(ctrl_usb->phy_reg);
  144. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wakeup");
  145. ctrl_usb->wkup = devm_ioremap_resource(&pdev->dev, res);
  146. if (IS_ERR(ctrl_usb->wkup))
  147. return PTR_ERR(ctrl_usb->wkup);
  148. spin_lock_init(&ctrl_usb->lock);
  149. ctrl_usb->phy_ctrl = *phy_ctrl;
  150. dev_set_drvdata(ctrl_usb->dev, ctrl_usb);
  151. return 0;
  152. }
  153. static struct platform_driver am335x_control_driver = {
  154. .probe = am335x_control_usb_probe,
  155. .driver = {
  156. .name = "am335x-control-usb",
  157. .of_match_table = omap_control_usb_id_table,
  158. },
  159. };
  160. module_platform_driver(am335x_control_driver);
  161. MODULE_LICENSE("GPL v2");