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