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