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