phy-stih407-usb.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2014 STMicroelectronics
  4. *
  5. * STMicroelectronics Generic PHY driver for STiH407 USB2.
  6. *
  7. * Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  8. */
  9. #include <linux/platform_device.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/clk.h>
  16. #include <linux/regmap.h>
  17. #include <linux/reset.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <linux/phy/phy.h>
  20. #define PHYPARAM_REG 1
  21. #define PHYCTRL_REG 2
  22. /* Default PHY_SEL and REFCLKSEL configuration */
  23. #define STIH407_USB_PICOPHY_CTRL_PORT_CONF 0x6
  24. #define STIH407_USB_PICOPHY_CTRL_PORT_MASK 0x1f
  25. /* ports parameters overriding */
  26. #define STIH407_USB_PICOPHY_PARAM_DEF 0x39a4dc
  27. #define STIH407_USB_PICOPHY_PARAM_MASK 0xffffffff
  28. struct stih407_usb2_picophy {
  29. struct phy *phy;
  30. struct regmap *regmap;
  31. struct device *dev;
  32. struct reset_control *rstc;
  33. struct reset_control *rstport;
  34. int ctrl;
  35. int param;
  36. };
  37. static int stih407_usb2_pico_ctrl(struct stih407_usb2_picophy *phy_dev)
  38. {
  39. reset_control_deassert(phy_dev->rstc);
  40. return regmap_update_bits(phy_dev->regmap, phy_dev->ctrl,
  41. STIH407_USB_PICOPHY_CTRL_PORT_MASK,
  42. STIH407_USB_PICOPHY_CTRL_PORT_CONF);
  43. }
  44. static int stih407_usb2_init_port(struct phy *phy)
  45. {
  46. int ret;
  47. struct stih407_usb2_picophy *phy_dev = phy_get_drvdata(phy);
  48. stih407_usb2_pico_ctrl(phy_dev);
  49. ret = regmap_update_bits(phy_dev->regmap,
  50. phy_dev->param,
  51. STIH407_USB_PICOPHY_PARAM_MASK,
  52. STIH407_USB_PICOPHY_PARAM_DEF);
  53. if (ret)
  54. return ret;
  55. return reset_control_deassert(phy_dev->rstport);
  56. }
  57. static int stih407_usb2_exit_port(struct phy *phy)
  58. {
  59. struct stih407_usb2_picophy *phy_dev = phy_get_drvdata(phy);
  60. /*
  61. * Only port reset is asserted, phy global reset is kept untouched
  62. * as other ports may still be active. When all ports are in reset
  63. * state, assumption is made that power will be cut off on the phy, in
  64. * case of suspend for instance. Theoretically, asserting individual
  65. * reset (like here) or global reset should be equivalent.
  66. */
  67. return reset_control_assert(phy_dev->rstport);
  68. }
  69. static const struct phy_ops stih407_usb2_picophy_data = {
  70. .init = stih407_usb2_init_port,
  71. .exit = stih407_usb2_exit_port,
  72. .owner = THIS_MODULE,
  73. };
  74. static int stih407_usb2_picophy_probe(struct platform_device *pdev)
  75. {
  76. struct stih407_usb2_picophy *phy_dev;
  77. struct device *dev = &pdev->dev;
  78. struct device_node *np = dev->of_node;
  79. struct phy_provider *phy_provider;
  80. struct phy *phy;
  81. int ret;
  82. phy_dev = devm_kzalloc(dev, sizeof(*phy_dev), GFP_KERNEL);
  83. if (!phy_dev)
  84. return -ENOMEM;
  85. phy_dev->dev = dev;
  86. dev_set_drvdata(dev, phy_dev);
  87. phy_dev->rstc = devm_reset_control_get_shared(dev, "global");
  88. if (IS_ERR(phy_dev->rstc)) {
  89. dev_err(dev, "failed to ctrl picoPHY reset\n");
  90. return PTR_ERR(phy_dev->rstc);
  91. }
  92. phy_dev->rstport = devm_reset_control_get_exclusive(dev, "port");
  93. if (IS_ERR(phy_dev->rstport)) {
  94. dev_err(dev, "failed to ctrl picoPHY reset\n");
  95. return PTR_ERR(phy_dev->rstport);
  96. }
  97. /* Reset port by default: only deassert it in phy init */
  98. reset_control_assert(phy_dev->rstport);
  99. phy_dev->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  100. if (IS_ERR(phy_dev->regmap)) {
  101. dev_err(dev, "No syscfg phandle specified\n");
  102. return PTR_ERR(phy_dev->regmap);
  103. }
  104. ret = of_property_read_u32_index(np, "st,syscfg", PHYPARAM_REG,
  105. &phy_dev->param);
  106. if (ret) {
  107. dev_err(dev, "can't get phyparam offset (%d)\n", ret);
  108. return ret;
  109. }
  110. ret = of_property_read_u32_index(np, "st,syscfg", PHYCTRL_REG,
  111. &phy_dev->ctrl);
  112. if (ret) {
  113. dev_err(dev, "can't get phyctrl offset (%d)\n", ret);
  114. return ret;
  115. }
  116. phy = devm_phy_create(dev, NULL, &stih407_usb2_picophy_data);
  117. if (IS_ERR(phy)) {
  118. dev_err(dev, "failed to create Display Port PHY\n");
  119. return PTR_ERR(phy);
  120. }
  121. phy_dev->phy = phy;
  122. phy_set_drvdata(phy, phy_dev);
  123. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  124. if (IS_ERR(phy_provider))
  125. return PTR_ERR(phy_provider);
  126. dev_info(dev, "STiH407 USB Generic picoPHY driver probed!");
  127. return 0;
  128. }
  129. static const struct of_device_id stih407_usb2_picophy_of_match[] = {
  130. { .compatible = "st,stih407-usb2-phy" },
  131. { /*sentinel */ },
  132. };
  133. MODULE_DEVICE_TABLE(of, stih407_usb2_picophy_of_match);
  134. static struct platform_driver stih407_usb2_picophy_driver = {
  135. .probe = stih407_usb2_picophy_probe,
  136. .driver = {
  137. .name = "stih407-usb-genphy",
  138. .of_match_table = stih407_usb2_picophy_of_match,
  139. }
  140. };
  141. module_platform_driver(stih407_usb2_picophy_driver);
  142. MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
  143. MODULE_DESCRIPTION("STMicroelectronics Generic picoPHY driver for STiH407");
  144. MODULE_LICENSE("GPL v2");