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