xref: /linux-6.15/include/linux/phy/phy.h (revision 86effd0d)
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_DEVICE,
29 	PHY_MODE_USB_OTG,
30 };
31 
32 /**
33  * struct phy_ops - set of function pointers for performing phy operations
34  * @init: operation to be performed for initializing phy
35  * @exit: operation to be performed while exiting
36  * @power_on: powering on the phy
37  * @power_off: powering off the phy
38  * @set_mode: set the mode of the phy
39  * @owner: the module owner containing the ops
40  */
41 struct phy_ops {
42 	int	(*init)(struct phy *phy);
43 	int	(*exit)(struct phy *phy);
44 	int	(*power_on)(struct phy *phy);
45 	int	(*power_off)(struct phy *phy);
46 	int	(*set_mode)(struct phy *phy, enum phy_mode mode);
47 	struct module *owner;
48 };
49 
50 /**
51  * struct phy_attrs - represents phy attributes
52  * @bus_width: Data path width implemented by PHY
53  */
54 struct phy_attrs {
55 	u32			bus_width;
56 };
57 
58 /**
59  * struct phy - represents the phy device
60  * @dev: phy device
61  * @id: id of the phy device
62  * @ops: function pointers for performing phy operations
63  * @init_data: list of PHY consumers (non-dt only)
64  * @mutex: mutex to protect phy_ops
65  * @init_count: used to protect when the PHY is used by multiple consumers
66  * @power_count: used to protect when the PHY is used by multiple consumers
67  * @phy_attrs: used to specify PHY specific attributes
68  */
69 struct phy {
70 	struct device		dev;
71 	int			id;
72 	const struct phy_ops	*ops;
73 	struct mutex		mutex;
74 	int			init_count;
75 	int			power_count;
76 	struct phy_attrs	attrs;
77 	struct regulator	*pwr;
78 };
79 
80 /**
81  * struct phy_provider - represents the phy provider
82  * @dev: phy provider device
83  * @owner: the module owner having of_xlate
84  * @of_xlate: function pointer to obtain phy instance from phy pointer
85  * @list: to maintain a linked list of PHY providers
86  */
87 struct phy_provider {
88 	struct device		*dev;
89 	struct device_node	*children;
90 	struct module		*owner;
91 	struct list_head	list;
92 	struct phy * (*of_xlate)(struct device *dev,
93 		struct of_phandle_args *args);
94 };
95 
96 struct phy_lookup {
97 	struct list_head node;
98 	const char *dev_id;
99 	const char *con_id;
100 	struct phy *phy;
101 };
102 
103 #define	to_phy(a)	(container_of((a), struct phy, dev))
104 
105 #define	of_phy_provider_register(dev, xlate)	\
106 	__of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
107 
108 #define	devm_of_phy_provider_register(dev, xlate)	\
109 	__devm_of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
110 
111 #define of_phy_provider_register_full(dev, children, xlate) \
112 	__of_phy_provider_register(dev, children, THIS_MODULE, xlate)
113 
114 #define devm_of_phy_provider_register_full(dev, children, xlate) \
115 	__devm_of_phy_provider_register(dev, children, THIS_MODULE, xlate)
116 
117 static inline void phy_set_drvdata(struct phy *phy, void *data)
118 {
119 	dev_set_drvdata(&phy->dev, data);
120 }
121 
122 static inline void *phy_get_drvdata(struct phy *phy)
123 {
124 	return dev_get_drvdata(&phy->dev);
125 }
126 
127 #if IS_ENABLED(CONFIG_GENERIC_PHY)
128 int phy_pm_runtime_get(struct phy *phy);
129 int phy_pm_runtime_get_sync(struct phy *phy);
130 int phy_pm_runtime_put(struct phy *phy);
131 int phy_pm_runtime_put_sync(struct phy *phy);
132 void phy_pm_runtime_allow(struct phy *phy);
133 void phy_pm_runtime_forbid(struct phy *phy);
134 int phy_init(struct phy *phy);
135 int phy_exit(struct phy *phy);
136 int phy_power_on(struct phy *phy);
137 int phy_power_off(struct phy *phy);
138 int phy_set_mode(struct phy *phy, enum phy_mode mode);
139 static inline int phy_get_bus_width(struct phy *phy)
140 {
141 	return phy->attrs.bus_width;
142 }
143 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
144 {
145 	phy->attrs.bus_width = bus_width;
146 }
147 struct phy *phy_get(struct device *dev, const char *string);
148 struct phy *phy_optional_get(struct device *dev, const char *string);
149 struct phy *devm_phy_get(struct device *dev, const char *string);
150 struct phy *devm_phy_optional_get(struct device *dev, const char *string);
151 struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
152 			    const char *con_id);
153 struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
154 				     int index);
155 void phy_put(struct phy *phy);
156 void devm_phy_put(struct device *dev, struct phy *phy);
157 struct phy *of_phy_get(struct device_node *np, const char *con_id);
158 struct phy *of_phy_simple_xlate(struct device *dev,
159 	struct of_phandle_args *args);
160 struct phy *phy_create(struct device *dev, struct device_node *node,
161 		       const struct phy_ops *ops);
162 struct phy *devm_phy_create(struct device *dev, struct device_node *node,
163 			    const struct phy_ops *ops);
164 void phy_destroy(struct phy *phy);
165 void devm_phy_destroy(struct device *dev, struct phy *phy);
166 struct phy_provider *__of_phy_provider_register(struct device *dev,
167 	struct device_node *children, struct module *owner,
168 	struct phy * (*of_xlate)(struct device *dev,
169 				 struct of_phandle_args *args));
170 struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
171 	struct device_node *children, struct module *owner,
172 	struct phy * (*of_xlate)(struct device *dev,
173 				 struct of_phandle_args *args));
174 void of_phy_provider_unregister(struct phy_provider *phy_provider);
175 void devm_of_phy_provider_unregister(struct device *dev,
176 	struct phy_provider *phy_provider);
177 int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id);
178 void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id);
179 #else
180 static inline int phy_pm_runtime_get(struct phy *phy)
181 {
182 	if (!phy)
183 		return 0;
184 	return -ENOSYS;
185 }
186 
187 static inline int phy_pm_runtime_get_sync(struct phy *phy)
188 {
189 	if (!phy)
190 		return 0;
191 	return -ENOSYS;
192 }
193 
194 static inline int phy_pm_runtime_put(struct phy *phy)
195 {
196 	if (!phy)
197 		return 0;
198 	return -ENOSYS;
199 }
200 
201 static inline int phy_pm_runtime_put_sync(struct phy *phy)
202 {
203 	if (!phy)
204 		return 0;
205 	return -ENOSYS;
206 }
207 
208 static inline void phy_pm_runtime_allow(struct phy *phy)
209 {
210 	return;
211 }
212 
213 static inline void phy_pm_runtime_forbid(struct phy *phy)
214 {
215 	return;
216 }
217 
218 static inline int phy_init(struct phy *phy)
219 {
220 	if (!phy)
221 		return 0;
222 	return -ENOSYS;
223 }
224 
225 static inline int phy_exit(struct phy *phy)
226 {
227 	if (!phy)
228 		return 0;
229 	return -ENOSYS;
230 }
231 
232 static inline int phy_power_on(struct phy *phy)
233 {
234 	if (!phy)
235 		return 0;
236 	return -ENOSYS;
237 }
238 
239 static inline int phy_power_off(struct phy *phy)
240 {
241 	if (!phy)
242 		return 0;
243 	return -ENOSYS;
244 }
245 
246 static inline int phy_set_mode(struct phy *phy, enum phy_mode mode)
247 {
248 	if (!phy)
249 		return 0;
250 	return -ENOSYS;
251 }
252 
253 static inline int phy_get_bus_width(struct phy *phy)
254 {
255 	return -ENOSYS;
256 }
257 
258 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
259 {
260 	return;
261 }
262 
263 static inline struct phy *phy_get(struct device *dev, const char *string)
264 {
265 	return ERR_PTR(-ENOSYS);
266 }
267 
268 static inline struct phy *phy_optional_get(struct device *dev,
269 					   const char *string)
270 {
271 	return ERR_PTR(-ENOSYS);
272 }
273 
274 static inline struct phy *devm_phy_get(struct device *dev, const char *string)
275 {
276 	return ERR_PTR(-ENOSYS);
277 }
278 
279 static inline struct phy *devm_phy_optional_get(struct device *dev,
280 						const char *string)
281 {
282 	return ERR_PTR(-ENOSYS);
283 }
284 
285 static inline struct phy *devm_of_phy_get(struct device *dev,
286 					  struct device_node *np,
287 					  const char *con_id)
288 {
289 	return ERR_PTR(-ENOSYS);
290 }
291 
292 static inline struct phy *devm_of_phy_get_by_index(struct device *dev,
293 						   struct device_node *np,
294 						   int index)
295 {
296 	return ERR_PTR(-ENOSYS);
297 }
298 
299 static inline void phy_put(struct phy *phy)
300 {
301 }
302 
303 static inline void devm_phy_put(struct device *dev, struct phy *phy)
304 {
305 }
306 
307 static inline struct phy *of_phy_get(struct device_node *np, const char *con_id)
308 {
309 	return ERR_PTR(-ENOSYS);
310 }
311 
312 static inline struct phy *of_phy_simple_xlate(struct device *dev,
313 	struct of_phandle_args *args)
314 {
315 	return ERR_PTR(-ENOSYS);
316 }
317 
318 static inline struct phy *phy_create(struct device *dev,
319 				     struct device_node *node,
320 				     const struct phy_ops *ops)
321 {
322 	return ERR_PTR(-ENOSYS);
323 }
324 
325 static inline struct phy *devm_phy_create(struct device *dev,
326 					  struct device_node *node,
327 					  const struct phy_ops *ops)
328 {
329 	return ERR_PTR(-ENOSYS);
330 }
331 
332 static inline void phy_destroy(struct phy *phy)
333 {
334 }
335 
336 static inline void devm_phy_destroy(struct device *dev, struct phy *phy)
337 {
338 }
339 
340 static inline struct phy_provider *__of_phy_provider_register(
341 	struct device *dev, struct device_node *children, struct module *owner,
342 	struct phy * (*of_xlate)(struct device *dev,
343 				 struct of_phandle_args *args))
344 {
345 	return ERR_PTR(-ENOSYS);
346 }
347 
348 static inline struct phy_provider *__devm_of_phy_provider_register(struct device
349 	*dev, struct device_node *children, struct module *owner,
350 	struct phy * (*of_xlate)(struct device *dev,
351 				 struct of_phandle_args *args))
352 {
353 	return ERR_PTR(-ENOSYS);
354 }
355 
356 static inline void of_phy_provider_unregister(struct phy_provider *phy_provider)
357 {
358 }
359 
360 static inline void devm_of_phy_provider_unregister(struct device *dev,
361 	struct phy_provider *phy_provider)
362 {
363 }
364 static inline int
365 phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
366 {
367 	return 0;
368 }
369 static inline void phy_remove_lookup(struct phy *phy, const char *con_id,
370 				     const char *dev_id) { }
371 #endif
372 
373 #endif /* __DRIVERS_PHY_H */
374