phy-qcom-ipq4019-usb.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2018 John Crispin <[email protected]>
  4. *
  5. * Based on code from
  6. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  7. *
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/of_device.h>
  17. #include <linux/phy/phy.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/reset.h>
  20. struct ipq4019_usb_phy {
  21. struct device *dev;
  22. struct phy *phy;
  23. void __iomem *base;
  24. struct reset_control *por_rst;
  25. struct reset_control *srif_rst;
  26. };
  27. static int ipq4019_ss_phy_power_off(struct phy *_phy)
  28. {
  29. struct ipq4019_usb_phy *phy = phy_get_drvdata(_phy);
  30. reset_control_assert(phy->por_rst);
  31. msleep(10);
  32. return 0;
  33. }
  34. static int ipq4019_ss_phy_power_on(struct phy *_phy)
  35. {
  36. struct ipq4019_usb_phy *phy = phy_get_drvdata(_phy);
  37. ipq4019_ss_phy_power_off(_phy);
  38. reset_control_deassert(phy->por_rst);
  39. return 0;
  40. }
  41. static const struct phy_ops ipq4019_usb_ss_phy_ops = {
  42. .power_on = ipq4019_ss_phy_power_on,
  43. .power_off = ipq4019_ss_phy_power_off,
  44. };
  45. static int ipq4019_hs_phy_power_off(struct phy *_phy)
  46. {
  47. struct ipq4019_usb_phy *phy = phy_get_drvdata(_phy);
  48. reset_control_assert(phy->por_rst);
  49. msleep(10);
  50. reset_control_assert(phy->srif_rst);
  51. msleep(10);
  52. return 0;
  53. }
  54. static int ipq4019_hs_phy_power_on(struct phy *_phy)
  55. {
  56. struct ipq4019_usb_phy *phy = phy_get_drvdata(_phy);
  57. ipq4019_hs_phy_power_off(_phy);
  58. reset_control_deassert(phy->srif_rst);
  59. msleep(10);
  60. reset_control_deassert(phy->por_rst);
  61. return 0;
  62. }
  63. static const struct phy_ops ipq4019_usb_hs_phy_ops = {
  64. .power_on = ipq4019_hs_phy_power_on,
  65. .power_off = ipq4019_hs_phy_power_off,
  66. };
  67. static const struct of_device_id ipq4019_usb_phy_of_match[] = {
  68. { .compatible = "qcom,usb-hs-ipq4019-phy", .data = &ipq4019_usb_hs_phy_ops},
  69. { .compatible = "qcom,usb-ss-ipq4019-phy", .data = &ipq4019_usb_ss_phy_ops},
  70. { },
  71. };
  72. MODULE_DEVICE_TABLE(of, ipq4019_usb_phy_of_match);
  73. static int ipq4019_usb_phy_probe(struct platform_device *pdev)
  74. {
  75. struct device *dev = &pdev->dev;
  76. struct phy_provider *phy_provider;
  77. struct ipq4019_usb_phy *phy;
  78. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  79. if (!phy)
  80. return -ENOMEM;
  81. phy->dev = &pdev->dev;
  82. phy->base = devm_platform_ioremap_resource(pdev, 0);
  83. if (IS_ERR(phy->base)) {
  84. dev_err(dev, "failed to remap register memory\n");
  85. return PTR_ERR(phy->base);
  86. }
  87. phy->por_rst = devm_reset_control_get(phy->dev, "por_rst");
  88. if (IS_ERR(phy->por_rst)) {
  89. if (PTR_ERR(phy->por_rst) != -EPROBE_DEFER)
  90. dev_err(dev, "POR reset is missing\n");
  91. return PTR_ERR(phy->por_rst);
  92. }
  93. phy->srif_rst = devm_reset_control_get_optional(phy->dev, "srif_rst");
  94. if (IS_ERR(phy->srif_rst))
  95. return PTR_ERR(phy->srif_rst);
  96. phy->phy = devm_phy_create(dev, NULL, of_device_get_match_data(dev));
  97. if (IS_ERR(phy->phy)) {
  98. dev_err(dev, "failed to create PHY\n");
  99. return PTR_ERR(phy->phy);
  100. }
  101. phy_set_drvdata(phy->phy, phy);
  102. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  103. return PTR_ERR_OR_ZERO(phy_provider);
  104. }
  105. static struct platform_driver ipq4019_usb_phy_driver = {
  106. .probe = ipq4019_usb_phy_probe,
  107. .driver = {
  108. .of_match_table = ipq4019_usb_phy_of_match,
  109. .name = "ipq4019-usb-phy",
  110. }
  111. };
  112. module_platform_driver(ipq4019_usb_phy_driver);
  113. MODULE_DESCRIPTION("QCOM/IPQ4019 USB phy driver");
  114. MODULE_AUTHOR("John Crispin <[email protected]>");
  115. MODULE_LICENSE("GPL v2");