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