phy.rst 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. =============
  2. PHY subsystem
  3. =============
  4. :Author: Kishon Vijay Abraham I <kishon@ti.com>
  5. This document explains the Generic PHY Framework along with the APIs provided,
  6. and how-to-use.
  7. Introduction
  8. ============
  9. *PHY* is the abbreviation for physical layer. It is used to connect a device
  10. to the physical medium e.g., the USB controller has a PHY to provide functions
  11. such as serialization, de-serialization, encoding, decoding and is responsible
  12. for obtaining the required data transmission rate. Note that some USB
  13. controllers have PHY functionality embedded into it and others use an external
  14. PHY. Other peripherals that use PHY include Wireless LAN, Ethernet,
  15. SATA etc.
  16. The intention of creating this framework is to bring the PHY drivers spread
  17. all over the Linux kernel to drivers/phy to increase code reuse and for
  18. better code maintainability.
  19. This framework will be of use only to devices that use external PHY (PHY
  20. functionality is not embedded within the controller).
  21. Registering/Unregistering the PHY provider
  22. ==========================================
  23. PHY provider refers to an entity that implements one or more PHY instances.
  24. For the simple case where the PHY provider implements only a single instance of
  25. the PHY, the framework provides its own implementation of of_xlate in
  26. of_phy_simple_xlate. If the PHY provider implements multiple instances, it
  27. should provide its own implementation of of_xlate. of_xlate is used only for
  28. dt boot case.
  29. ::
  30. #define of_phy_provider_register(dev, xlate) \
  31. __of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
  32. #define devm_of_phy_provider_register(dev, xlate) \
  33. __devm_of_phy_provider_register((dev), NULL, THIS_MODULE,
  34. (xlate))
  35. of_phy_provider_register and devm_of_phy_provider_register macros can be used to
  36. register the phy_provider and it takes device and of_xlate as
  37. arguments. For the dt boot case, all PHY providers should use one of the above
  38. 2 macros to register the PHY provider.
  39. Often the device tree nodes associated with a PHY provider will contain a set
  40. of children that each represent a single PHY. Some bindings may nest the child
  41. nodes within extra levels for context and extensibility, in which case the low
  42. level of_phy_provider_register_full() and devm_of_phy_provider_register_full()
  43. macros can be used to override the node containing the children.
  44. ::
  45. #define of_phy_provider_register_full(dev, children, xlate) \
  46. __of_phy_provider_register(dev, children, THIS_MODULE, xlate)
  47. #define devm_of_phy_provider_register_full(dev, children, xlate) \
  48. __devm_of_phy_provider_register_full(dev, children,
  49. THIS_MODULE, xlate)
  50. void devm_of_phy_provider_unregister(struct device *dev,
  51. struct phy_provider *phy_provider);
  52. void of_phy_provider_unregister(struct phy_provider *phy_provider);
  53. devm_of_phy_provider_unregister and of_phy_provider_unregister can be used to
  54. unregister the PHY.
  55. Creating the PHY
  56. ================
  57. The PHY driver should create the PHY in order for other peripheral controllers
  58. to make use of it. The PHY framework provides 2 APIs to create the PHY.
  59. ::
  60. struct phy *phy_create(struct device *dev, struct device_node *node,
  61. const struct phy_ops *ops);
  62. struct phy *devm_phy_create(struct device *dev,
  63. struct device_node *node,
  64. const struct phy_ops *ops);
  65. The PHY drivers can use one of the above 2 APIs to create the PHY by passing
  66. the device pointer and phy ops.
  67. phy_ops is a set of function pointers for performing PHY operations such as
  68. init, exit, power_on and power_off.
  69. Inorder to dereference the private data (in phy_ops), the phy provider driver
  70. can use phy_set_drvdata() after creating the PHY and use phy_get_drvdata() in
  71. phy_ops to get back the private data.
  72. Getting a reference to the PHY
  73. ==============================
  74. Before the controller can make use of the PHY, it has to get a reference to
  75. it. This framework provides the following APIs to get a reference to the PHY.
  76. ::
  77. struct phy *phy_get(struct device *dev, const char *string);
  78. struct phy *devm_phy_get(struct device *dev, const char *string);
  79. struct phy *devm_phy_optional_get(struct device *dev,
  80. const char *string);
  81. struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
  82. const char *con_id);
  83. struct phy *devm_of_phy_optional_get(struct device *dev,
  84. struct device_node *np,
  85. const char *con_id);
  86. struct phy *devm_of_phy_get_by_index(struct device *dev,
  87. struct device_node *np,
  88. int index);
  89. phy_get, devm_phy_get and devm_phy_optional_get can be used to get the PHY.
  90. In the case of dt boot, the string arguments
  91. should contain the phy name as given in the dt data and in the case of
  92. non-dt boot, it should contain the label of the PHY. The two
  93. devm_phy_get associates the device with the PHY using devres on
  94. successful PHY get. On driver detach, release function is invoked on
  95. the devres data and devres data is freed.
  96. The _optional_get variants should be used when the phy is optional. These
  97. functions will never return -ENODEV, but instead return NULL when
  98. the phy cannot be found.
  99. Some generic drivers, such as ehci, may use multiple phys. In this case,
  100. devm_of_phy_get or devm_of_phy_get_by_index can be used to get a phy
  101. reference based on name or index.
  102. It should be noted that NULL is a valid phy reference. All phy
  103. consumer calls on the NULL phy become NOPs. That is the release calls,
  104. the phy_init() and phy_exit() calls, and phy_power_on() and
  105. phy_power_off() calls are all NOP when applied to a NULL phy. The NULL
  106. phy is useful in devices for handling optional phy devices.
  107. Order of API calls
  108. ==================
  109. The general order of calls should be::
  110. [devm_][of_]phy_get()
  111. phy_init()
  112. phy_power_on()
  113. [phy_set_mode[_ext]()]
  114. ...
  115. phy_power_off()
  116. phy_exit()
  117. [[of_]phy_put()]
  118. Some PHY drivers may not implement :c:func:`phy_init` or :c:func:`phy_power_on`,
  119. but controllers should always call these functions to be compatible with other
  120. PHYs. Some PHYs may require :c:func:`phy_set_mode <phy_set_mode_ext>`, while
  121. others may use a default mode (typically configured via devicetree or other
  122. firmware). For compatibility, you should always call this function if you know
  123. what mode you will be using. Generally, this function should be called after
  124. :c:func:`phy_power_on`, although some PHY drivers may allow it at any time.
  125. Releasing a reference to the PHY
  126. ================================
  127. When the controller no longer needs the PHY, it has to release the reference
  128. to the PHY it has obtained using the APIs mentioned in the above section. The
  129. PHY framework provides 2 APIs to release a reference to the PHY.
  130. ::
  131. void phy_put(struct phy *phy);
  132. void devm_phy_put(struct device *dev, struct phy *phy);
  133. Both these APIs are used to release a reference to the PHY and devm_phy_put
  134. destroys the devres associated with this PHY.
  135. Destroying the PHY
  136. ==================
  137. When the driver that created the PHY is unloaded, it should destroy the PHY it
  138. created using one of the following 2 APIs::
  139. void phy_destroy(struct phy *phy);
  140. void devm_phy_destroy(struct device *dev, struct phy *phy);
  141. Both these APIs destroy the PHY and devm_phy_destroy destroys the devres
  142. associated with this PHY.
  143. PM Runtime
  144. ==========
  145. This subsystem is pm runtime enabled. So while creating the PHY,
  146. pm_runtime_enable of the phy device created by this subsystem is called and
  147. while destroying the PHY, pm_runtime_disable is called. Note that the phy
  148. device created by this subsystem will be a child of the device that calls
  149. phy_create (PHY provider device).
  150. So pm_runtime_get_sync of the phy_device created by this subsystem will invoke
  151. pm_runtime_get_sync of PHY provider device because of parent-child relationship.
  152. It should also be noted that phy_power_on and phy_power_off performs
  153. phy_pm_runtime_get_sync and phy_pm_runtime_put respectively.
  154. There are exported APIs like phy_pm_runtime_get, phy_pm_runtime_get_sync,
  155. phy_pm_runtime_put and phy_pm_runtime_put_sync for performing PM operations.
  156. PHY Mappings
  157. ============
  158. In order to get reference to a PHY without help from DeviceTree, the framework
  159. offers lookups which can be compared to clkdev that allow clk structures to be
  160. bound to devices. A lookup can be made during runtime when a handle to the
  161. struct phy already exists.
  162. The framework offers the following API for registering and unregistering the
  163. lookups::
  164. int phy_create_lookup(struct phy *phy, const char *con_id,
  165. const char *dev_id);
  166. void phy_remove_lookup(struct phy *phy, const char *con_id,
  167. const char *dev_id);
  168. DeviceTree Binding
  169. ==================
  170. The documentation for PHY dt binding can be found @
  171. Documentation/devicetree/bindings/phy/phy-bindings.txt