xref: /OK3568_Linux_fs/kernel/Documentation/driver-api/phy/phy.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun=============
2*4882a593SmuzhiyunPHY subsystem
3*4882a593Smuzhiyun=============
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun:Author: Kishon Vijay Abraham I <kishon@ti.com>
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunThis document explains the Generic PHY Framework along with the APIs provided,
8*4882a593Smuzhiyunand how-to-use.
9*4882a593Smuzhiyun
10*4882a593SmuzhiyunIntroduction
11*4882a593Smuzhiyun============
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun*PHY* is the abbreviation for physical layer. It is used to connect a device
14*4882a593Smuzhiyunto the physical medium e.g., the USB controller has a PHY to provide functions
15*4882a593Smuzhiyunsuch as serialization, de-serialization, encoding, decoding and is responsible
16*4882a593Smuzhiyunfor obtaining the required data transmission rate. Note that some USB
17*4882a593Smuzhiyuncontrollers have PHY functionality embedded into it and others use an external
18*4882a593SmuzhiyunPHY. Other peripherals that use PHY include Wireless LAN, Ethernet,
19*4882a593SmuzhiyunSATA etc.
20*4882a593Smuzhiyun
21*4882a593SmuzhiyunThe intention of creating this framework is to bring the PHY drivers spread
22*4882a593Smuzhiyunall over the Linux kernel to drivers/phy to increase code re-use and for
23*4882a593Smuzhiyunbetter code maintainability.
24*4882a593Smuzhiyun
25*4882a593SmuzhiyunThis framework will be of use only to devices that use external PHY (PHY
26*4882a593Smuzhiyunfunctionality is not embedded within the controller).
27*4882a593Smuzhiyun
28*4882a593SmuzhiyunRegistering/Unregistering the PHY provider
29*4882a593Smuzhiyun==========================================
30*4882a593Smuzhiyun
31*4882a593SmuzhiyunPHY provider refers to an entity that implements one or more PHY instances.
32*4882a593SmuzhiyunFor the simple case where the PHY provider implements only a single instance of
33*4882a593Smuzhiyunthe PHY, the framework provides its own implementation of of_xlate in
34*4882a593Smuzhiyunof_phy_simple_xlate. If the PHY provider implements multiple instances, it
35*4882a593Smuzhiyunshould provide its own implementation of of_xlate. of_xlate is used only for
36*4882a593Smuzhiyundt boot case.
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun::
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun	#define of_phy_provider_register(dev, xlate)    \
41*4882a593Smuzhiyun		__of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun	#define devm_of_phy_provider_register(dev, xlate)       \
44*4882a593Smuzhiyun		__devm_of_phy_provider_register((dev), NULL, THIS_MODULE,
45*4882a593Smuzhiyun						(xlate))
46*4882a593Smuzhiyun
47*4882a593Smuzhiyunof_phy_provider_register and devm_of_phy_provider_register macros can be used to
48*4882a593Smuzhiyunregister the phy_provider and it takes device and of_xlate as
49*4882a593Smuzhiyunarguments. For the dt boot case, all PHY providers should use one of the above
50*4882a593Smuzhiyun2 macros to register the PHY provider.
51*4882a593Smuzhiyun
52*4882a593SmuzhiyunOften the device tree nodes associated with a PHY provider will contain a set
53*4882a593Smuzhiyunof children that each represent a single PHY. Some bindings may nest the child
54*4882a593Smuzhiyunnodes within extra levels for context and extensibility, in which case the low
55*4882a593Smuzhiyunlevel of_phy_provider_register_full() and devm_of_phy_provider_register_full()
56*4882a593Smuzhiyunmacros can be used to override the node containing the children.
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun::
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun	#define of_phy_provider_register_full(dev, children, xlate) \
61*4882a593Smuzhiyun		__of_phy_provider_register(dev, children, THIS_MODULE, xlate)
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun	#define devm_of_phy_provider_register_full(dev, children, xlate) \
64*4882a593Smuzhiyun		__devm_of_phy_provider_register_full(dev, children,
65*4882a593Smuzhiyun						     THIS_MODULE, xlate)
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun	void devm_of_phy_provider_unregister(struct device *dev,
68*4882a593Smuzhiyun		struct phy_provider *phy_provider);
69*4882a593Smuzhiyun	void of_phy_provider_unregister(struct phy_provider *phy_provider);
70*4882a593Smuzhiyun
71*4882a593Smuzhiyundevm_of_phy_provider_unregister and of_phy_provider_unregister can be used to
72*4882a593Smuzhiyununregister the PHY.
73*4882a593Smuzhiyun
74*4882a593SmuzhiyunCreating the PHY
75*4882a593Smuzhiyun================
76*4882a593Smuzhiyun
77*4882a593SmuzhiyunThe PHY driver should create the PHY in order for other peripheral controllers
78*4882a593Smuzhiyunto make use of it. The PHY framework provides 2 APIs to create the PHY.
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun::
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun	struct phy *phy_create(struct device *dev, struct device_node *node,
83*4882a593Smuzhiyun			       const struct phy_ops *ops);
84*4882a593Smuzhiyun	struct phy *devm_phy_create(struct device *dev,
85*4882a593Smuzhiyun				    struct device_node *node,
86*4882a593Smuzhiyun				    const struct phy_ops *ops);
87*4882a593Smuzhiyun
88*4882a593SmuzhiyunThe PHY drivers can use one of the above 2 APIs to create the PHY by passing
89*4882a593Smuzhiyunthe device pointer and phy ops.
90*4882a593Smuzhiyunphy_ops is a set of function pointers for performing PHY operations such as
91*4882a593Smuzhiyuninit, exit, power_on and power_off.
92*4882a593Smuzhiyun
93*4882a593SmuzhiyunInorder to dereference the private data (in phy_ops), the phy provider driver
94*4882a593Smuzhiyuncan use phy_set_drvdata() after creating the PHY and use phy_get_drvdata() in
95*4882a593Smuzhiyunphy_ops to get back the private data.
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun4. Getting a reference to the PHY
98*4882a593Smuzhiyun
99*4882a593SmuzhiyunBefore the controller can make use of the PHY, it has to get a reference to
100*4882a593Smuzhiyunit. This framework provides the following APIs to get a reference to the PHY.
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun::
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun	struct phy *phy_get(struct device *dev, const char *string);
105*4882a593Smuzhiyun	struct phy *phy_optional_get(struct device *dev, const char *string);
106*4882a593Smuzhiyun	struct phy *devm_phy_get(struct device *dev, const char *string);
107*4882a593Smuzhiyun	struct phy *devm_phy_optional_get(struct device *dev,
108*4882a593Smuzhiyun					  const char *string);
109*4882a593Smuzhiyun	struct phy *devm_of_phy_get_by_index(struct device *dev,
110*4882a593Smuzhiyun					     struct device_node *np,
111*4882a593Smuzhiyun					     int index);
112*4882a593Smuzhiyun
113*4882a593Smuzhiyunphy_get, phy_optional_get, devm_phy_get and devm_phy_optional_get can
114*4882a593Smuzhiyunbe used to get the PHY. In the case of dt boot, the string arguments
115*4882a593Smuzhiyunshould contain the phy name as given in the dt data and in the case of
116*4882a593Smuzhiyunnon-dt boot, it should contain the label of the PHY.  The two
117*4882a593Smuzhiyundevm_phy_get associates the device with the PHY using devres on
118*4882a593Smuzhiyunsuccessful PHY get. On driver detach, release function is invoked on
119*4882a593Smuzhiyunthe devres data and devres data is freed. phy_optional_get and
120*4882a593Smuzhiyundevm_phy_optional_get should be used when the phy is optional. These
121*4882a593Smuzhiyuntwo functions will never return -ENODEV, but instead returns NULL when
122*4882a593Smuzhiyunthe phy cannot be found.Some generic drivers, such as ehci, may use multiple
123*4882a593Smuzhiyunphys and for such drivers referencing phy(s) by name(s) does not make sense. In
124*4882a593Smuzhiyunthis case, devm_of_phy_get_by_index can be used to get a phy reference based on
125*4882a593Smuzhiyunthe index.
126*4882a593Smuzhiyun
127*4882a593SmuzhiyunIt should be noted that NULL is a valid phy reference. All phy
128*4882a593Smuzhiyunconsumer calls on the NULL phy become NOPs. That is the release calls,
129*4882a593Smuzhiyunthe phy_init() and phy_exit() calls, and phy_power_on() and
130*4882a593Smuzhiyunphy_power_off() calls are all NOP when applied to a NULL phy. The NULL
131*4882a593Smuzhiyunphy is useful in devices for handling optional phy devices.
132*4882a593Smuzhiyun
133*4882a593SmuzhiyunReleasing a reference to the PHY
134*4882a593Smuzhiyun================================
135*4882a593Smuzhiyun
136*4882a593SmuzhiyunWhen the controller no longer needs the PHY, it has to release the reference
137*4882a593Smuzhiyunto the PHY it has obtained using the APIs mentioned in the above section. The
138*4882a593SmuzhiyunPHY framework provides 2 APIs to release a reference to the PHY.
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun::
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun	void phy_put(struct phy *phy);
143*4882a593Smuzhiyun	void devm_phy_put(struct device *dev, struct phy *phy);
144*4882a593Smuzhiyun
145*4882a593SmuzhiyunBoth these APIs are used to release a reference to the PHY and devm_phy_put
146*4882a593Smuzhiyundestroys the devres associated with this PHY.
147*4882a593Smuzhiyun
148*4882a593SmuzhiyunDestroying the PHY
149*4882a593Smuzhiyun==================
150*4882a593Smuzhiyun
151*4882a593SmuzhiyunWhen the driver that created the PHY is unloaded, it should destroy the PHY it
152*4882a593Smuzhiyuncreated using one of the following 2 APIs::
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun	void phy_destroy(struct phy *phy);
155*4882a593Smuzhiyun	void devm_phy_destroy(struct device *dev, struct phy *phy);
156*4882a593Smuzhiyun
157*4882a593SmuzhiyunBoth these APIs destroy the PHY and devm_phy_destroy destroys the devres
158*4882a593Smuzhiyunassociated with this PHY.
159*4882a593Smuzhiyun
160*4882a593SmuzhiyunPM Runtime
161*4882a593Smuzhiyun==========
162*4882a593Smuzhiyun
163*4882a593SmuzhiyunThis subsystem is pm runtime enabled. So while creating the PHY,
164*4882a593Smuzhiyunpm_runtime_enable of the phy device created by this subsystem is called and
165*4882a593Smuzhiyunwhile destroying the PHY, pm_runtime_disable is called. Note that the phy
166*4882a593Smuzhiyundevice created by this subsystem will be a child of the device that calls
167*4882a593Smuzhiyunphy_create (PHY provider device).
168*4882a593Smuzhiyun
169*4882a593SmuzhiyunSo pm_runtime_get_sync of the phy_device created by this subsystem will invoke
170*4882a593Smuzhiyunpm_runtime_get_sync of PHY provider device because of parent-child relationship.
171*4882a593SmuzhiyunIt should also be noted that phy_power_on and phy_power_off performs
172*4882a593Smuzhiyunphy_pm_runtime_get_sync and phy_pm_runtime_put respectively.
173*4882a593SmuzhiyunThere are exported APIs like phy_pm_runtime_get, phy_pm_runtime_get_sync,
174*4882a593Smuzhiyunphy_pm_runtime_put, phy_pm_runtime_put_sync, phy_pm_runtime_allow and
175*4882a593Smuzhiyunphy_pm_runtime_forbid for performing PM operations.
176*4882a593Smuzhiyun
177*4882a593SmuzhiyunPHY Mappings
178*4882a593Smuzhiyun============
179*4882a593Smuzhiyun
180*4882a593SmuzhiyunIn order to get reference to a PHY without help from DeviceTree, the framework
181*4882a593Smuzhiyunoffers lookups which can be compared to clkdev that allow clk structures to be
182*4882a593Smuzhiyunbound to devices. A lookup can be made during runtime when a handle to the
183*4882a593Smuzhiyunstruct phy already exists.
184*4882a593Smuzhiyun
185*4882a593SmuzhiyunThe framework offers the following API for registering and unregistering the
186*4882a593Smuzhiyunlookups::
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun	int phy_create_lookup(struct phy *phy, const char *con_id,
189*4882a593Smuzhiyun			      const char *dev_id);
190*4882a593Smuzhiyun	void phy_remove_lookup(struct phy *phy, const char *con_id,
191*4882a593Smuzhiyun			       const char *dev_id);
192*4882a593Smuzhiyun
193*4882a593SmuzhiyunDeviceTree Binding
194*4882a593Smuzhiyun==================
195*4882a593Smuzhiyun
196*4882a593SmuzhiyunThe documentation for PHY dt binding can be found @
197*4882a593SmuzhiyunDocumentation/devicetree/bindings/phy/phy-bindings.txt
198