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