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