xref: /linux-6.15/include/linux/phy/phy.h (revision 2af8caee)
1 /*
2  * phy.h -- generic phy header file
3  *
4  * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
5  *
6  * Author: Kishon Vijay Abraham I <[email protected]>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #ifndef __DRIVERS_PHY_H
15 #define __DRIVERS_PHY_H
16 
17 #include <linux/err.h>
18 #include <linux/of.h>
19 #include <linux/device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regulator/consumer.h>
22 
23 struct phy;
24 
25 enum phy_mode {
26 	PHY_MODE_INVALID,
27 	PHY_MODE_USB_HOST,
28 	PHY_MODE_USB_HOST_LS,
29 	PHY_MODE_USB_HOST_FS,
30 	PHY_MODE_USB_HOST_HS,
31 	PHY_MODE_USB_HOST_SS,
32 	PHY_MODE_USB_DEVICE,
33 	PHY_MODE_USB_DEVICE_LS,
34 	PHY_MODE_USB_DEVICE_FS,
35 	PHY_MODE_USB_DEVICE_HS,
36 	PHY_MODE_USB_DEVICE_SS,
37 	PHY_MODE_USB_OTG,
38 	PHY_MODE_SGMII,
39 	PHY_MODE_2500SGMII,
40 	PHY_MODE_QSGMII,
41 	PHY_MODE_10GKR,
42 	PHY_MODE_UFS_HS_A,
43 	PHY_MODE_UFS_HS_B,
44 	PHY_MODE_PCIE,
45 	PHY_MODE_ETHERNET,
46 };
47 
48 /**
49  * struct phy_ops - set of function pointers for performing phy operations
50  * @init: operation to be performed for initializing phy
51  * @exit: operation to be performed while exiting
52  * @power_on: powering on the phy
53  * @power_off: powering off the phy
54  * @set_mode: set the mode of the phy
55  * @reset: resetting the phy
56  * @calibrate: calibrate the phy
57  * @owner: the module owner containing the ops
58  */
59 struct phy_ops {
60 	int	(*init)(struct phy *phy);
61 	int	(*exit)(struct phy *phy);
62 	int	(*power_on)(struct phy *phy);
63 	int	(*power_off)(struct phy *phy);
64 	int	(*set_mode)(struct phy *phy, enum phy_mode mode, int submode);
65 	int	(*reset)(struct phy *phy);
66 	int	(*calibrate)(struct phy *phy);
67 	struct module *owner;
68 };
69 
70 /**
71  * struct phy_attrs - represents phy attributes
72  * @bus_width: Data path width implemented by PHY
73  */
74 struct phy_attrs {
75 	u32			bus_width;
76 	enum phy_mode		mode;
77 };
78 
79 /**
80  * struct phy - represents the phy device
81  * @dev: phy device
82  * @id: id of the phy device
83  * @ops: function pointers for performing phy operations
84  * @init_data: list of PHY consumers (non-dt only)
85  * @mutex: mutex to protect phy_ops
86  * @init_count: used to protect when the PHY is used by multiple consumers
87  * @power_count: used to protect when the PHY is used by multiple consumers
88  * @attrs: used to specify PHY specific attributes
89  * @pwr: power regulator associated with the phy
90  */
91 struct phy {
92 	struct device		dev;
93 	int			id;
94 	const struct phy_ops	*ops;
95 	struct mutex		mutex;
96 	int			init_count;
97 	int			power_count;
98 	struct phy_attrs	attrs;
99 	struct regulator	*pwr;
100 };
101 
102 /**
103  * struct phy_provider - represents the phy provider
104  * @dev: phy provider device
105  * @children: can be used to override the default (dev->of_node) child node
106  * @owner: the module owner having of_xlate
107  * @list: to maintain a linked list of PHY providers
108  * @of_xlate: function pointer to obtain phy instance from phy pointer
109  */
110 struct phy_provider {
111 	struct device		*dev;
112 	struct device_node	*children;
113 	struct module		*owner;
114 	struct list_head	list;
115 	struct phy * (*of_xlate)(struct device *dev,
116 		struct of_phandle_args *args);
117 };
118 
119 /**
120  * struct phy_lookup - PHY association in list of phys managed by the phy driver
121  * @node: list node
122  * @dev_id: the device of the association
123  * @con_id: connection ID string on device
124  * @phy: the phy of the association
125  */
126 struct phy_lookup {
127 	struct list_head node;
128 	const char *dev_id;
129 	const char *con_id;
130 	struct phy *phy;
131 };
132 
133 #define	to_phy(a)	(container_of((a), struct phy, dev))
134 
135 #define	of_phy_provider_register(dev, xlate)	\
136 	__of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
137 
138 #define	devm_of_phy_provider_register(dev, xlate)	\
139 	__devm_of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
140 
141 #define of_phy_provider_register_full(dev, children, xlate) \
142 	__of_phy_provider_register(dev, children, THIS_MODULE, xlate)
143 
144 #define devm_of_phy_provider_register_full(dev, children, xlate) \
145 	__devm_of_phy_provider_register(dev, children, THIS_MODULE, xlate)
146 
147 static inline void phy_set_drvdata(struct phy *phy, void *data)
148 {
149 	dev_set_drvdata(&phy->dev, data);
150 }
151 
152 static inline void *phy_get_drvdata(struct phy *phy)
153 {
154 	return dev_get_drvdata(&phy->dev);
155 }
156 
157 #if IS_ENABLED(CONFIG_GENERIC_PHY)
158 int phy_pm_runtime_get(struct phy *phy);
159 int phy_pm_runtime_get_sync(struct phy *phy);
160 int phy_pm_runtime_put(struct phy *phy);
161 int phy_pm_runtime_put_sync(struct phy *phy);
162 void phy_pm_runtime_allow(struct phy *phy);
163 void phy_pm_runtime_forbid(struct phy *phy);
164 int phy_init(struct phy *phy);
165 int phy_exit(struct phy *phy);
166 int phy_power_on(struct phy *phy);
167 int phy_power_off(struct phy *phy);
168 int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode);
169 #define phy_set_mode(phy, mode) \
170 	phy_set_mode_ext(phy, mode, 0)
171 
172 static inline enum phy_mode phy_get_mode(struct phy *phy)
173 {
174 	return phy->attrs.mode;
175 }
176 int phy_reset(struct phy *phy);
177 int phy_calibrate(struct phy *phy);
178 static inline int phy_get_bus_width(struct phy *phy)
179 {
180 	return phy->attrs.bus_width;
181 }
182 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
183 {
184 	phy->attrs.bus_width = bus_width;
185 }
186 struct phy *phy_get(struct device *dev, const char *string);
187 struct phy *phy_optional_get(struct device *dev, const char *string);
188 struct phy *devm_phy_get(struct device *dev, const char *string);
189 struct phy *devm_phy_optional_get(struct device *dev, const char *string);
190 struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
191 			    const char *con_id);
192 struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
193 				     int index);
194 void phy_put(struct phy *phy);
195 void devm_phy_put(struct device *dev, struct phy *phy);
196 struct phy *of_phy_get(struct device_node *np, const char *con_id);
197 struct phy *of_phy_simple_xlate(struct device *dev,
198 	struct of_phandle_args *args);
199 struct phy *phy_create(struct device *dev, struct device_node *node,
200 		       const struct phy_ops *ops);
201 struct phy *devm_phy_create(struct device *dev, struct device_node *node,
202 			    const struct phy_ops *ops);
203 void phy_destroy(struct phy *phy);
204 void devm_phy_destroy(struct device *dev, struct phy *phy);
205 struct phy_provider *__of_phy_provider_register(struct device *dev,
206 	struct device_node *children, struct module *owner,
207 	struct phy * (*of_xlate)(struct device *dev,
208 				 struct of_phandle_args *args));
209 struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
210 	struct device_node *children, struct module *owner,
211 	struct phy * (*of_xlate)(struct device *dev,
212 				 struct of_phandle_args *args));
213 void of_phy_provider_unregister(struct phy_provider *phy_provider);
214 void devm_of_phy_provider_unregister(struct device *dev,
215 	struct phy_provider *phy_provider);
216 int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id);
217 void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id);
218 #else
219 static inline int phy_pm_runtime_get(struct phy *phy)
220 {
221 	if (!phy)
222 		return 0;
223 	return -ENOSYS;
224 }
225 
226 static inline int phy_pm_runtime_get_sync(struct phy *phy)
227 {
228 	if (!phy)
229 		return 0;
230 	return -ENOSYS;
231 }
232 
233 static inline int phy_pm_runtime_put(struct phy *phy)
234 {
235 	if (!phy)
236 		return 0;
237 	return -ENOSYS;
238 }
239 
240 static inline int phy_pm_runtime_put_sync(struct phy *phy)
241 {
242 	if (!phy)
243 		return 0;
244 	return -ENOSYS;
245 }
246 
247 static inline void phy_pm_runtime_allow(struct phy *phy)
248 {
249 	return;
250 }
251 
252 static inline void phy_pm_runtime_forbid(struct phy *phy)
253 {
254 	return;
255 }
256 
257 static inline int phy_init(struct phy *phy)
258 {
259 	if (!phy)
260 		return 0;
261 	return -ENOSYS;
262 }
263 
264 static inline int phy_exit(struct phy *phy)
265 {
266 	if (!phy)
267 		return 0;
268 	return -ENOSYS;
269 }
270 
271 static inline int phy_power_on(struct phy *phy)
272 {
273 	if (!phy)
274 		return 0;
275 	return -ENOSYS;
276 }
277 
278 static inline int phy_power_off(struct phy *phy)
279 {
280 	if (!phy)
281 		return 0;
282 	return -ENOSYS;
283 }
284 
285 static inline int phy_set_mode_ext(struct phy *phy, enum phy_mode mode,
286 				   int submode)
287 {
288 	if (!phy)
289 		return 0;
290 	return -ENOSYS;
291 }
292 
293 #define phy_set_mode(phy, mode) \
294 	phy_set_mode_ext(phy, mode, 0)
295 
296 static inline enum phy_mode phy_get_mode(struct phy *phy)
297 {
298 	return PHY_MODE_INVALID;
299 }
300 
301 static inline int phy_reset(struct phy *phy)
302 {
303 	if (!phy)
304 		return 0;
305 	return -ENOSYS;
306 }
307 
308 static inline int phy_calibrate(struct phy *phy)
309 {
310 	if (!phy)
311 		return 0;
312 	return -ENOSYS;
313 }
314 
315 static inline int phy_get_bus_width(struct phy *phy)
316 {
317 	return -ENOSYS;
318 }
319 
320 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
321 {
322 	return;
323 }
324 
325 static inline struct phy *phy_get(struct device *dev, const char *string)
326 {
327 	return ERR_PTR(-ENOSYS);
328 }
329 
330 static inline struct phy *phy_optional_get(struct device *dev,
331 					   const char *string)
332 {
333 	return ERR_PTR(-ENOSYS);
334 }
335 
336 static inline struct phy *devm_phy_get(struct device *dev, const char *string)
337 {
338 	return ERR_PTR(-ENOSYS);
339 }
340 
341 static inline struct phy *devm_phy_optional_get(struct device *dev,
342 						const char *string)
343 {
344 	return NULL;
345 }
346 
347 static inline struct phy *devm_of_phy_get(struct device *dev,
348 					  struct device_node *np,
349 					  const char *con_id)
350 {
351 	return ERR_PTR(-ENOSYS);
352 }
353 
354 static inline struct phy *devm_of_phy_get_by_index(struct device *dev,
355 						   struct device_node *np,
356 						   int index)
357 {
358 	return ERR_PTR(-ENOSYS);
359 }
360 
361 static inline void phy_put(struct phy *phy)
362 {
363 }
364 
365 static inline void devm_phy_put(struct device *dev, struct phy *phy)
366 {
367 }
368 
369 static inline struct phy *of_phy_get(struct device_node *np, const char *con_id)
370 {
371 	return ERR_PTR(-ENOSYS);
372 }
373 
374 static inline struct phy *of_phy_simple_xlate(struct device *dev,
375 	struct of_phandle_args *args)
376 {
377 	return ERR_PTR(-ENOSYS);
378 }
379 
380 static inline struct phy *phy_create(struct device *dev,
381 				     struct device_node *node,
382 				     const struct phy_ops *ops)
383 {
384 	return ERR_PTR(-ENOSYS);
385 }
386 
387 static inline struct phy *devm_phy_create(struct device *dev,
388 					  struct device_node *node,
389 					  const struct phy_ops *ops)
390 {
391 	return ERR_PTR(-ENOSYS);
392 }
393 
394 static inline void phy_destroy(struct phy *phy)
395 {
396 }
397 
398 static inline void devm_phy_destroy(struct device *dev, struct phy *phy)
399 {
400 }
401 
402 static inline struct phy_provider *__of_phy_provider_register(
403 	struct device *dev, struct device_node *children, struct module *owner,
404 	struct phy * (*of_xlate)(struct device *dev,
405 				 struct of_phandle_args *args))
406 {
407 	return ERR_PTR(-ENOSYS);
408 }
409 
410 static inline struct phy_provider *__devm_of_phy_provider_register(struct device
411 	*dev, struct device_node *children, struct module *owner,
412 	struct phy * (*of_xlate)(struct device *dev,
413 				 struct of_phandle_args *args))
414 {
415 	return ERR_PTR(-ENOSYS);
416 }
417 
418 static inline void of_phy_provider_unregister(struct phy_provider *phy_provider)
419 {
420 }
421 
422 static inline void devm_of_phy_provider_unregister(struct device *dev,
423 	struct phy_provider *phy_provider)
424 {
425 }
426 static inline int
427 phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
428 {
429 	return 0;
430 }
431 static inline void phy_remove_lookup(struct phy *phy, const char *con_id,
432 				     const char *dev_id) { }
433 #endif
434 
435 #endif /* __DRIVERS_PHY_H */
436