xref: /linux/include/linux/phy/phy.h (revision 9b725d5959a1d9bb670ccdb23add45f0eaafefb5)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * phy.h -- generic phy header file
4  *
5  * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
6  *
7  * Author: Kishon Vijay Abraham I <kishon@ti.com>
8  */
9 
10 #ifndef __DRIVERS_PHY_H
11 #define __DRIVERS_PHY_H
12 
13 #include <linux/err.h>
14 #include <linux/of.h>
15 #include <linux/device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regulator/consumer.h>
18 
19 #include <linux/phy/phy-dp.h>
20 #include <linux/phy/phy-hdmi.h>
21 #include <linux/phy/phy-lvds.h>
22 #include <linux/phy/phy-mipi-dphy.h>
23 
24 struct phy;
25 
26 enum phy_mode {
27 	PHY_MODE_INVALID,
28 	PHY_MODE_USB_HOST,
29 	PHY_MODE_USB_HOST_LS,
30 	PHY_MODE_USB_HOST_FS,
31 	PHY_MODE_USB_HOST_HS,
32 	PHY_MODE_USB_HOST_SS,
33 	PHY_MODE_USB_DEVICE,
34 	PHY_MODE_USB_DEVICE_LS,
35 	PHY_MODE_USB_DEVICE_FS,
36 	PHY_MODE_USB_DEVICE_HS,
37 	PHY_MODE_USB_DEVICE_SS,
38 	PHY_MODE_USB_OTG,
39 	PHY_MODE_UFS_HS_A,
40 	PHY_MODE_UFS_HS_B,
41 	PHY_MODE_PCIE,
42 	PHY_MODE_ETHERNET,
43 	PHY_MODE_MIPI_DPHY,
44 	PHY_MODE_SATA,
45 	PHY_MODE_LVDS,
46 	PHY_MODE_DP,
47 	PHY_MODE_HDMI,
48 };
49 
50 enum phy_media {
51 	PHY_MEDIA_DEFAULT,
52 	PHY_MEDIA_SR,
53 	PHY_MEDIA_DAC,
54 };
55 
56 enum phy_ufs_state {
57 	PHY_UFS_HIBERN8_ENTER,
58 	PHY_UFS_HIBERN8_EXIT,
59 };
60 
61 union phy_notify {
62 	enum phy_ufs_state ufs_state;
63 };
64 
65 /**
66  * union phy_configure_opts - Opaque generic phy configuration
67  *
68  * @mipi_dphy:	Configuration set applicable for phys supporting
69  *		the MIPI_DPHY phy mode.
70  * @dp:		Configuration set applicable for phys supporting
71  *		the DisplayPort protocol.
72  * @lvds:	Configuration set applicable for phys supporting
73  *		the LVDS phy mode.
74  * @hdmi:	Configuration set applicable for phys supporting
75  *		the HDMI phy mode.
76  */
77 union phy_configure_opts {
78 	struct phy_configure_opts_mipi_dphy	mipi_dphy;
79 	struct phy_configure_opts_dp		dp;
80 	struct phy_configure_opts_lvds		lvds;
81 	struct phy_configure_opts_hdmi		hdmi;
82 };
83 
84 /**
85  * struct phy_ops - set of function pointers for performing phy operations
86  * @init: operation to be performed for initializing phy
87  * @exit: operation to be performed while exiting
88  * @power_on: powering on the phy
89  * @power_off: powering off the phy
90  * @set_mode: set the mode of the phy
91  * @set_media: set the media type of the phy (optional)
92  * @set_speed: set the speed of the phy (optional)
93  * @reset: resetting the phy
94  * @calibrate: calibrate the phy
95  * @notify_phystate: notify and configure the phy for a particular state
96  * @release: ops to be performed while the consumer relinquishes the PHY
97  * @owner: the module owner containing the ops
98  */
99 struct phy_ops {
100 	int	(*init)(struct phy *phy);
101 	int	(*exit)(struct phy *phy);
102 	int	(*power_on)(struct phy *phy);
103 	int	(*power_off)(struct phy *phy);
104 	int	(*set_mode)(struct phy *phy, enum phy_mode mode, int submode);
105 	int	(*set_media)(struct phy *phy, enum phy_media media);
106 	int	(*set_speed)(struct phy *phy, int speed);
107 
108 	/**
109 	 * @configure:
110 	 *
111 	 * Optional.
112 	 *
113 	 * Used to change the PHY parameters. phy_init() must have
114 	 * been called on the phy.
115 	 *
116 	 * Returns: 0 if successful, an negative error code otherwise
117 	 */
118 	int	(*configure)(struct phy *phy, union phy_configure_opts *opts);
119 
120 	/**
121 	 * @validate:
122 	 *
123 	 * Optional.
124 	 *
125 	 * Used to check that the current set of parameters can be
126 	 * handled by the phy. Implementations are free to tune the
127 	 * parameters passed as arguments if needed by some
128 	 * implementation detail or constraints. It must not change
129 	 * any actual configuration of the PHY, so calling it as many
130 	 * times as deemed fit by the consumer must have no side
131 	 * effect.
132 	 *
133 	 * Returns: 0 if the configuration can be applied, an negative
134 	 * error code otherwise
135 	 */
136 	int	(*validate)(struct phy *phy, enum phy_mode mode, int submode,
137 			    union phy_configure_opts *opts);
138 	int	(*reset)(struct phy *phy);
139 	int	(*calibrate)(struct phy *phy);
140 
141 	/* notify phy connect status change */
142 	int	(*connect)(struct phy *phy, int port);
143 	int	(*disconnect)(struct phy *phy, int port);
144 
145 	int	(*notify_phystate)(struct phy *phy, union phy_notify state);
146 	void	(*release)(struct phy *phy);
147 	struct module *owner;
148 };
149 
150 /**
151  * struct phy_attrs - represents phy attributes
152  * @bus_width: Data path width implemented by PHY
153  * @max_link_rate: Maximum link rate supported by PHY (units to be decided by producer and consumer)
154  * @mode: PHY mode
155  */
156 struct phy_attrs {
157 	u32			bus_width;
158 	u32			max_link_rate;
159 	enum phy_mode		mode;
160 };
161 
162 /**
163  * struct phy - represents the phy device
164  * @dev: phy device
165  * @id: id of the phy device
166  * @ops: function pointers for performing phy operations
167  * @mutex: mutex to protect phy_ops
168  * @lockdep_key: lockdep information for this mutex
169  * @init_count: used to protect when the PHY is used by multiple consumers
170  * @power_count: used to protect when the PHY is used by multiple consumers
171  * @attrs: used to specify PHY specific attributes
172  * @pwr: power regulator associated with the phy
173  * @debugfs: debugfs directory
174  */
175 struct phy {
176 	struct device		dev;
177 	int			id;
178 	const struct phy_ops	*ops;
179 	struct mutex		mutex;
180 	struct lock_class_key	lockdep_key;
181 	int			init_count;
182 	int			power_count;
183 	struct phy_attrs	attrs;
184 	struct regulator	*pwr;
185 	struct dentry		*debugfs;
186 };
187 
188 /**
189  * struct phy_provider - represents the phy provider
190  * @dev: phy provider device
191  * @children: can be used to override the default (dev->of_node) child node
192  * @owner: the module owner having of_xlate
193  * @list: to maintain a linked list of PHY providers
194  * @of_xlate: function pointer to obtain phy instance from phy pointer
195  */
196 struct phy_provider {
197 	struct device		*dev;
198 	struct device_node	*children;
199 	struct module		*owner;
200 	struct list_head	list;
201 	struct phy * (*of_xlate)(struct device *dev,
202 				 const struct of_phandle_args *args);
203 };
204 
205 /**
206  * struct phy_lookup - PHY association in list of phys managed by the phy driver
207  * @node: list node
208  * @dev_id: the device of the association
209  * @con_id: connection ID string on device
210  * @phy: the phy of the association
211  */
212 struct phy_lookup {
213 	struct list_head node;
214 	const char *dev_id;
215 	const char *con_id;
216 	struct phy *phy;
217 };
218 
219 #define	to_phy(a)	(container_of((a), struct phy, dev))
220 
221 #define	of_phy_provider_register(dev, xlate)	\
222 	__of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
223 
224 #define	devm_of_phy_provider_register(dev, xlate)	\
225 	__devm_of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
226 
227 #define of_phy_provider_register_full(dev, children, xlate) \
228 	__of_phy_provider_register(dev, children, THIS_MODULE, xlate)
229 
230 #define devm_of_phy_provider_register_full(dev, children, xlate) \
231 	__devm_of_phy_provider_register(dev, children, THIS_MODULE, xlate)
232 
phy_set_drvdata(struct phy * phy,void * data)233 static inline void phy_set_drvdata(struct phy *phy, void *data)
234 {
235 	dev_set_drvdata(&phy->dev, data);
236 }
237 
phy_get_drvdata(struct phy * phy)238 static inline void *phy_get_drvdata(struct phy *phy)
239 {
240 	return dev_get_drvdata(&phy->dev);
241 }
242 
243 #if IS_ENABLED(CONFIG_GENERIC_PHY)
244 int phy_pm_runtime_get(struct phy *phy);
245 int phy_pm_runtime_get_sync(struct phy *phy);
246 void phy_pm_runtime_put(struct phy *phy);
247 int phy_pm_runtime_put_sync(struct phy *phy);
248 int phy_init(struct phy *phy);
249 int phy_exit(struct phy *phy);
250 int phy_power_on(struct phy *phy);
251 int phy_power_off(struct phy *phy);
252 int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode);
253 #define phy_set_mode(phy, mode) \
254 	phy_set_mode_ext(phy, mode, 0)
255 int phy_set_media(struct phy *phy, enum phy_media media);
256 int phy_set_speed(struct phy *phy, int speed);
257 int phy_configure(struct phy *phy, union phy_configure_opts *opts);
258 int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
259 		 union phy_configure_opts *opts);
260 
phy_get_mode(struct phy * phy)261 static inline enum phy_mode phy_get_mode(struct phy *phy)
262 {
263 	return phy->attrs.mode;
264 }
265 int phy_reset(struct phy *phy);
266 int phy_calibrate(struct phy *phy);
267 int phy_notify_connect(struct phy *phy, int port);
268 int phy_notify_disconnect(struct phy *phy, int port);
269 int phy_notify_state(struct phy *phy, union phy_notify state);
phy_get_bus_width(struct phy * phy)270 static inline int phy_get_bus_width(struct phy *phy)
271 {
272 	return phy->attrs.bus_width;
273 }
phy_set_bus_width(struct phy * phy,int bus_width)274 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
275 {
276 	phy->attrs.bus_width = bus_width;
277 }
278 struct phy *phy_get(struct device *dev, const char *string);
279 struct phy *devm_phy_get(struct device *dev, const char *string);
280 struct phy *devm_phy_optional_get(struct device *dev, const char *string);
281 struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
282 			    const char *con_id);
283 struct phy *devm_of_phy_optional_get(struct device *dev, struct device_node *np,
284 				     const char *con_id);
285 struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
286 				     int index);
287 void of_phy_put(struct phy *phy);
288 void phy_put(struct device *dev, struct phy *phy);
289 void devm_phy_put(struct device *dev, struct phy *phy);
290 struct phy *of_phy_get(struct device_node *np, const char *con_id);
291 struct phy *of_phy_simple_xlate(struct device *dev,
292 				const struct of_phandle_args *args);
293 struct phy *phy_create(struct device *dev, struct device_node *node,
294 		       const struct phy_ops *ops);
295 struct phy *devm_phy_create(struct device *dev, struct device_node *node,
296 			    const struct phy_ops *ops);
297 void phy_destroy(struct phy *phy);
298 void devm_phy_destroy(struct device *dev, struct phy *phy);
299 struct phy_provider *__of_phy_provider_register(struct device *dev,
300 	struct device_node *children, struct module *owner,
301 	struct phy * (*of_xlate)(struct device *dev,
302 				 const struct of_phandle_args *args));
303 struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
304 	struct device_node *children, struct module *owner,
305 	struct phy * (*of_xlate)(struct device *dev,
306 				 const struct of_phandle_args *args));
307 void of_phy_provider_unregister(struct phy_provider *phy_provider);
308 void devm_of_phy_provider_unregister(struct device *dev,
309 	struct phy_provider *phy_provider);
310 int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id);
311 void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id);
312 #else
phy_pm_runtime_get(struct phy * phy)313 static inline int phy_pm_runtime_get(struct phy *phy)
314 {
315 	if (!phy)
316 		return 0;
317 	return -ENOSYS;
318 }
319 
phy_pm_runtime_get_sync(struct phy * phy)320 static inline int phy_pm_runtime_get_sync(struct phy *phy)
321 {
322 	if (!phy)
323 		return 0;
324 	return -ENOSYS;
325 }
326 
phy_pm_runtime_put(struct phy * phy)327 static inline void phy_pm_runtime_put(struct phy *phy)
328 {
329 }
330 
phy_pm_runtime_put_sync(struct phy * phy)331 static inline int phy_pm_runtime_put_sync(struct phy *phy)
332 {
333 	if (!phy)
334 		return 0;
335 	return -ENOSYS;
336 }
337 
phy_init(struct phy * phy)338 static inline int phy_init(struct phy *phy)
339 {
340 	if (!phy)
341 		return 0;
342 	return -ENOSYS;
343 }
344 
phy_exit(struct phy * phy)345 static inline int phy_exit(struct phy *phy)
346 {
347 	if (!phy)
348 		return 0;
349 	return -ENOSYS;
350 }
351 
phy_power_on(struct phy * phy)352 static inline int phy_power_on(struct phy *phy)
353 {
354 	if (!phy)
355 		return 0;
356 	return -ENOSYS;
357 }
358 
phy_power_off(struct phy * phy)359 static inline int phy_power_off(struct phy *phy)
360 {
361 	if (!phy)
362 		return 0;
363 	return -ENOSYS;
364 }
365 
phy_set_mode_ext(struct phy * phy,enum phy_mode mode,int submode)366 static inline int phy_set_mode_ext(struct phy *phy, enum phy_mode mode,
367 				   int submode)
368 {
369 	if (!phy)
370 		return 0;
371 	return -ENOSYS;
372 }
373 
374 #define phy_set_mode(phy, mode) \
375 	phy_set_mode_ext(phy, mode, 0)
376 
phy_set_media(struct phy * phy,enum phy_media media)377 static inline int phy_set_media(struct phy *phy, enum phy_media media)
378 {
379 	if (!phy)
380 		return 0;
381 	return -ENODEV;
382 }
383 
phy_set_speed(struct phy * phy,int speed)384 static inline int phy_set_speed(struct phy *phy, int speed)
385 {
386 	if (!phy)
387 		return 0;
388 	return -ENODEV;
389 }
390 
phy_get_mode(struct phy * phy)391 static inline enum phy_mode phy_get_mode(struct phy *phy)
392 {
393 	return PHY_MODE_INVALID;
394 }
395 
phy_reset(struct phy * phy)396 static inline int phy_reset(struct phy *phy)
397 {
398 	if (!phy)
399 		return 0;
400 	return -ENOSYS;
401 }
402 
phy_calibrate(struct phy * phy)403 static inline int phy_calibrate(struct phy *phy)
404 {
405 	if (!phy)
406 		return 0;
407 	return -ENOSYS;
408 }
409 
phy_notify_connect(struct phy * phy,int index)410 static inline int phy_notify_connect(struct phy *phy, int index)
411 {
412 	if (!phy)
413 		return 0;
414 	return -ENOSYS;
415 }
416 
phy_notify_disconnect(struct phy * phy,int index)417 static inline int phy_notify_disconnect(struct phy *phy, int index)
418 {
419 	if (!phy)
420 		return 0;
421 	return -ENOSYS;
422 }
423 
phy_notify_state(struct phy * phy,union phy_notify state)424 static inline int phy_notify_state(struct phy *phy, union phy_notify state)
425 {
426 	if (!phy)
427 		return 0;
428 	return -ENOSYS;
429 }
430 
phy_configure(struct phy * phy,union phy_configure_opts * opts)431 static inline int phy_configure(struct phy *phy,
432 				union phy_configure_opts *opts)
433 {
434 	if (!phy)
435 		return 0;
436 
437 	return -ENOSYS;
438 }
439 
phy_validate(struct phy * phy,enum phy_mode mode,int submode,union phy_configure_opts * opts)440 static inline int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
441 			       union phy_configure_opts *opts)
442 {
443 	if (!phy)
444 		return 0;
445 
446 	return -ENOSYS;
447 }
448 
phy_get_bus_width(struct phy * phy)449 static inline int phy_get_bus_width(struct phy *phy)
450 {
451 	return -ENOSYS;
452 }
453 
phy_set_bus_width(struct phy * phy,int bus_width)454 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
455 {
456 	return;
457 }
458 
phy_get(struct device * dev,const char * string)459 static inline struct phy *phy_get(struct device *dev, const char *string)
460 {
461 	return ERR_PTR(-ENOSYS);
462 }
463 
devm_phy_get(struct device * dev,const char * string)464 static inline struct phy *devm_phy_get(struct device *dev, const char *string)
465 {
466 	return ERR_PTR(-ENOSYS);
467 }
468 
devm_phy_optional_get(struct device * dev,const char * string)469 static inline struct phy *devm_phy_optional_get(struct device *dev,
470 						const char *string)
471 {
472 	return NULL;
473 }
474 
devm_of_phy_get(struct device * dev,struct device_node * np,const char * con_id)475 static inline struct phy *devm_of_phy_get(struct device *dev,
476 					  struct device_node *np,
477 					  const char *con_id)
478 {
479 	return ERR_PTR(-ENOSYS);
480 }
481 
devm_of_phy_optional_get(struct device * dev,struct device_node * np,const char * con_id)482 static inline struct phy *devm_of_phy_optional_get(struct device *dev,
483 						   struct device_node *np,
484 						   const char *con_id)
485 {
486 	return NULL;
487 }
488 
devm_of_phy_get_by_index(struct device * dev,struct device_node * np,int index)489 static inline struct phy *devm_of_phy_get_by_index(struct device *dev,
490 						   struct device_node *np,
491 						   int index)
492 {
493 	return ERR_PTR(-ENOSYS);
494 }
495 
of_phy_put(struct phy * phy)496 static inline void of_phy_put(struct phy *phy)
497 {
498 }
499 
phy_put(struct device * dev,struct phy * phy)500 static inline void phy_put(struct device *dev, struct phy *phy)
501 {
502 }
503 
devm_phy_put(struct device * dev,struct phy * phy)504 static inline void devm_phy_put(struct device *dev, struct phy *phy)
505 {
506 }
507 
of_phy_get(struct device_node * np,const char * con_id)508 static inline struct phy *of_phy_get(struct device_node *np, const char *con_id)
509 {
510 	return ERR_PTR(-ENOSYS);
511 }
512 
of_phy_simple_xlate(struct device * dev,const struct of_phandle_args * args)513 static inline struct phy *of_phy_simple_xlate(struct device *dev,
514 					      const struct of_phandle_args *args)
515 {
516 	return ERR_PTR(-ENOSYS);
517 }
518 
phy_create(struct device * dev,struct device_node * node,const struct phy_ops * ops)519 static inline struct phy *phy_create(struct device *dev,
520 				     struct device_node *node,
521 				     const struct phy_ops *ops)
522 {
523 	return ERR_PTR(-ENOSYS);
524 }
525 
devm_phy_create(struct device * dev,struct device_node * node,const struct phy_ops * ops)526 static inline struct phy *devm_phy_create(struct device *dev,
527 					  struct device_node *node,
528 					  const struct phy_ops *ops)
529 {
530 	return ERR_PTR(-ENOSYS);
531 }
532 
phy_destroy(struct phy * phy)533 static inline void phy_destroy(struct phy *phy)
534 {
535 }
536 
devm_phy_destroy(struct device * dev,struct phy * phy)537 static inline void devm_phy_destroy(struct device *dev, struct phy *phy)
538 {
539 }
540 
__of_phy_provider_register(struct device * dev,struct device_node * children,struct module * owner,struct phy * (* of_xlate)(struct device * dev,const struct of_phandle_args * args))541 static inline struct phy_provider *__of_phy_provider_register(
542 	struct device *dev, struct device_node *children, struct module *owner,
543 	struct phy * (*of_xlate)(struct device *dev,
544 				 const struct of_phandle_args *args))
545 {
546 	return ERR_PTR(-ENOSYS);
547 }
548 
__devm_of_phy_provider_register(struct device * dev,struct device_node * children,struct module * owner,struct phy * (* of_xlate)(struct device * dev,const struct of_phandle_args * args))549 static inline struct phy_provider *__devm_of_phy_provider_register(struct device
550 	*dev, struct device_node *children, struct module *owner,
551 	struct phy * (*of_xlate)(struct device *dev,
552 				 const struct of_phandle_args *args))
553 {
554 	return ERR_PTR(-ENOSYS);
555 }
556 
of_phy_provider_unregister(struct phy_provider * phy_provider)557 static inline void of_phy_provider_unregister(struct phy_provider *phy_provider)
558 {
559 }
560 
devm_of_phy_provider_unregister(struct device * dev,struct phy_provider * phy_provider)561 static inline void devm_of_phy_provider_unregister(struct device *dev,
562 	struct phy_provider *phy_provider)
563 {
564 }
565 static inline int
phy_create_lookup(struct phy * phy,const char * con_id,const char * dev_id)566 phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
567 {
568 	return 0;
569 }
phy_remove_lookup(struct phy * phy,const char * con_id,const char * dev_id)570 static inline void phy_remove_lookup(struct phy *phy, const char *con_id,
571 				     const char *dev_id) { }
572 #endif
573 
574 #endif /* __DRIVERS_PHY_H */
575