phy-am335x-control.c 4.2 KB

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