phy-qcom-usb-ss.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2012-2014,2017 The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2018-2020, Linaro Limited
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/delay.h>
  8. #include <linux/err.h>
  9. #include <linux/io.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/phy/phy.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/reset.h>
  17. #include <linux/slab.h>
  18. #define PHY_CTRL0 0x6C
  19. #define PHY_CTRL1 0x70
  20. #define PHY_CTRL2 0x74
  21. #define PHY_CTRL4 0x7C
  22. /* PHY_CTRL bits */
  23. #define REF_PHY_EN BIT(0)
  24. #define LANE0_PWR_ON BIT(2)
  25. #define SWI_PCS_CLK_SEL BIT(4)
  26. #define TST_PWR_DOWN BIT(4)
  27. #define PHY_RESET BIT(7)
  28. #define NUM_BULK_CLKS 3
  29. #define NUM_BULK_REGS 2
  30. struct ssphy_priv {
  31. void __iomem *base;
  32. struct device *dev;
  33. struct reset_control *reset_com;
  34. struct reset_control *reset_phy;
  35. struct regulator_bulk_data regs[NUM_BULK_REGS];
  36. struct clk_bulk_data clks[NUM_BULK_CLKS];
  37. enum phy_mode mode;
  38. };
  39. static inline void qcom_ssphy_updatel(void __iomem *addr, u32 mask, u32 val)
  40. {
  41. writel((readl(addr) & ~mask) | val, addr);
  42. }
  43. static int qcom_ssphy_do_reset(struct ssphy_priv *priv)
  44. {
  45. int ret;
  46. if (!priv->reset_com) {
  47. qcom_ssphy_updatel(priv->base + PHY_CTRL1, PHY_RESET,
  48. PHY_RESET);
  49. usleep_range(10, 20);
  50. qcom_ssphy_updatel(priv->base + PHY_CTRL1, PHY_RESET, 0);
  51. } else {
  52. ret = reset_control_assert(priv->reset_com);
  53. if (ret) {
  54. dev_err(priv->dev, "Failed to assert reset com\n");
  55. return ret;
  56. }
  57. ret = reset_control_assert(priv->reset_phy);
  58. if (ret) {
  59. dev_err(priv->dev, "Failed to assert reset phy\n");
  60. return ret;
  61. }
  62. usleep_range(10, 20);
  63. ret = reset_control_deassert(priv->reset_com);
  64. if (ret) {
  65. dev_err(priv->dev, "Failed to deassert reset com\n");
  66. return ret;
  67. }
  68. ret = reset_control_deassert(priv->reset_phy);
  69. if (ret) {
  70. dev_err(priv->dev, "Failed to deassert reset phy\n");
  71. return ret;
  72. }
  73. }
  74. return 0;
  75. }
  76. static int qcom_ssphy_power_on(struct phy *phy)
  77. {
  78. struct ssphy_priv *priv = phy_get_drvdata(phy);
  79. int ret;
  80. ret = regulator_bulk_enable(NUM_BULK_REGS, priv->regs);
  81. if (ret)
  82. return ret;
  83. ret = clk_bulk_prepare_enable(NUM_BULK_CLKS, priv->clks);
  84. if (ret)
  85. goto err_disable_regulator;
  86. ret = qcom_ssphy_do_reset(priv);
  87. if (ret)
  88. goto err_disable_clock;
  89. writeb(SWI_PCS_CLK_SEL, priv->base + PHY_CTRL0);
  90. qcom_ssphy_updatel(priv->base + PHY_CTRL4, LANE0_PWR_ON, LANE0_PWR_ON);
  91. qcom_ssphy_updatel(priv->base + PHY_CTRL2, REF_PHY_EN, REF_PHY_EN);
  92. qcom_ssphy_updatel(priv->base + PHY_CTRL4, TST_PWR_DOWN, 0);
  93. return 0;
  94. err_disable_clock:
  95. clk_bulk_disable_unprepare(NUM_BULK_CLKS, priv->clks);
  96. err_disable_regulator:
  97. regulator_bulk_disable(NUM_BULK_REGS, priv->regs);
  98. return ret;
  99. }
  100. static int qcom_ssphy_power_off(struct phy *phy)
  101. {
  102. struct ssphy_priv *priv = phy_get_drvdata(phy);
  103. qcom_ssphy_updatel(priv->base + PHY_CTRL4, LANE0_PWR_ON, 0);
  104. qcom_ssphy_updatel(priv->base + PHY_CTRL2, REF_PHY_EN, 0);
  105. qcom_ssphy_updatel(priv->base + PHY_CTRL4, TST_PWR_DOWN, TST_PWR_DOWN);
  106. clk_bulk_disable_unprepare(NUM_BULK_CLKS, priv->clks);
  107. regulator_bulk_disable(NUM_BULK_REGS, priv->regs);
  108. return 0;
  109. }
  110. static int qcom_ssphy_init_clock(struct ssphy_priv *priv)
  111. {
  112. priv->clks[0].id = "ref";
  113. priv->clks[1].id = "ahb";
  114. priv->clks[2].id = "pipe";
  115. return devm_clk_bulk_get(priv->dev, NUM_BULK_CLKS, priv->clks);
  116. }
  117. static int qcom_ssphy_init_regulator(struct ssphy_priv *priv)
  118. {
  119. int ret;
  120. priv->regs[0].supply = "vdd";
  121. priv->regs[1].supply = "vdda1p8";
  122. ret = devm_regulator_bulk_get(priv->dev, NUM_BULK_REGS, priv->regs);
  123. if (ret) {
  124. if (ret != -EPROBE_DEFER)
  125. dev_err(priv->dev, "Failed to get regulators\n");
  126. return ret;
  127. }
  128. return ret;
  129. }
  130. static int qcom_ssphy_init_reset(struct ssphy_priv *priv)
  131. {
  132. priv->reset_com = devm_reset_control_get_optional_exclusive(priv->dev, "com");
  133. if (IS_ERR(priv->reset_com)) {
  134. dev_err(priv->dev, "Failed to get reset control com\n");
  135. return PTR_ERR(priv->reset_com);
  136. }
  137. if (priv->reset_com) {
  138. /* if reset_com is present, reset_phy is no longer optional */
  139. priv->reset_phy = devm_reset_control_get_exclusive(priv->dev, "phy");
  140. if (IS_ERR(priv->reset_phy)) {
  141. dev_err(priv->dev, "Failed to get reset control phy\n");
  142. return PTR_ERR(priv->reset_phy);
  143. }
  144. }
  145. return 0;
  146. }
  147. static const struct phy_ops qcom_ssphy_ops = {
  148. .power_off = qcom_ssphy_power_off,
  149. .power_on = qcom_ssphy_power_on,
  150. .owner = THIS_MODULE,
  151. };
  152. static int qcom_ssphy_probe(struct platform_device *pdev)
  153. {
  154. struct device *dev = &pdev->dev;
  155. struct phy_provider *provider;
  156. struct ssphy_priv *priv;
  157. struct phy *phy;
  158. int ret;
  159. priv = devm_kzalloc(dev, sizeof(struct ssphy_priv), GFP_KERNEL);
  160. if (!priv)
  161. return -ENOMEM;
  162. priv->dev = dev;
  163. priv->mode = PHY_MODE_INVALID;
  164. priv->base = devm_platform_ioremap_resource(pdev, 0);
  165. if (IS_ERR(priv->base))
  166. return PTR_ERR(priv->base);
  167. ret = qcom_ssphy_init_clock(priv);
  168. if (ret)
  169. return ret;
  170. ret = qcom_ssphy_init_reset(priv);
  171. if (ret)
  172. return ret;
  173. ret = qcom_ssphy_init_regulator(priv);
  174. if (ret)
  175. return ret;
  176. phy = devm_phy_create(dev, dev->of_node, &qcom_ssphy_ops);
  177. if (IS_ERR(phy)) {
  178. dev_err(dev, "Failed to create the SS phy\n");
  179. return PTR_ERR(phy);
  180. }
  181. phy_set_drvdata(phy, priv);
  182. provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  183. return PTR_ERR_OR_ZERO(provider);
  184. }
  185. static const struct of_device_id qcom_ssphy_match[] = {
  186. { .compatible = "qcom,usb-ss-28nm-phy", },
  187. { },
  188. };
  189. MODULE_DEVICE_TABLE(of, qcom_ssphy_match);
  190. static struct platform_driver qcom_ssphy_driver = {
  191. .probe = qcom_ssphy_probe,
  192. .driver = {
  193. .name = "qcom-usb-ssphy",
  194. .of_match_table = qcom_ssphy_match,
  195. },
  196. };
  197. module_platform_driver(qcom_ssphy_driver);
  198. MODULE_DESCRIPTION("Qualcomm SuperSpeed USB PHY driver");
  199. MODULE_LICENSE("GPL v2");