xref: /linux-6.15/include/linux/phy/phy.h (revision aeaac93d)
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_UFS_HS_A,
39 	PHY_MODE_UFS_HS_B,
40 	PHY_MODE_PCIE,
41 	PHY_MODE_ETHERNET,
42 	PHY_MODE_MIPI_DPHY,
43 };
44 
45 /**
46  * union phy_configure_opts - Opaque generic phy configuration
47  */
48 union phy_configure_opts {
49 };
50 
51 /**
52  * struct phy_ops - set of function pointers for performing phy operations
53  * @init: operation to be performed for initializing phy
54  * @exit: operation to be performed while exiting
55  * @power_on: powering on the phy
56  * @power_off: powering off the phy
57  * @set_mode: set the mode of the phy
58  * @reset: resetting the phy
59  * @calibrate: calibrate the phy
60  * @owner: the module owner containing the ops
61  */
62 struct phy_ops {
63 	int	(*init)(struct phy *phy);
64 	int	(*exit)(struct phy *phy);
65 	int	(*power_on)(struct phy *phy);
66 	int	(*power_off)(struct phy *phy);
67 	int	(*set_mode)(struct phy *phy, enum phy_mode mode, int submode);
68 
69 	/**
70 	 * @configure:
71 	 *
72 	 * Optional.
73 	 *
74 	 * Used to change the PHY parameters. phy_init() must have
75 	 * been called on the phy.
76 	 *
77 	 * Returns: 0 if successful, an negative error code otherwise
78 	 */
79 	int	(*configure)(struct phy *phy, union phy_configure_opts *opts);
80 
81 	/**
82 	 * @validate:
83 	 *
84 	 * Optional.
85 	 *
86 	 * Used to check that the current set of parameters can be
87 	 * handled by the phy. Implementations are free to tune the
88 	 * parameters passed as arguments if needed by some
89 	 * implementation detail or constraints. It must not change
90 	 * any actual configuration of the PHY, so calling it as many
91 	 * times as deemed fit by the consumer must have no side
92 	 * effect.
93 	 *
94 	 * Returns: 0 if the configuration can be applied, an negative
95 	 * error code otherwise
96 	 */
97 	int	(*validate)(struct phy *phy, enum phy_mode mode, int submode,
98 			    union phy_configure_opts *opts);
99 	int	(*reset)(struct phy *phy);
100 	int	(*calibrate)(struct phy *phy);
101 	struct module *owner;
102 };
103 
104 /**
105  * struct phy_attrs - represents phy attributes
106  * @bus_width: Data path width implemented by PHY
107  */
108 struct phy_attrs {
109 	u32			bus_width;
110 	enum phy_mode		mode;
111 };
112 
113 /**
114  * struct phy - represents the phy device
115  * @dev: phy device
116  * @id: id of the phy device
117  * @ops: function pointers for performing phy operations
118  * @init_data: list of PHY consumers (non-dt only)
119  * @mutex: mutex to protect phy_ops
120  * @init_count: used to protect when the PHY is used by multiple consumers
121  * @power_count: used to protect when the PHY is used by multiple consumers
122  * @attrs: used to specify PHY specific attributes
123  * @pwr: power regulator associated with the phy
124  */
125 struct phy {
126 	struct device		dev;
127 	int			id;
128 	const struct phy_ops	*ops;
129 	struct mutex		mutex;
130 	int			init_count;
131 	int			power_count;
132 	struct phy_attrs	attrs;
133 	struct regulator	*pwr;
134 };
135 
136 /**
137  * struct phy_provider - represents the phy provider
138  * @dev: phy provider device
139  * @children: can be used to override the default (dev->of_node) child node
140  * @owner: the module owner having of_xlate
141  * @list: to maintain a linked list of PHY providers
142  * @of_xlate: function pointer to obtain phy instance from phy pointer
143  */
144 struct phy_provider {
145 	struct device		*dev;
146 	struct device_node	*children;
147 	struct module		*owner;
148 	struct list_head	list;
149 	struct phy * (*of_xlate)(struct device *dev,
150 		struct of_phandle_args *args);
151 };
152 
153 /**
154  * struct phy_lookup - PHY association in list of phys managed by the phy driver
155  * @node: list node
156  * @dev_id: the device of the association
157  * @con_id: connection ID string on device
158  * @phy: the phy of the association
159  */
160 struct phy_lookup {
161 	struct list_head node;
162 	const char *dev_id;
163 	const char *con_id;
164 	struct phy *phy;
165 };
166 
167 #define	to_phy(a)	(container_of((a), struct phy, dev))
168 
169 #define	of_phy_provider_register(dev, xlate)	\
170 	__of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
171 
172 #define	devm_of_phy_provider_register(dev, xlate)	\
173 	__devm_of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
174 
175 #define of_phy_provider_register_full(dev, children, xlate) \
176 	__of_phy_provider_register(dev, children, THIS_MODULE, xlate)
177 
178 #define devm_of_phy_provider_register_full(dev, children, xlate) \
179 	__devm_of_phy_provider_register(dev, children, THIS_MODULE, xlate)
180 
181 static inline void phy_set_drvdata(struct phy *phy, void *data)
182 {
183 	dev_set_drvdata(&phy->dev, data);
184 }
185 
186 static inline void *phy_get_drvdata(struct phy *phy)
187 {
188 	return dev_get_drvdata(&phy->dev);
189 }
190 
191 #if IS_ENABLED(CONFIG_GENERIC_PHY)
192 int phy_pm_runtime_get(struct phy *phy);
193 int phy_pm_runtime_get_sync(struct phy *phy);
194 int phy_pm_runtime_put(struct phy *phy);
195 int phy_pm_runtime_put_sync(struct phy *phy);
196 void phy_pm_runtime_allow(struct phy *phy);
197 void phy_pm_runtime_forbid(struct phy *phy);
198 int phy_init(struct phy *phy);
199 int phy_exit(struct phy *phy);
200 int phy_power_on(struct phy *phy);
201 int phy_power_off(struct phy *phy);
202 int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode);
203 #define phy_set_mode(phy, mode) \
204 	phy_set_mode_ext(phy, mode, 0)
205 int phy_configure(struct phy *phy, union phy_configure_opts *opts);
206 int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
207 		 union phy_configure_opts *opts);
208 
209 static inline enum phy_mode phy_get_mode(struct phy *phy)
210 {
211 	return phy->attrs.mode;
212 }
213 int phy_reset(struct phy *phy);
214 int phy_calibrate(struct phy *phy);
215 static inline int phy_get_bus_width(struct phy *phy)
216 {
217 	return phy->attrs.bus_width;
218 }
219 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
220 {
221 	phy->attrs.bus_width = bus_width;
222 }
223 struct phy *phy_get(struct device *dev, const char *string);
224 struct phy *phy_optional_get(struct device *dev, const char *string);
225 struct phy *devm_phy_get(struct device *dev, const char *string);
226 struct phy *devm_phy_optional_get(struct device *dev, const char *string);
227 struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
228 			    const char *con_id);
229 struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
230 				     int index);
231 void phy_put(struct phy *phy);
232 void devm_phy_put(struct device *dev, struct phy *phy);
233 struct phy *of_phy_get(struct device_node *np, const char *con_id);
234 struct phy *of_phy_simple_xlate(struct device *dev,
235 	struct of_phandle_args *args);
236 struct phy *phy_create(struct device *dev, struct device_node *node,
237 		       const struct phy_ops *ops);
238 struct phy *devm_phy_create(struct device *dev, struct device_node *node,
239 			    const struct phy_ops *ops);
240 void phy_destroy(struct phy *phy);
241 void devm_phy_destroy(struct device *dev, struct phy *phy);
242 struct phy_provider *__of_phy_provider_register(struct device *dev,
243 	struct device_node *children, struct module *owner,
244 	struct phy * (*of_xlate)(struct device *dev,
245 				 struct of_phandle_args *args));
246 struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
247 	struct device_node *children, struct module *owner,
248 	struct phy * (*of_xlate)(struct device *dev,
249 				 struct of_phandle_args *args));
250 void of_phy_provider_unregister(struct phy_provider *phy_provider);
251 void devm_of_phy_provider_unregister(struct device *dev,
252 	struct phy_provider *phy_provider);
253 int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id);
254 void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id);
255 #else
256 static inline int phy_pm_runtime_get(struct phy *phy)
257 {
258 	if (!phy)
259 		return 0;
260 	return -ENOSYS;
261 }
262 
263 static inline int phy_pm_runtime_get_sync(struct phy *phy)
264 {
265 	if (!phy)
266 		return 0;
267 	return -ENOSYS;
268 }
269 
270 static inline int phy_pm_runtime_put(struct phy *phy)
271 {
272 	if (!phy)
273 		return 0;
274 	return -ENOSYS;
275 }
276 
277 static inline int phy_pm_runtime_put_sync(struct phy *phy)
278 {
279 	if (!phy)
280 		return 0;
281 	return -ENOSYS;
282 }
283 
284 static inline void phy_pm_runtime_allow(struct phy *phy)
285 {
286 	return;
287 }
288 
289 static inline void phy_pm_runtime_forbid(struct phy *phy)
290 {
291 	return;
292 }
293 
294 static inline int phy_init(struct phy *phy)
295 {
296 	if (!phy)
297 		return 0;
298 	return -ENOSYS;
299 }
300 
301 static inline int phy_exit(struct phy *phy)
302 {
303 	if (!phy)
304 		return 0;
305 	return -ENOSYS;
306 }
307 
308 static inline int phy_power_on(struct phy *phy)
309 {
310 	if (!phy)
311 		return 0;
312 	return -ENOSYS;
313 }
314 
315 static inline int phy_power_off(struct phy *phy)
316 {
317 	if (!phy)
318 		return 0;
319 	return -ENOSYS;
320 }
321 
322 static inline int phy_set_mode_ext(struct phy *phy, enum phy_mode mode,
323 				   int submode)
324 {
325 	if (!phy)
326 		return 0;
327 	return -ENOSYS;
328 }
329 
330 #define phy_set_mode(phy, mode) \
331 	phy_set_mode_ext(phy, mode, 0)
332 
333 static inline enum phy_mode phy_get_mode(struct phy *phy)
334 {
335 	return PHY_MODE_INVALID;
336 }
337 
338 static inline int phy_reset(struct phy *phy)
339 {
340 	if (!phy)
341 		return 0;
342 	return -ENOSYS;
343 }
344 
345 static inline int phy_calibrate(struct phy *phy)
346 {
347 	if (!phy)
348 		return 0;
349 	return -ENOSYS;
350 }
351 
352 static inline int phy_configure(struct phy *phy,
353 				union phy_configure_opts *opts)
354 {
355 	if (!phy)
356 		return 0;
357 
358 	return -ENOSYS;
359 }
360 
361 static inline int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
362 			       union phy_configure_opts *opts)
363 {
364 	if (!phy)
365 		return 0;
366 
367 	return -ENOSYS;
368 }
369 
370 static inline int phy_get_bus_width(struct phy *phy)
371 {
372 	return -ENOSYS;
373 }
374 
375 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
376 {
377 	return;
378 }
379 
380 static inline struct phy *phy_get(struct device *dev, const char *string)
381 {
382 	return ERR_PTR(-ENOSYS);
383 }
384 
385 static inline struct phy *phy_optional_get(struct device *dev,
386 					   const char *string)
387 {
388 	return ERR_PTR(-ENOSYS);
389 }
390 
391 static inline struct phy *devm_phy_get(struct device *dev, const char *string)
392 {
393 	return ERR_PTR(-ENOSYS);
394 }
395 
396 static inline struct phy *devm_phy_optional_get(struct device *dev,
397 						const char *string)
398 {
399 	return NULL;
400 }
401 
402 static inline struct phy *devm_of_phy_get(struct device *dev,
403 					  struct device_node *np,
404 					  const char *con_id)
405 {
406 	return ERR_PTR(-ENOSYS);
407 }
408 
409 static inline struct phy *devm_of_phy_get_by_index(struct device *dev,
410 						   struct device_node *np,
411 						   int index)
412 {
413 	return ERR_PTR(-ENOSYS);
414 }
415 
416 static inline void phy_put(struct phy *phy)
417 {
418 }
419 
420 static inline void devm_phy_put(struct device *dev, struct phy *phy)
421 {
422 }
423 
424 static inline struct phy *of_phy_get(struct device_node *np, const char *con_id)
425 {
426 	return ERR_PTR(-ENOSYS);
427 }
428 
429 static inline struct phy *of_phy_simple_xlate(struct device *dev,
430 	struct of_phandle_args *args)
431 {
432 	return ERR_PTR(-ENOSYS);
433 }
434 
435 static inline struct phy *phy_create(struct device *dev,
436 				     struct device_node *node,
437 				     const struct phy_ops *ops)
438 {
439 	return ERR_PTR(-ENOSYS);
440 }
441 
442 static inline struct phy *devm_phy_create(struct device *dev,
443 					  struct device_node *node,
444 					  const struct phy_ops *ops)
445 {
446 	return ERR_PTR(-ENOSYS);
447 }
448 
449 static inline void phy_destroy(struct phy *phy)
450 {
451 }
452 
453 static inline void devm_phy_destroy(struct device *dev, struct phy *phy)
454 {
455 }
456 
457 static inline struct phy_provider *__of_phy_provider_register(
458 	struct device *dev, struct device_node *children, struct module *owner,
459 	struct phy * (*of_xlate)(struct device *dev,
460 				 struct of_phandle_args *args))
461 {
462 	return ERR_PTR(-ENOSYS);
463 }
464 
465 static inline struct phy_provider *__devm_of_phy_provider_register(struct device
466 	*dev, struct device_node *children, struct module *owner,
467 	struct phy * (*of_xlate)(struct device *dev,
468 				 struct of_phandle_args *args))
469 {
470 	return ERR_PTR(-ENOSYS);
471 }
472 
473 static inline void of_phy_provider_unregister(struct phy_provider *phy_provider)
474 {
475 }
476 
477 static inline void devm_of_phy_provider_unregister(struct device *dev,
478 	struct phy_provider *phy_provider)
479 {
480 }
481 static inline int
482 phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
483 {
484 	return 0;
485 }
486 static inline void phy_remove_lookup(struct phy *phy, const char *con_id,
487 				     const char *dev_id) { }
488 #endif
489 
490 #endif /* __DRIVERS_PHY_H */
491