phy-hi6220-usb.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2015 Linaro Ltd.
  4. * Copyright (c) 2015 HiSilicon Limited.
  5. */
  6. #include <linux/mfd/syscon.h>
  7. #include <linux/module.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/phy/phy.h>
  10. #include <linux/regmap.h>
  11. #define SC_PERIPH_CTRL4 0x00c
  12. #define CTRL4_PICO_SIDDQ BIT(6)
  13. #define CTRL4_PICO_OGDISABLE BIT(8)
  14. #define CTRL4_PICO_VBUSVLDEXT BIT(10)
  15. #define CTRL4_PICO_VBUSVLDEXTSEL BIT(11)
  16. #define CTRL4_OTG_PHY_SEL BIT(21)
  17. #define SC_PERIPH_CTRL5 0x010
  18. #define CTRL5_USBOTG_RES_SEL BIT(3)
  19. #define CTRL5_PICOPHY_ACAENB BIT(4)
  20. #define CTRL5_PICOPHY_BC_MODE BIT(5)
  21. #define CTRL5_PICOPHY_CHRGSEL BIT(6)
  22. #define CTRL5_PICOPHY_VDATSRCEND BIT(7)
  23. #define CTRL5_PICOPHY_VDATDETENB BIT(8)
  24. #define CTRL5_PICOPHY_DCDENB BIT(9)
  25. #define CTRL5_PICOPHY_IDDIG BIT(10)
  26. #define SC_PERIPH_CTRL8 0x018
  27. #define SC_PERIPH_RSTEN0 0x300
  28. #define SC_PERIPH_RSTDIS0 0x304
  29. #define RST0_USBOTG_BUS BIT(4)
  30. #define RST0_POR_PICOPHY BIT(5)
  31. #define RST0_USBOTG BIT(6)
  32. #define RST0_USBOTG_32K BIT(7)
  33. #define EYE_PATTERN_PARA 0x7053348c
  34. struct hi6220_priv {
  35. struct regmap *reg;
  36. struct device *dev;
  37. };
  38. static void hi6220_phy_init(struct hi6220_priv *priv)
  39. {
  40. struct regmap *reg = priv->reg;
  41. u32 val, mask;
  42. val = RST0_USBOTG_BUS | RST0_POR_PICOPHY |
  43. RST0_USBOTG | RST0_USBOTG_32K;
  44. mask = val;
  45. regmap_update_bits(reg, SC_PERIPH_RSTEN0, mask, val);
  46. regmap_update_bits(reg, SC_PERIPH_RSTDIS0, mask, val);
  47. }
  48. static int hi6220_phy_setup(struct hi6220_priv *priv, bool on)
  49. {
  50. struct regmap *reg = priv->reg;
  51. u32 val, mask;
  52. int ret;
  53. if (on) {
  54. val = CTRL5_USBOTG_RES_SEL | CTRL5_PICOPHY_ACAENB;
  55. mask = val | CTRL5_PICOPHY_BC_MODE;
  56. ret = regmap_update_bits(reg, SC_PERIPH_CTRL5, mask, val);
  57. if (ret)
  58. goto out;
  59. val = CTRL4_PICO_VBUSVLDEXT | CTRL4_PICO_VBUSVLDEXTSEL |
  60. CTRL4_OTG_PHY_SEL;
  61. mask = val | CTRL4_PICO_SIDDQ | CTRL4_PICO_OGDISABLE;
  62. ret = regmap_update_bits(reg, SC_PERIPH_CTRL4, mask, val);
  63. if (ret)
  64. goto out;
  65. ret = regmap_write(reg, SC_PERIPH_CTRL8, EYE_PATTERN_PARA);
  66. if (ret)
  67. goto out;
  68. } else {
  69. val = CTRL4_PICO_SIDDQ;
  70. mask = val;
  71. ret = regmap_update_bits(reg, SC_PERIPH_CTRL4, mask, val);
  72. if (ret)
  73. goto out;
  74. }
  75. return 0;
  76. out:
  77. dev_err(priv->dev, "failed to setup phy ret: %d\n", ret);
  78. return ret;
  79. }
  80. static int hi6220_phy_start(struct phy *phy)
  81. {
  82. struct hi6220_priv *priv = phy_get_drvdata(phy);
  83. return hi6220_phy_setup(priv, true);
  84. }
  85. static int hi6220_phy_exit(struct phy *phy)
  86. {
  87. struct hi6220_priv *priv = phy_get_drvdata(phy);
  88. return hi6220_phy_setup(priv, false);
  89. }
  90. static const struct phy_ops hi6220_phy_ops = {
  91. .init = hi6220_phy_start,
  92. .exit = hi6220_phy_exit,
  93. .owner = THIS_MODULE,
  94. };
  95. static int hi6220_phy_probe(struct platform_device *pdev)
  96. {
  97. struct phy_provider *phy_provider;
  98. struct device *dev = &pdev->dev;
  99. struct phy *phy;
  100. struct hi6220_priv *priv;
  101. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  102. if (!priv)
  103. return -ENOMEM;
  104. priv->dev = dev;
  105. priv->reg = syscon_regmap_lookup_by_phandle(dev->of_node,
  106. "hisilicon,peripheral-syscon");
  107. if (IS_ERR(priv->reg)) {
  108. dev_err(dev, "no hisilicon,peripheral-syscon\n");
  109. return PTR_ERR(priv->reg);
  110. }
  111. hi6220_phy_init(priv);
  112. phy = devm_phy_create(dev, NULL, &hi6220_phy_ops);
  113. if (IS_ERR(phy))
  114. return PTR_ERR(phy);
  115. phy_set_drvdata(phy, priv);
  116. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  117. return PTR_ERR_OR_ZERO(phy_provider);
  118. }
  119. static const struct of_device_id hi6220_phy_of_match[] = {
  120. {.compatible = "hisilicon,hi6220-usb-phy",},
  121. { },
  122. };
  123. MODULE_DEVICE_TABLE(of, hi6220_phy_of_match);
  124. static struct platform_driver hi6220_phy_driver = {
  125. .probe = hi6220_phy_probe,
  126. .driver = {
  127. .name = "hi6220-usb-phy",
  128. .of_match_table = hi6220_phy_of_match,
  129. }
  130. };
  131. module_platform_driver(hi6220_phy_driver);
  132. MODULE_DESCRIPTION("HISILICON HI6220 USB PHY driver");
  133. MODULE_ALIAS("platform:hi6220-usb-phy");
  134. MODULE_LICENSE("GPL");