phy-am335x-control.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. struct phy_control {
  7. void (*phy_power)(struct phy_control *phy_ctrl, u32 id, bool on);
  8. void (*phy_wkup)(struct phy_control *phy_ctrl, u32 id, bool on);
  9. };
  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, 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. val &= ~(USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN);
  72. val |= USBPHY_OTGVDET_EN | USBPHY_OTGSESSEND_EN;
  73. } else {
  74. val |= USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN;
  75. }
  76. writel(val, usb_ctrl->phy_reg + reg);
  77. }
  78. static const struct phy_control ctrl_am335x = {
  79. .phy_power = am335x_phy_power,
  80. .phy_wkup = am335x_phy_wkup,
  81. };
  82. static const struct of_device_id omap_control_usb_id_table[] = {
  83. { .compatible = "ti,am335x-usb-ctrl-module", .data = &ctrl_am335x },
  84. {}
  85. };
  86. MODULE_DEVICE_TABLE(of, omap_control_usb_id_table);
  87. static struct platform_driver am335x_control_driver;
  88. static int match(struct device *dev, void *data)
  89. {
  90. struct device_node *node = (struct device_node *)data;
  91. return dev->of_node == node &&
  92. dev->driver == &am335x_control_driver.driver;
  93. }
  94. struct phy_control *am335x_get_phy_control(struct device *dev)
  95. {
  96. struct device_node *node;
  97. struct am335x_control_usb *ctrl_usb;
  98. node = of_parse_phandle(dev->of_node, "ti,ctrl_mod", 0);
  99. if (!node)
  100. return NULL;
  101. dev = bus_find_device(&platform_bus_type, NULL, node, match);
  102. ctrl_usb = dev_get_drvdata(dev);
  103. if (!ctrl_usb)
  104. return NULL;
  105. return &ctrl_usb->phy_ctrl;
  106. }
  107. EXPORT_SYMBOL_GPL(am335x_get_phy_control);
  108. static int am335x_control_usb_probe(struct platform_device *pdev)
  109. {
  110. struct resource *res;
  111. struct am335x_control_usb *ctrl_usb;
  112. const struct of_device_id *of_id;
  113. const struct phy_control *phy_ctrl;
  114. of_id = of_match_node(omap_control_usb_id_table, pdev->dev.of_node);
  115. if (!of_id)
  116. return -EINVAL;
  117. phy_ctrl = of_id->data;
  118. ctrl_usb = devm_kzalloc(&pdev->dev, sizeof(*ctrl_usb), GFP_KERNEL);
  119. if (!ctrl_usb) {
  120. dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
  121. return -ENOMEM;
  122. }
  123. ctrl_usb->dev = &pdev->dev;
  124. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
  125. ctrl_usb->phy_reg = devm_ioremap_resource(&pdev->dev, res);
  126. if (IS_ERR(ctrl_usb->phy_reg))
  127. return PTR_ERR(ctrl_usb->phy_reg);
  128. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wakeup");
  129. ctrl_usb->wkup = devm_ioremap_resource(&pdev->dev, res);
  130. if (IS_ERR(ctrl_usb->wkup))
  131. return PTR_ERR(ctrl_usb->wkup);
  132. spin_lock_init(&ctrl_usb->lock);
  133. ctrl_usb->phy_ctrl = *phy_ctrl;
  134. dev_set_drvdata(ctrl_usb->dev, ctrl_usb);
  135. return 0;
  136. }
  137. static struct platform_driver am335x_control_driver = {
  138. .probe = am335x_control_usb_probe,
  139. .driver = {
  140. .name = "am335x-control-usb",
  141. .owner = THIS_MODULE,
  142. .of_match_table = omap_control_usb_id_table,
  143. },
  144. };
  145. module_platform_driver(am335x_control_driver);
  146. MODULE_LICENSE("GPL v2");