phy-am335x-control.c 4.4 KB

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