phy-am335x-control.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. static void am335x_phy_power(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_USB0_CTRL;
  33. break;
  34. case 1:
  35. reg = AM335X_USB1_CTRL;
  36. break;
  37. default:
  38. WARN_ON(1);
  39. return;
  40. }
  41. val = readl(usb_ctrl->phy_reg + reg);
  42. if (on) {
  43. val &= ~(USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN);
  44. val |= USBPHY_OTGVDET_EN | USBPHY_OTGSESSEND_EN;
  45. } else {
  46. val |= USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN;
  47. }
  48. writel(val, usb_ctrl->phy_reg + reg);
  49. }
  50. static const struct phy_control ctrl_am335x = {
  51. .phy_power = am335x_phy_power,
  52. };
  53. static const struct of_device_id omap_control_usb_id_table[] = {
  54. { .compatible = "ti,am335x-usb-ctrl-module", .data = &ctrl_am335x },
  55. {}
  56. };
  57. MODULE_DEVICE_TABLE(of, omap_control_usb_id_table);
  58. static struct platform_driver am335x_control_driver;
  59. static int match(struct device *dev, void *data)
  60. {
  61. struct device_node *node = (struct device_node *)data;
  62. return dev->of_node == node &&
  63. dev->driver == &am335x_control_driver.driver;
  64. }
  65. struct phy_control *am335x_get_phy_control(struct device *dev)
  66. {
  67. struct device_node *node;
  68. struct am335x_control_usb *ctrl_usb;
  69. node = of_parse_phandle(dev->of_node, "ti,ctrl_mod", 0);
  70. if (!node)
  71. return NULL;
  72. dev = bus_find_device(&platform_bus_type, NULL, node, match);
  73. ctrl_usb = dev_get_drvdata(dev);
  74. if (!ctrl_usb)
  75. return NULL;
  76. return &ctrl_usb->phy_ctrl;
  77. }
  78. EXPORT_SYMBOL_GPL(am335x_get_phy_control);
  79. static int am335x_control_usb_probe(struct platform_device *pdev)
  80. {
  81. struct resource *res;
  82. struct am335x_control_usb *ctrl_usb;
  83. const struct of_device_id *of_id;
  84. const struct phy_control *phy_ctrl;
  85. of_id = of_match_node(omap_control_usb_id_table, pdev->dev.of_node);
  86. if (!of_id)
  87. return -EINVAL;
  88. phy_ctrl = of_id->data;
  89. ctrl_usb = devm_kzalloc(&pdev->dev, sizeof(*ctrl_usb), GFP_KERNEL);
  90. if (!ctrl_usb) {
  91. dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
  92. return -ENOMEM;
  93. }
  94. ctrl_usb->dev = &pdev->dev;
  95. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
  96. ctrl_usb->phy_reg = devm_ioremap_resource(&pdev->dev, res);
  97. if (IS_ERR(ctrl_usb->phy_reg))
  98. return PTR_ERR(ctrl_usb->phy_reg);
  99. spin_lock_init(&ctrl_usb->lock);
  100. ctrl_usb->phy_ctrl = *phy_ctrl;
  101. dev_set_drvdata(ctrl_usb->dev, ctrl_usb);
  102. return 0;
  103. }
  104. static struct platform_driver am335x_control_driver = {
  105. .probe = am335x_control_usb_probe,
  106. .driver = {
  107. .name = "am335x-control-usb",
  108. .owner = THIS_MODULE,
  109. .of_match_table = of_match_ptr(omap_control_usb_id_table),
  110. },
  111. };
  112. module_platform_driver(am335x_control_driver);
  113. MODULE_LICENSE("GPL v2");