]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/linux/usb/phy.h
ENGR00286426-7 usb: phy: add notify suspend and resume callback
[karo-tx-linux.git] / include / linux / usb / phy.h
1 /* USB OTG (On The Go) defines */
2 /*
3  *
4  * These APIs may be used between USB controllers.  USB device drivers
5  * (for either host or peripheral roles) don't use these calls; they
6  * continue to use just usb_device and usb_gadget.
7  */
8
9 #ifndef __LINUX_USB_PHY_H
10 #define __LINUX_USB_PHY_H
11
12 #include <linux/notifier.h>
13 #include <linux/usb.h>
14
15 enum usb_phy_interface {
16         USBPHY_INTERFACE_MODE_UNKNOWN,
17         USBPHY_INTERFACE_MODE_UTMI,
18         USBPHY_INTERFACE_MODE_UTMIW,
19         USBPHY_INTERFACE_MODE_ULPI,
20         USBPHY_INTERFACE_MODE_SERIAL,
21         USBPHY_INTERFACE_MODE_HSIC,
22 };
23
24 enum usb_phy_events {
25         USB_EVENT_NONE,         /* no events or cable disconnected */
26         USB_EVENT_VBUS,         /* vbus valid event */
27         USB_EVENT_ID,           /* id was grounded */
28         USB_EVENT_CHARGER,      /* usb dedicated charger */
29         USB_EVENT_ENUMERATED,   /* gadget driver enumerated */
30 };
31
32 /* associate a type with PHY */
33 enum usb_phy_type {
34         USB_PHY_TYPE_UNDEFINED,
35         USB_PHY_TYPE_USB2,
36         USB_PHY_TYPE_USB3,
37 };
38
39 /* OTG defines lots of enumeration states before device reset */
40 enum usb_otg_state {
41         OTG_STATE_UNDEFINED = 0,
42
43         /* single-role peripheral, and dual-role default-b */
44         OTG_STATE_B_IDLE,
45         OTG_STATE_B_SRP_INIT,
46         OTG_STATE_B_PERIPHERAL,
47
48         /* extra dual-role default-b states */
49         OTG_STATE_B_WAIT_ACON,
50         OTG_STATE_B_HOST,
51
52         /* dual-role default-a */
53         OTG_STATE_A_IDLE,
54         OTG_STATE_A_WAIT_VRISE,
55         OTG_STATE_A_WAIT_BCON,
56         OTG_STATE_A_HOST,
57         OTG_STATE_A_SUSPEND,
58         OTG_STATE_A_PERIPHERAL,
59         OTG_STATE_A_WAIT_VFALL,
60         OTG_STATE_A_VBUS_ERR,
61 };
62
63 struct usb_phy;
64 struct usb_otg;
65
66 /* for transceivers connected thru an ULPI interface, the user must
67  * provide access ops
68  */
69 struct usb_phy_io_ops {
70         int (*read)(struct usb_phy *x, u32 reg);
71         int (*write)(struct usb_phy *x, u32 val, u32 reg);
72 };
73
74 struct usb_phy {
75         struct device           *dev;
76         const char              *label;
77         unsigned int             flags;
78
79         enum usb_phy_type       type;
80         enum usb_otg_state      state;
81         enum usb_phy_events     last_event;
82
83         struct usb_otg          *otg;
84
85         struct device           *io_dev;
86         struct usb_phy_io_ops   *io_ops;
87         void __iomem            *io_priv;
88
89         /* for notification of usb_phy_events */
90         struct atomic_notifier_head     notifier;
91
92         /* to pass extra port status to the root hub */
93         u16                     port_status;
94         u16                     port_change;
95
96         /* to support controllers that have multiple transceivers */
97         struct list_head        head;
98
99         /* initialize/shutdown the OTG controller */
100         int     (*init)(struct usb_phy *x);
101         void    (*shutdown)(struct usb_phy *x);
102
103         /* enable/disable VBUS */
104         int     (*set_vbus)(struct usb_phy *x, int on);
105
106         /* effective for B devices, ignored for A-peripheral */
107         int     (*set_power)(struct usb_phy *x,
108                                 unsigned mA);
109
110         /* for non-OTG B devices: set transceiver into suspend mode */
111         int     (*set_suspend)(struct usb_phy *x,
112                                 int suspend);
113
114         /*
115          * Set wakeup enable for PHY, in that case, the PHY can be
116          * woken up from suspend status due to external events,
117          * like vbus change, dp/dm change and id.
118          */
119         int     (*set_wakeup)(struct usb_phy *x, bool enabled);
120
121         /* notify phy connect status change */
122         int     (*notify_connect)(struct usb_phy *x,
123                         enum usb_device_speed speed);
124         int     (*notify_disconnect)(struct usb_phy *x,
125                         enum usb_device_speed speed);
126         int     (*notify_suspend)(struct usb_phy *x,
127                         enum usb_device_speed speed);
128         int     (*notify_resume)(struct usb_phy *x,
129                         enum usb_device_speed speed);
130
131 };
132
133 /**
134  * struct usb_phy_bind - represent the binding for the phy
135  * @dev_name: the device name of the device that will bind to the phy
136  * @phy_dev_name: the device name of the phy
137  * @index: used if a single controller uses multiple phys
138  * @phy: reference to the phy
139  * @list: to maintain a linked list of the binding information
140  */
141 struct usb_phy_bind {
142         const char      *dev_name;
143         const char      *phy_dev_name;
144         u8              index;
145         struct usb_phy  *phy;
146         struct list_head list;
147 };
148
149 /* for board-specific init logic */
150 extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
151 extern int usb_add_phy_dev(struct usb_phy *);
152 extern void usb_remove_phy(struct usb_phy *);
153
154 /* helpers for direct access thru low-level io interface */
155 static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
156 {
157         if (x && x->io_ops && x->io_ops->read)
158                 return x->io_ops->read(x, reg);
159
160         return -EINVAL;
161 }
162
163 static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
164 {
165         if (x && x->io_ops && x->io_ops->write)
166                 return x->io_ops->write(x, val, reg);
167
168         return -EINVAL;
169 }
170
171 static inline int
172 usb_phy_init(struct usb_phy *x)
173 {
174         if (x && x->init)
175                 return x->init(x);
176
177         return 0;
178 }
179
180 static inline void
181 usb_phy_shutdown(struct usb_phy *x)
182 {
183         if (x && x->shutdown)
184                 x->shutdown(x);
185 }
186
187 static inline int
188 usb_phy_vbus_on(struct usb_phy *x)
189 {
190         if (!x || !x->set_vbus)
191                 return 0;
192
193         return x->set_vbus(x, true);
194 }
195
196 static inline int
197 usb_phy_vbus_off(struct usb_phy *x)
198 {
199         if (!x || !x->set_vbus)
200                 return 0;
201
202         return x->set_vbus(x, false);
203 }
204
205 /* for usb host and peripheral controller drivers */
206 #if IS_ENABLED(CONFIG_USB_PHY)
207 extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
208 extern struct usb_phy *devm_usb_get_phy(struct device *dev,
209         enum usb_phy_type type);
210 extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
211 extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
212 extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
213         const char *phandle, u8 index);
214 extern void usb_put_phy(struct usb_phy *);
215 extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
216 extern int usb_bind_phy(const char *dev_name, u8 index,
217                                 const char *phy_dev_name);
218 #else
219 static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
220 {
221         return ERR_PTR(-ENXIO);
222 }
223
224 static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
225         enum usb_phy_type type)
226 {
227         return ERR_PTR(-ENXIO);
228 }
229
230 static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
231 {
232         return ERR_PTR(-ENXIO);
233 }
234
235 static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
236 {
237         return ERR_PTR(-ENXIO);
238 }
239
240 static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
241         const char *phandle, u8 index)
242 {
243         return ERR_PTR(-ENXIO);
244 }
245
246 static inline void usb_put_phy(struct usb_phy *x)
247 {
248 }
249
250 static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
251 {
252 }
253
254 static inline int usb_bind_phy(const char *dev_name, u8 index,
255                                 const char *phy_dev_name)
256 {
257         return -EOPNOTSUPP;
258 }
259 #endif
260
261 static inline int
262 usb_phy_set_power(struct usb_phy *x, unsigned mA)
263 {
264         if (x && x->set_power)
265                 return x->set_power(x, mA);
266         return 0;
267 }
268
269 /* Context: can sleep */
270 static inline int
271 usb_phy_set_suspend(struct usb_phy *x, int suspend)
272 {
273         if (x && x->set_suspend != NULL)
274                 return x->set_suspend(x, suspend);
275         else
276                 return 0;
277 }
278
279 static inline int
280 usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
281 {
282         if (x && x->set_wakeup)
283                 return x->set_wakeup(x, enabled);
284         else
285                 return 0;
286 }
287
288 static inline int
289 usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
290 {
291         if (x && x->notify_connect)
292                 return x->notify_connect(x, speed);
293         else
294                 return 0;
295 }
296
297 static inline int
298 usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
299 {
300         if (x && x->notify_disconnect)
301                 return x->notify_disconnect(x, speed);
302         else
303                 return 0;
304 }
305
306 static inline int usb_phy_notify_suspend
307         (struct usb_phy *x, enum usb_device_speed speed)
308 {
309         if (x && x->notify_suspend)
310                 return x->notify_suspend(x, speed);
311         else
312                 return 0;
313 }
314
315 static inline int usb_phy_notify_resume
316         (struct usb_phy *x, enum usb_device_speed speed)
317 {
318         if (x && x->notify_resume)
319                 return x->notify_resume(x, speed);
320         else
321                 return 0;
322 }
323
324 /* notifiers */
325 static inline int
326 usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
327 {
328         return atomic_notifier_chain_register(&x->notifier, nb);
329 }
330
331 static inline void
332 usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
333 {
334         atomic_notifier_chain_unregister(&x->notifier, nb);
335 }
336
337 static inline const char *usb_phy_type_string(enum usb_phy_type type)
338 {
339         switch (type) {
340         case USB_PHY_TYPE_USB2:
341                 return "USB2 PHY";
342         case USB_PHY_TYPE_USB3:
343                 return "USB3 PHY";
344         default:
345                 return "UNKNOWN PHY TYPE";
346         }
347 }
348 #endif /* __LINUX_USB_PHY_H */