phy.txt 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. PHY SUBSYSTEM
  2. Kishon Vijay Abraham I <kishon@ti.com>
  3. This document explains the Generic PHY Framework along with the APIs provided,
  4. and how-to-use.
  5. 1. Introduction
  6. *PHY* is the abbreviation for physical layer. It is used to connect a device
  7. to the physical medium e.g., the USB controller has a PHY to provide functions
  8. such as serialization, de-serialization, encoding, decoding and is responsible
  9. for obtaining the required data transmission rate. Note that some USB
  10. controllers have PHY functionality embedded into it and others use an external
  11. PHY. Other peripherals that use PHY include Wireless LAN, Ethernet,
  12. SATA etc.
  13. The intention of creating this framework is to bring the PHY drivers spread
  14. all over the Linux kernel to drivers/phy to increase code re-use and for
  15. better code maintainability.
  16. This framework will be of use only to devices that use external PHY (PHY
  17. functionality is not embedded within the controller).
  18. 2. Registering/Unregistering the PHY provider
  19. PHY provider refers to an entity that implements one or more PHY instances.
  20. For the simple case where the PHY provider implements only a single instance of
  21. the PHY, the framework provides its own implementation of of_xlate in
  22. of_phy_simple_xlate. If the PHY provider implements multiple instances, it
  23. should provide its own implementation of of_xlate. of_xlate is used only for
  24. dt boot case.
  25. #define of_phy_provider_register(dev, xlate) \
  26. __of_phy_provider_register((dev), THIS_MODULE, (xlate))
  27. #define devm_of_phy_provider_register(dev, xlate) \
  28. __devm_of_phy_provider_register((dev), THIS_MODULE, (xlate))
  29. of_phy_provider_register and devm_of_phy_provider_register macros can be used to
  30. register the phy_provider and it takes device and of_xlate as
  31. arguments. For the dt boot case, all PHY providers should use one of the above
  32. 2 macros to register the PHY provider.
  33. void devm_of_phy_provider_unregister(struct device *dev,
  34. struct phy_provider *phy_provider);
  35. void of_phy_provider_unregister(struct phy_provider *phy_provider);
  36. devm_of_phy_provider_unregister and of_phy_provider_unregister can be used to
  37. unregister the PHY.
  38. 3. Creating the PHY
  39. The PHY driver should create the PHY in order for other peripheral controllers
  40. to make use of it. The PHY framework provides 2 APIs to create the PHY.
  41. struct phy *phy_create(struct device *dev, struct device_node *node,
  42. const struct phy_ops *ops,
  43. struct phy_init_data *init_data);
  44. struct phy *devm_phy_create(struct device *dev, struct device_node *node,
  45. const struct phy_ops *ops,
  46. struct phy_init_data *init_data);
  47. The PHY drivers can use one of the above 2 APIs to create the PHY by passing
  48. the device pointer, phy ops and init_data.
  49. phy_ops is a set of function pointers for performing PHY operations such as
  50. init, exit, power_on and power_off. *init_data* is mandatory to get a reference
  51. to the PHY in the case of non-dt boot. See section *Board File Initialization*
  52. on how init_data should be used.
  53. Inorder to dereference the private data (in phy_ops), the phy provider driver
  54. can use phy_set_drvdata() after creating the PHY and use phy_get_drvdata() in
  55. phy_ops to get back the private data.
  56. 4. Getting a reference to the PHY
  57. Before the controller can make use of the PHY, it has to get a reference to
  58. it. This framework provides the following APIs to get a reference to the PHY.
  59. struct phy *phy_get(struct device *dev, const char *string);
  60. struct phy *phy_optional_get(struct device *dev, const char *string);
  61. struct phy *devm_phy_get(struct device *dev, const char *string);
  62. struct phy *devm_phy_optional_get(struct device *dev, const char *string);
  63. phy_get, phy_optional_get, devm_phy_get and devm_phy_optional_get can
  64. be used to get the PHY. In the case of dt boot, the string arguments
  65. should contain the phy name as given in the dt data and in the case of
  66. non-dt boot, it should contain the label of the PHY. The two
  67. devm_phy_get associates the device with the PHY using devres on
  68. successful PHY get. On driver detach, release function is invoked on
  69. the the devres data and devres data is freed. phy_optional_get and
  70. devm_phy_optional_get should be used when the phy is optional. These
  71. two functions will never return -ENODEV, but instead returns NULL when
  72. the phy cannot be found.
  73. It should be noted that NULL is a valid phy reference. All phy
  74. consumer calls on the NULL phy become NOPs. That is the release calls,
  75. the phy_init() and phy_exit() calls, and phy_power_on() and
  76. phy_power_off() calls are all NOP when applied to a NULL phy. The NULL
  77. phy is useful in devices for handling optional phy devices.
  78. 5. Releasing a reference to the PHY
  79. When the controller no longer needs the PHY, it has to release the reference
  80. to the PHY it has obtained using the APIs mentioned in the above section. The
  81. PHY framework provides 2 APIs to release a reference to the PHY.
  82. void phy_put(struct phy *phy);
  83. void devm_phy_put(struct device *dev, struct phy *phy);
  84. Both these APIs are used to release a reference to the PHY and devm_phy_put
  85. destroys the devres associated with this PHY.
  86. 6. Destroying the PHY
  87. When the driver that created the PHY is unloaded, it should destroy the PHY it
  88. created using one of the following 2 APIs.
  89. void phy_destroy(struct phy *phy);
  90. void devm_phy_destroy(struct device *dev, struct phy *phy);
  91. Both these APIs destroy the PHY and devm_phy_destroy destroys the devres
  92. associated with this PHY.
  93. 7. PM Runtime
  94. This subsystem is pm runtime enabled. So while creating the PHY,
  95. pm_runtime_enable of the phy device created by this subsystem is called and
  96. while destroying the PHY, pm_runtime_disable is called. Note that the phy
  97. device created by this subsystem will be a child of the device that calls
  98. phy_create (PHY provider device).
  99. So pm_runtime_get_sync of the phy_device created by this subsystem will invoke
  100. pm_runtime_get_sync of PHY provider device because of parent-child relationship.
  101. It should also be noted that phy_power_on and phy_power_off performs
  102. phy_pm_runtime_get_sync and phy_pm_runtime_put respectively.
  103. There are exported APIs like phy_pm_runtime_get, phy_pm_runtime_get_sync,
  104. phy_pm_runtime_put, phy_pm_runtime_put_sync, phy_pm_runtime_allow and
  105. phy_pm_runtime_forbid for performing PM operations.
  106. 8. Board File Initialization
  107. Certain board file initialization is necessary in order to get a reference
  108. to the PHY in the case of non-dt boot.
  109. Say we have a single device that implements 3 PHYs that of USB, SATA and PCIe,
  110. then in the board file the following initialization should be done.
  111. struct phy_consumer consumers[] = {
  112. PHY_CONSUMER("dwc3.0", "usb"),
  113. PHY_CONSUMER("pcie.0", "pcie"),
  114. PHY_CONSUMER("sata.0", "sata"),
  115. };
  116. PHY_CONSUMER takes 2 parameters, first is the device name of the controller
  117. (PHY consumer) and second is the port name.
  118. struct phy_init_data init_data = {
  119. .consumers = consumers,
  120. .num_consumers = ARRAY_SIZE(consumers),
  121. };
  122. static const struct platform_device pipe3_phy_dev = {
  123. .name = "pipe3-phy",
  124. .id = -1,
  125. .dev = {
  126. .platform_data = {
  127. .init_data = &init_data,
  128. },
  129. },
  130. };
  131. then, while doing phy_create, the PHY driver should pass this init_data
  132. phy_create(dev, ops, pdata->init_data);
  133. and the controller driver (phy consumer) should pass the port name along with
  134. the device to get a reference to the PHY
  135. phy_get(dev, "pcie");
  136. 9. DeviceTree Binding
  137. The documentation for PHY dt binding can be found @
  138. Documentation/devicetree/bindings/phy/phy-bindings.txt