phy-am335x-control.c 4.0 KB

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