phy-stih407-usb.c 4.8 KB

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