blob: bc91b5d380fdf5400b427082e01cbe11d3159f6b [file] [log] [blame]
Venu Byravarasude4217d2012-09-04 14:25:58 +05301/*
Peter Chen07673202014-12-30 14:02:28 +08002 * USB PHY defines
Venu Byravarasude4217d2012-09-04 14:25:58 +05303 *
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>
Peter Chenac965112012-11-09 09:44:44 +080013#include <linux/usb.h>
Venu Byravarasude4217d2012-09-04 14:25:58 +053014
Michael Grzeschik1c9af652013-06-13 17:59:55 +030015enum 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
Venu Byravarasude4217d2012-09-04 14:25:58 +053024enum 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 */
33enum usb_phy_type {
34 USB_PHY_TYPE_UNDEFINED,
35 USB_PHY_TYPE_USB2,
36 USB_PHY_TYPE_USB3,
37};
38
Venu Byravarasua4c3dde2012-09-06 10:42:15 +053039/* OTG defines lots of enumeration states before device reset */
40enum 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
Venu Byravarasude4217d2012-09-04 14:25:58 +053063struct usb_phy;
64struct usb_otg;
65
66/* for transceivers connected thru an ULPI interface, the user must
67 * provide access ops
68 */
69struct 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
74struct usb_phy {
75 struct device *dev;
76 const char *label;
77 unsigned int flags;
78
79 enum usb_phy_type type;
Venu Byravarasude4217d2012-09-04 14:25:58 +053080 enum usb_phy_events last_event;
81
82 struct usb_otg *otg;
83
84 struct device *io_dev;
85 struct usb_phy_io_ops *io_ops;
86 void __iomem *io_priv;
87
88 /* for notification of usb_phy_events */
89 struct atomic_notifier_head notifier;
90
91 /* to pass extra port status to the root hub */
92 u16 port_status;
93 u16 port_change;
94
95 /* to support controllers that have multiple transceivers */
96 struct list_head head;
97
98 /* initialize/shutdown the OTG controller */
99 int (*init)(struct usb_phy *x);
100 void (*shutdown)(struct usb_phy *x);
101
Felipe Balbib7742122013-03-08 13:22:58 +0200102 /* enable/disable VBUS */
103 int (*set_vbus)(struct usb_phy *x, int on);
104
Venu Byravarasude4217d2012-09-04 14:25:58 +0530105 /* effective for B devices, ignored for A-peripheral */
106 int (*set_power)(struct usb_phy *x,
107 unsigned mA);
108
Peter Chen07673202014-12-30 14:02:28 +0800109 /* Set transceiver into suspend mode */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530110 int (*set_suspend)(struct usb_phy *x,
111 int suspend);
112
Peter Chen57bf9b02014-02-24 10:21:01 +0800113 /*
114 * Set wakeup enable for PHY, in that case, the PHY can be
115 * woken up from suspend status due to external events,
116 * like vbus change, dp/dm change and id.
117 */
118 int (*set_wakeup)(struct usb_phy *x, bool enabled);
119
Venu Byravarasude4217d2012-09-04 14:25:58 +0530120 /* notify phy connect status change */
Peter Chenac965112012-11-09 09:44:44 +0800121 int (*notify_connect)(struct usb_phy *x,
122 enum usb_device_speed speed);
123 int (*notify_disconnect)(struct usb_phy *x,
124 enum usb_device_speed speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530125};
126
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530127/**
128 * struct usb_phy_bind - represent the binding for the phy
129 * @dev_name: the device name of the device that will bind to the phy
130 * @phy_dev_name: the device name of the phy
131 * @index: used if a single controller uses multiple phys
132 * @phy: reference to the phy
133 * @list: to maintain a linked list of the binding information
134 */
135struct usb_phy_bind {
136 const char *dev_name;
137 const char *phy_dev_name;
138 u8 index;
139 struct usb_phy *phy;
140 struct list_head list;
141};
Venu Byravarasude4217d2012-09-04 14:25:58 +0530142
143/* for board-specific init logic */
144extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530145extern int usb_add_phy_dev(struct usb_phy *);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530146extern void usb_remove_phy(struct usb_phy *);
147
148/* helpers for direct access thru low-level io interface */
149static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
150{
Felipe Balbi410aee72013-06-30 15:27:26 +0300151 if (x && x->io_ops && x->io_ops->read)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530152 return x->io_ops->read(x, reg);
153
154 return -EINVAL;
155}
156
157static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
158{
Felipe Balbi410aee72013-06-30 15:27:26 +0300159 if (x && x->io_ops && x->io_ops->write)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530160 return x->io_ops->write(x, val, reg);
161
162 return -EINVAL;
163}
164
165static inline int
166usb_phy_init(struct usb_phy *x)
167{
Felipe Balbi410aee72013-06-30 15:27:26 +0300168 if (x && x->init)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530169 return x->init(x);
170
171 return 0;
172}
173
174static inline void
175usb_phy_shutdown(struct usb_phy *x)
176{
Felipe Balbi410aee72013-06-30 15:27:26 +0300177 if (x && x->shutdown)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530178 x->shutdown(x);
179}
180
Felipe Balbib7742122013-03-08 13:22:58 +0200181static inline int
182usb_phy_vbus_on(struct usb_phy *x)
183{
Felipe Balbi410aee72013-06-30 15:27:26 +0300184 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200185 return 0;
186
187 return x->set_vbus(x, true);
188}
189
190static inline int
191usb_phy_vbus_off(struct usb_phy *x)
192{
Felipe Balbi410aee72013-06-30 15:27:26 +0300193 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200194 return 0;
195
196 return x->set_vbus(x, false);
197}
198
Venu Byravarasude4217d2012-09-04 14:25:58 +0530199/* for usb host and peripheral controller drivers */
Felipe Balbiedc7cb22013-03-07 11:13:43 +0200200#if IS_ENABLED(CONFIG_USB_PHY)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530201extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
202extern struct usb_phy *devm_usb_get_phy(struct device *dev,
203 enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530204extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
205extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530206extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
207 const char *phandle, u8 index);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530208extern void usb_put_phy(struct usb_phy *);
209extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530210extern int usb_bind_phy(const char *dev_name, u8 index,
211 const char *phy_dev_name);
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530212extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530213#else
214static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
215{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200216 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530217}
218
219static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
220 enum usb_phy_type type)
221{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200222 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530223}
224
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530225static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
226{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200227 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530228}
229
230static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
231{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200232 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530233}
234
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530235static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
236 const char *phandle, u8 index)
237{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200238 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530239}
240
Venu Byravarasude4217d2012-09-04 14:25:58 +0530241static inline void usb_put_phy(struct usb_phy *x)
242{
243}
244
245static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
246{
247}
248
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530249static inline int usb_bind_phy(const char *dev_name, u8 index,
250 const char *phy_dev_name)
251{
252 return -EOPNOTSUPP;
253}
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530254
255static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
256{
257}
Venu Byravarasude4217d2012-09-04 14:25:58 +0530258#endif
259
260static inline int
261usb_phy_set_power(struct usb_phy *x, unsigned mA)
262{
263 if (x && x->set_power)
264 return x->set_power(x, mA);
265 return 0;
266}
267
268/* Context: can sleep */
269static inline int
270usb_phy_set_suspend(struct usb_phy *x, int suspend)
271{
Felipe Balbi410aee72013-06-30 15:27:26 +0300272 if (x && x->set_suspend != NULL)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530273 return x->set_suspend(x, suspend);
274 else
275 return 0;
276}
277
278static inline int
Peter Chen57bf9b02014-02-24 10:21:01 +0800279usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
280{
281 if (x && x->set_wakeup)
282 return x->set_wakeup(x, enabled);
283 else
284 return 0;
285}
286
287static inline int
Peter Chenac965112012-11-09 09:44:44 +0800288usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530289{
Felipe Balbi410aee72013-06-30 15:27:26 +0300290 if (x && x->notify_connect)
Peter Chenac965112012-11-09 09:44:44 +0800291 return x->notify_connect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530292 else
293 return 0;
294}
295
296static inline int
Peter Chenac965112012-11-09 09:44:44 +0800297usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530298{
Felipe Balbi410aee72013-06-30 15:27:26 +0300299 if (x && x->notify_disconnect)
Peter Chenac965112012-11-09 09:44:44 +0800300 return x->notify_disconnect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530301 else
302 return 0;
303}
304
305/* notifiers */
306static inline int
307usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
308{
309 return atomic_notifier_chain_register(&x->notifier, nb);
310}
311
312static inline void
313usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
314{
315 atomic_notifier_chain_unregister(&x->notifier, nb);
316}
317
318static inline const char *usb_phy_type_string(enum usb_phy_type type)
319{
320 switch (type) {
321 case USB_PHY_TYPE_USB2:
322 return "USB2 PHY";
323 case USB_PHY_TYPE_USB3:
324 return "USB3 PHY";
325 default:
326 return "UNKNOWN PHY TYPE";
327 }
328}
329#endif /* __LINUX_USB_PHY_H */