4248adeeffcabef3b5df3998eff7deef21f6b1ff
[firefly-linux-kernel-4.4.55.git] / include / linux / phy / phy.h
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 };
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 module           *owner;
90         struct list_head        list;
91         struct phy * (*of_xlate)(struct device *dev,
92                 struct of_phandle_args *args);
93 };
94
95 struct phy_lookup {
96         struct list_head node;
97         const char *dev_id;
98         const char *con_id;
99         struct phy *phy;
100 };
101
102 #define to_phy(a)       (container_of((a), struct phy, dev))
103
104 #define of_phy_provider_register(dev, xlate)    \
105         __of_phy_provider_register((dev), THIS_MODULE, (xlate))
106
107 #define devm_of_phy_provider_register(dev, xlate)       \
108         __devm_of_phy_provider_register((dev), THIS_MODULE, (xlate))
109
110 static inline void phy_set_drvdata(struct phy *phy, void *data)
111 {
112         dev_set_drvdata(&phy->dev, data);
113 }
114
115 static inline void *phy_get_drvdata(struct phy *phy)
116 {
117         return dev_get_drvdata(&phy->dev);
118 }
119
120 #if IS_ENABLED(CONFIG_GENERIC_PHY)
121 int phy_pm_runtime_get(struct phy *phy);
122 int phy_pm_runtime_get_sync(struct phy *phy);
123 int phy_pm_runtime_put(struct phy *phy);
124 int phy_pm_runtime_put_sync(struct phy *phy);
125 void phy_pm_runtime_allow(struct phy *phy);
126 void phy_pm_runtime_forbid(struct phy *phy);
127 int phy_init(struct phy *phy);
128 int phy_exit(struct phy *phy);
129 int phy_power_on(struct phy *phy);
130 int phy_power_off(struct phy *phy);
131 int phy_set_mode(struct phy *phy, enum phy_mode mode);
132 static inline int phy_get_bus_width(struct phy *phy)
133 {
134         return phy->attrs.bus_width;
135 }
136 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
137 {
138         phy->attrs.bus_width = bus_width;
139 }
140 struct phy *phy_get(struct device *dev, const char *string);
141 struct phy *phy_optional_get(struct device *dev, const char *string);
142 struct phy *devm_phy_get(struct device *dev, const char *string);
143 struct phy *devm_phy_optional_get(struct device *dev, const char *string);
144 struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
145                             const char *con_id);
146 struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
147                                      int index);
148 void phy_put(struct phy *phy);
149 void devm_phy_put(struct device *dev, struct phy *phy);
150 struct phy *of_phy_get(struct device_node *np, const char *con_id);
151 struct phy *of_phy_simple_xlate(struct device *dev,
152         struct of_phandle_args *args);
153 struct phy *phy_create(struct device *dev, struct device_node *node,
154                        const struct phy_ops *ops);
155 struct phy *devm_phy_create(struct device *dev, struct device_node *node,
156                             const struct phy_ops *ops);
157 void phy_destroy(struct phy *phy);
158 void devm_phy_destroy(struct device *dev, struct phy *phy);
159 struct phy_provider *__of_phy_provider_register(struct device *dev,
160         struct module *owner, struct phy * (*of_xlate)(struct device *dev,
161         struct of_phandle_args *args));
162 struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
163         struct module *owner, struct phy * (*of_xlate)(struct device *dev,
164         struct of_phandle_args *args));
165 void of_phy_provider_unregister(struct phy_provider *phy_provider);
166 void devm_of_phy_provider_unregister(struct device *dev,
167         struct phy_provider *phy_provider);
168 int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id);
169 void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id);
170 #else
171 static inline int phy_pm_runtime_get(struct phy *phy)
172 {
173         if (!phy)
174                 return 0;
175         return -ENOSYS;
176 }
177
178 static inline int phy_pm_runtime_get_sync(struct phy *phy)
179 {
180         if (!phy)
181                 return 0;
182         return -ENOSYS;
183 }
184
185 static inline int phy_pm_runtime_put(struct phy *phy)
186 {
187         if (!phy)
188                 return 0;
189         return -ENOSYS;
190 }
191
192 static inline int phy_pm_runtime_put_sync(struct phy *phy)
193 {
194         if (!phy)
195                 return 0;
196         return -ENOSYS;
197 }
198
199 static inline void phy_pm_runtime_allow(struct phy *phy)
200 {
201         return;
202 }
203
204 static inline void phy_pm_runtime_forbid(struct phy *phy)
205 {
206         return;
207 }
208
209 static inline int phy_init(struct phy *phy)
210 {
211         if (!phy)
212                 return 0;
213         return -ENOSYS;
214 }
215
216 static inline int phy_exit(struct phy *phy)
217 {
218         if (!phy)
219                 return 0;
220         return -ENOSYS;
221 }
222
223 static inline int phy_power_on(struct phy *phy)
224 {
225         if (!phy)
226                 return 0;
227         return -ENOSYS;
228 }
229
230 static inline int phy_power_off(struct phy *phy)
231 {
232         if (!phy)
233                 return 0;
234         return -ENOSYS;
235 }
236
237 static inline int phy_set_mode(struct phy *phy, enum phy_mode mode)
238 {
239         if (!phy)
240                 return 0;
241         return -ENOSYS;
242 }
243
244 static inline int phy_get_bus_width(struct phy *phy)
245 {
246         return -ENOSYS;
247 }
248
249 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
250 {
251         return;
252 }
253
254 static inline struct phy *phy_get(struct device *dev, const char *string)
255 {
256         return ERR_PTR(-ENOSYS);
257 }
258
259 static inline struct phy *phy_optional_get(struct device *dev,
260                                            const char *string)
261 {
262         return ERR_PTR(-ENOSYS);
263 }
264
265 static inline struct phy *devm_phy_get(struct device *dev, const char *string)
266 {
267         return ERR_PTR(-ENOSYS);
268 }
269
270 static inline struct phy *devm_phy_optional_get(struct device *dev,
271                                                 const char *string)
272 {
273         return ERR_PTR(-ENOSYS);
274 }
275
276 static inline struct phy *devm_of_phy_get(struct device *dev,
277                                           struct device_node *np,
278                                           const char *con_id)
279 {
280         return ERR_PTR(-ENOSYS);
281 }
282
283 static inline struct phy *devm_of_phy_get_by_index(struct device *dev,
284                                                    struct device_node *np,
285                                                    int index)
286 {
287         return ERR_PTR(-ENOSYS);
288 }
289
290 static inline void phy_put(struct phy *phy)
291 {
292 }
293
294 static inline void devm_phy_put(struct device *dev, struct phy *phy)
295 {
296 }
297
298 static inline struct phy *of_phy_get(struct device_node *np, const char *con_id)
299 {
300         return ERR_PTR(-ENOSYS);
301 }
302
303 static inline struct phy *of_phy_simple_xlate(struct device *dev,
304         struct of_phandle_args *args)
305 {
306         return ERR_PTR(-ENOSYS);
307 }
308
309 static inline struct phy *phy_create(struct device *dev,
310                                      struct device_node *node,
311                                      const struct phy_ops *ops)
312 {
313         return ERR_PTR(-ENOSYS);
314 }
315
316 static inline struct phy *devm_phy_create(struct device *dev,
317                                           struct device_node *node,
318                                           const struct phy_ops *ops)
319 {
320         return ERR_PTR(-ENOSYS);
321 }
322
323 static inline void phy_destroy(struct phy *phy)
324 {
325 }
326
327 static inline void devm_phy_destroy(struct device *dev, struct phy *phy)
328 {
329 }
330
331 static inline struct phy_provider *__of_phy_provider_register(
332         struct device *dev, struct module *owner, struct phy * (*of_xlate)(
333         struct device *dev, struct of_phandle_args *args))
334 {
335         return ERR_PTR(-ENOSYS);
336 }
337
338 static inline struct phy_provider *__devm_of_phy_provider_register(struct device
339         *dev, struct module *owner, struct phy * (*of_xlate)(struct device *dev,
340         struct of_phandle_args *args))
341 {
342         return ERR_PTR(-ENOSYS);
343 }
344
345 static inline void of_phy_provider_unregister(struct phy_provider *phy_provider)
346 {
347 }
348
349 static inline void devm_of_phy_provider_unregister(struct device *dev,
350         struct phy_provider *phy_provider)
351 {
352 }
353 static inline int
354 phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
355 {
356         return 0;
357 }
358 static inline void phy_remove_lookup(struct phy *phy, const char *con_id,
359                                      const char *dev_id) { }
360 #endif
361
362 #endif /* __DRIVERS_PHY_H */