phy-hi6220-usb.c 3.9 KB

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