162306a36Sopenharmony_ci=============
262306a36Sopenharmony_ciPHY subsystem
362306a36Sopenharmony_ci=============
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci:Author: Kishon Vijay Abraham I <kishon@ti.com>
662306a36Sopenharmony_ci
762306a36Sopenharmony_ciThis document explains the Generic PHY Framework along with the APIs provided,
862306a36Sopenharmony_ciand how-to-use.
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ciIntroduction
1162306a36Sopenharmony_ci============
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci*PHY* is the abbreviation for physical layer. It is used to connect a device
1462306a36Sopenharmony_cito the physical medium e.g., the USB controller has a PHY to provide functions
1562306a36Sopenharmony_cisuch as serialization, de-serialization, encoding, decoding and is responsible
1662306a36Sopenharmony_cifor obtaining the required data transmission rate. Note that some USB
1762306a36Sopenharmony_cicontrollers have PHY functionality embedded into it and others use an external
1862306a36Sopenharmony_ciPHY. Other peripherals that use PHY include Wireless LAN, Ethernet,
1962306a36Sopenharmony_ciSATA etc.
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ciThe intention of creating this framework is to bring the PHY drivers spread
2262306a36Sopenharmony_ciall over the Linux kernel to drivers/phy to increase code re-use and for
2362306a36Sopenharmony_cibetter code maintainability.
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ciThis framework will be of use only to devices that use external PHY (PHY
2662306a36Sopenharmony_cifunctionality is not embedded within the controller).
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ciRegistering/Unregistering the PHY provider
2962306a36Sopenharmony_ci==========================================
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ciPHY provider refers to an entity that implements one or more PHY instances.
3262306a36Sopenharmony_ciFor the simple case where the PHY provider implements only a single instance of
3362306a36Sopenharmony_cithe PHY, the framework provides its own implementation of of_xlate in
3462306a36Sopenharmony_ciof_phy_simple_xlate. If the PHY provider implements multiple instances, it
3562306a36Sopenharmony_cishould provide its own implementation of of_xlate. of_xlate is used only for
3662306a36Sopenharmony_cidt boot case.
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci::
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci	#define of_phy_provider_register(dev, xlate)    \
4162306a36Sopenharmony_ci		__of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci	#define devm_of_phy_provider_register(dev, xlate)       \
4462306a36Sopenharmony_ci		__devm_of_phy_provider_register((dev), NULL, THIS_MODULE,
4562306a36Sopenharmony_ci						(xlate))
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciof_phy_provider_register and devm_of_phy_provider_register macros can be used to
4862306a36Sopenharmony_ciregister the phy_provider and it takes device and of_xlate as
4962306a36Sopenharmony_ciarguments. For the dt boot case, all PHY providers should use one of the above
5062306a36Sopenharmony_ci2 macros to register the PHY provider.
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ciOften the device tree nodes associated with a PHY provider will contain a set
5362306a36Sopenharmony_ciof children that each represent a single PHY. Some bindings may nest the child
5462306a36Sopenharmony_cinodes within extra levels for context and extensibility, in which case the low
5562306a36Sopenharmony_cilevel of_phy_provider_register_full() and devm_of_phy_provider_register_full()
5662306a36Sopenharmony_cimacros can be used to override the node containing the children.
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci::
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	#define of_phy_provider_register_full(dev, children, xlate) \
6162306a36Sopenharmony_ci		__of_phy_provider_register(dev, children, THIS_MODULE, xlate)
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	#define devm_of_phy_provider_register_full(dev, children, xlate) \
6462306a36Sopenharmony_ci		__devm_of_phy_provider_register_full(dev, children,
6562306a36Sopenharmony_ci						     THIS_MODULE, xlate)
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	void devm_of_phy_provider_unregister(struct device *dev,
6862306a36Sopenharmony_ci		struct phy_provider *phy_provider);
6962306a36Sopenharmony_ci	void of_phy_provider_unregister(struct phy_provider *phy_provider);
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_cidevm_of_phy_provider_unregister and of_phy_provider_unregister can be used to
7262306a36Sopenharmony_ciunregister the PHY.
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ciCreating the PHY
7562306a36Sopenharmony_ci================
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ciThe PHY driver should create the PHY in order for other peripheral controllers
7862306a36Sopenharmony_cito make use of it. The PHY framework provides 2 APIs to create the PHY.
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci::
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	struct phy *phy_create(struct device *dev, struct device_node *node,
8362306a36Sopenharmony_ci			       const struct phy_ops *ops);
8462306a36Sopenharmony_ci	struct phy *devm_phy_create(struct device *dev,
8562306a36Sopenharmony_ci				    struct device_node *node,
8662306a36Sopenharmony_ci				    const struct phy_ops *ops);
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ciThe PHY drivers can use one of the above 2 APIs to create the PHY by passing
8962306a36Sopenharmony_cithe device pointer and phy ops.
9062306a36Sopenharmony_ciphy_ops is a set of function pointers for performing PHY operations such as
9162306a36Sopenharmony_ciinit, exit, power_on and power_off.
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ciInorder to dereference the private data (in phy_ops), the phy provider driver
9462306a36Sopenharmony_cican use phy_set_drvdata() after creating the PHY and use phy_get_drvdata() in
9562306a36Sopenharmony_ciphy_ops to get back the private data.
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ciGetting a reference to the PHY
9862306a36Sopenharmony_ci==============================
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ciBefore the controller can make use of the PHY, it has to get a reference to
10162306a36Sopenharmony_ciit. This framework provides the following APIs to get a reference to the PHY.
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci::
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	struct phy *phy_get(struct device *dev, const char *string);
10662306a36Sopenharmony_ci	struct phy *devm_phy_get(struct device *dev, const char *string);
10762306a36Sopenharmony_ci	struct phy *devm_phy_optional_get(struct device *dev,
10862306a36Sopenharmony_ci					  const char *string);
10962306a36Sopenharmony_ci	struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
11062306a36Sopenharmony_ci				    const char *con_id);
11162306a36Sopenharmony_ci	struct phy *devm_of_phy_optional_get(struct device *dev,
11262306a36Sopenharmony_ci					     struct device_node *np,
11362306a36Sopenharmony_ci					     const char *con_id);
11462306a36Sopenharmony_ci	struct phy *devm_of_phy_get_by_index(struct device *dev,
11562306a36Sopenharmony_ci					     struct device_node *np,
11662306a36Sopenharmony_ci					     int index);
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ciphy_get, devm_phy_get and devm_phy_optional_get can be used to get the PHY.
11962306a36Sopenharmony_ciIn the case of dt boot, the string arguments
12062306a36Sopenharmony_cishould contain the phy name as given in the dt data and in the case of
12162306a36Sopenharmony_cinon-dt boot, it should contain the label of the PHY.  The two
12262306a36Sopenharmony_cidevm_phy_get associates the device with the PHY using devres on
12362306a36Sopenharmony_cisuccessful PHY get. On driver detach, release function is invoked on
12462306a36Sopenharmony_cithe devres data and devres data is freed.
12562306a36Sopenharmony_ciThe _optional_get variants should be used when the phy is optional. These
12662306a36Sopenharmony_cifunctions will never return -ENODEV, but instead return NULL when
12762306a36Sopenharmony_cithe phy cannot be found.
12862306a36Sopenharmony_ciSome generic drivers, such as ehci, may use multiple phys. In this case,
12962306a36Sopenharmony_cidevm_of_phy_get or devm_of_phy_get_by_index can be used to get a phy
13062306a36Sopenharmony_cireference based on name or index.
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ciIt should be noted that NULL is a valid phy reference. All phy
13362306a36Sopenharmony_ciconsumer calls on the NULL phy become NOPs. That is the release calls,
13462306a36Sopenharmony_cithe phy_init() and phy_exit() calls, and phy_power_on() and
13562306a36Sopenharmony_ciphy_power_off() calls are all NOP when applied to a NULL phy. The NULL
13662306a36Sopenharmony_ciphy is useful in devices for handling optional phy devices.
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ciOrder of API calls
13962306a36Sopenharmony_ci==================
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ciThe general order of calls should be::
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci    [devm_][of_]phy_get()
14462306a36Sopenharmony_ci    phy_init()
14562306a36Sopenharmony_ci    phy_power_on()
14662306a36Sopenharmony_ci    [phy_set_mode[_ext]()]
14762306a36Sopenharmony_ci    ...
14862306a36Sopenharmony_ci    phy_power_off()
14962306a36Sopenharmony_ci    phy_exit()
15062306a36Sopenharmony_ci    [[of_]phy_put()]
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ciSome PHY drivers may not implement :c:func:`phy_init` or :c:func:`phy_power_on`,
15362306a36Sopenharmony_cibut controllers should always call these functions to be compatible with other
15462306a36Sopenharmony_ciPHYs. Some PHYs may require :c:func:`phy_set_mode <phy_set_mode_ext>`, while
15562306a36Sopenharmony_ciothers may use a default mode (typically configured via devicetree or other
15662306a36Sopenharmony_cifirmware). For compatibility, you should always call this function if you know
15762306a36Sopenharmony_ciwhat mode you will be using. Generally, this function should be called after
15862306a36Sopenharmony_ci:c:func:`phy_power_on`, although some PHY drivers may allow it at any time.
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ciReleasing a reference to the PHY
16162306a36Sopenharmony_ci================================
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ciWhen the controller no longer needs the PHY, it has to release the reference
16462306a36Sopenharmony_cito the PHY it has obtained using the APIs mentioned in the above section. The
16562306a36Sopenharmony_ciPHY framework provides 2 APIs to release a reference to the PHY.
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci::
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	void phy_put(struct phy *phy);
17062306a36Sopenharmony_ci	void devm_phy_put(struct device *dev, struct phy *phy);
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ciBoth these APIs are used to release a reference to the PHY and devm_phy_put
17362306a36Sopenharmony_cidestroys the devres associated with this PHY.
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ciDestroying the PHY
17662306a36Sopenharmony_ci==================
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ciWhen the driver that created the PHY is unloaded, it should destroy the PHY it
17962306a36Sopenharmony_cicreated using one of the following 2 APIs::
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	void phy_destroy(struct phy *phy);
18262306a36Sopenharmony_ci	void devm_phy_destroy(struct device *dev, struct phy *phy);
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ciBoth these APIs destroy the PHY and devm_phy_destroy destroys the devres
18562306a36Sopenharmony_ciassociated with this PHY.
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ciPM Runtime
18862306a36Sopenharmony_ci==========
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ciThis subsystem is pm runtime enabled. So while creating the PHY,
19162306a36Sopenharmony_cipm_runtime_enable of the phy device created by this subsystem is called and
19262306a36Sopenharmony_ciwhile destroying the PHY, pm_runtime_disable is called. Note that the phy
19362306a36Sopenharmony_cidevice created by this subsystem will be a child of the device that calls
19462306a36Sopenharmony_ciphy_create (PHY provider device).
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ciSo pm_runtime_get_sync of the phy_device created by this subsystem will invoke
19762306a36Sopenharmony_cipm_runtime_get_sync of PHY provider device because of parent-child relationship.
19862306a36Sopenharmony_ciIt should also be noted that phy_power_on and phy_power_off performs
19962306a36Sopenharmony_ciphy_pm_runtime_get_sync and phy_pm_runtime_put respectively.
20062306a36Sopenharmony_ciThere are exported APIs like phy_pm_runtime_get, phy_pm_runtime_get_sync,
20162306a36Sopenharmony_ciphy_pm_runtime_put, phy_pm_runtime_put_sync, phy_pm_runtime_allow and
20262306a36Sopenharmony_ciphy_pm_runtime_forbid for performing PM operations.
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ciPHY Mappings
20562306a36Sopenharmony_ci============
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ciIn order to get reference to a PHY without help from DeviceTree, the framework
20862306a36Sopenharmony_cioffers lookups which can be compared to clkdev that allow clk structures to be
20962306a36Sopenharmony_cibound to devices. A lookup can be made during runtime when a handle to the
21062306a36Sopenharmony_cistruct phy already exists.
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ciThe framework offers the following API for registering and unregistering the
21362306a36Sopenharmony_cilookups::
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	int phy_create_lookup(struct phy *phy, const char *con_id,
21662306a36Sopenharmony_ci			      const char *dev_id);
21762306a36Sopenharmony_ci	void phy_remove_lookup(struct phy *phy, const char *con_id,
21862306a36Sopenharmony_ci			       const char *dev_id);
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ciDeviceTree Binding
22162306a36Sopenharmony_ci==================
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ciThe documentation for PHY dt binding can be found @
22462306a36Sopenharmony_ciDocumentation/devicetree/bindings/phy/phy-bindings.txt
225