blob: 263f20ab1ee68fce802cc8f8ff134f94ae49e2b8 [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
Mayank Rana83ad5822016-08-09 14:17:22 -070015#define ENABLE_DP_MANUAL_PULLUP BIT(0)
16#define ENABLE_SECONDARY_PHY BIT(1)
17#define PHY_HOST_MODE BIT(2)
18#define PHY_CHARGER_CONNECTED BIT(3)
19#define PHY_VBUS_VALID_OVERRIDE BIT(4)
20#define DEVICE_IN_SS_MODE BIT(5)
21#define PHY_LANE_A BIT(6)
22#define PHY_LANE_B BIT(7)
23#define PHY_HSFS_MODE BIT(8)
24#define PHY_LS_MODE BIT(9)
25
Michael Grzeschik1c9af652013-06-13 17:59:55 +030026enum usb_phy_interface {
27 USBPHY_INTERFACE_MODE_UNKNOWN,
28 USBPHY_INTERFACE_MODE_UTMI,
29 USBPHY_INTERFACE_MODE_UTMIW,
30 USBPHY_INTERFACE_MODE_ULPI,
31 USBPHY_INTERFACE_MODE_SERIAL,
32 USBPHY_INTERFACE_MODE_HSIC,
33};
34
Venu Byravarasude4217d2012-09-04 14:25:58 +053035enum usb_phy_events {
36 USB_EVENT_NONE, /* no events or cable disconnected */
37 USB_EVENT_VBUS, /* vbus valid event */
38 USB_EVENT_ID, /* id was grounded */
39 USB_EVENT_CHARGER, /* usb dedicated charger */
40 USB_EVENT_ENUMERATED, /* gadget driver enumerated */
41};
42
43/* associate a type with PHY */
44enum usb_phy_type {
45 USB_PHY_TYPE_UNDEFINED,
46 USB_PHY_TYPE_USB2,
47 USB_PHY_TYPE_USB3,
48};
49
Venu Byravarasua4c3dde2012-09-06 10:42:15 +053050/* OTG defines lots of enumeration states before device reset */
51enum usb_otg_state {
52 OTG_STATE_UNDEFINED = 0,
53
54 /* single-role peripheral, and dual-role default-b */
55 OTG_STATE_B_IDLE,
56 OTG_STATE_B_SRP_INIT,
57 OTG_STATE_B_PERIPHERAL,
Mayank Ranaa99689a2016-08-10 17:39:47 -070058 OTG_STATE_B_SUSPEND,
Venu Byravarasua4c3dde2012-09-06 10:42:15 +053059
60 /* extra dual-role default-b states */
61 OTG_STATE_B_WAIT_ACON,
62 OTG_STATE_B_HOST,
63
64 /* dual-role default-a */
65 OTG_STATE_A_IDLE,
66 OTG_STATE_A_WAIT_VRISE,
67 OTG_STATE_A_WAIT_BCON,
68 OTG_STATE_A_HOST,
69 OTG_STATE_A_SUSPEND,
70 OTG_STATE_A_PERIPHERAL,
71 OTG_STATE_A_WAIT_VFALL,
72 OTG_STATE_A_VBUS_ERR,
73};
74
Venu Byravarasude4217d2012-09-04 14:25:58 +053075struct usb_phy;
76struct usb_otg;
77
Peter Chen58efd4b02015-09-22 15:31:34 +080078/* for phys connected thru an ULPI interface, the user must
Venu Byravarasude4217d2012-09-04 14:25:58 +053079 * provide access ops
80 */
81struct usb_phy_io_ops {
82 int (*read)(struct usb_phy *x, u32 reg);
83 int (*write)(struct usb_phy *x, u32 val, u32 reg);
84};
85
86struct usb_phy {
87 struct device *dev;
88 const char *label;
89 unsigned int flags;
90
91 enum usb_phy_type type;
Venu Byravarasude4217d2012-09-04 14:25:58 +053092 enum usb_phy_events last_event;
93
94 struct usb_otg *otg;
95
96 struct device *io_dev;
97 struct usb_phy_io_ops *io_ops;
98 void __iomem *io_priv;
99
100 /* for notification of usb_phy_events */
101 struct atomic_notifier_head notifier;
102
103 /* to pass extra port status to the root hub */
104 u16 port_status;
105 u16 port_change;
106
Peter Chen58efd4b02015-09-22 15:31:34 +0800107 /* to support controllers that have multiple phys */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530108 struct list_head head;
109
Peter Chen58efd4b02015-09-22 15:31:34 +0800110 /* initialize/shutdown the phy */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530111 int (*init)(struct usb_phy *x);
112 void (*shutdown)(struct usb_phy *x);
113
Felipe Balbib7742122013-03-08 13:22:58 +0200114 /* enable/disable VBUS */
115 int (*set_vbus)(struct usb_phy *x, int on);
116
Venu Byravarasude4217d2012-09-04 14:25:58 +0530117 /* effective for B devices, ignored for A-peripheral */
118 int (*set_power)(struct usb_phy *x,
119 unsigned mA);
120
Peter Chen58efd4b02015-09-22 15:31:34 +0800121 /* Set phy into suspend mode */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530122 int (*set_suspend)(struct usb_phy *x,
123 int suspend);
124
Peter Chen57bf9b02014-02-24 10:21:01 +0800125 /*
126 * Set wakeup enable for PHY, in that case, the PHY can be
127 * woken up from suspend status due to external events,
128 * like vbus change, dp/dm change and id.
129 */
130 int (*set_wakeup)(struct usb_phy *x, bool enabled);
131
Venu Byravarasude4217d2012-09-04 14:25:58 +0530132 /* notify phy connect status change */
Peter Chenac965112012-11-09 09:44:44 +0800133 int (*notify_connect)(struct usb_phy *x,
134 enum usb_device_speed speed);
135 int (*notify_disconnect)(struct usb_phy *x,
136 enum usb_device_speed speed);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700137
138 /* reset the PHY clocks */
139 int (*reset)(struct usb_phy *x);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530140};
141
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530142/**
143 * struct usb_phy_bind - represent the binding for the phy
144 * @dev_name: the device name of the device that will bind to the phy
145 * @phy_dev_name: the device name of the phy
146 * @index: used if a single controller uses multiple phys
147 * @phy: reference to the phy
148 * @list: to maintain a linked list of the binding information
149 */
150struct usb_phy_bind {
151 const char *dev_name;
152 const char *phy_dev_name;
153 u8 index;
154 struct usb_phy *phy;
155 struct list_head list;
156};
Venu Byravarasude4217d2012-09-04 14:25:58 +0530157
158/* for board-specific init logic */
159extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530160extern int usb_add_phy_dev(struct usb_phy *);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530161extern void usb_remove_phy(struct usb_phy *);
162
163/* helpers for direct access thru low-level io interface */
164static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
165{
Felipe Balbi410aee72013-06-30 15:27:26 +0300166 if (x && x->io_ops && x->io_ops->read)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530167 return x->io_ops->read(x, reg);
168
169 return -EINVAL;
170}
171
172static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
173{
Felipe Balbi410aee72013-06-30 15:27:26 +0300174 if (x && x->io_ops && x->io_ops->write)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530175 return x->io_ops->write(x, val, reg);
176
177 return -EINVAL;
178}
179
180static inline int
181usb_phy_init(struct usb_phy *x)
182{
Felipe Balbi410aee72013-06-30 15:27:26 +0300183 if (x && x->init)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530184 return x->init(x);
185
186 return 0;
187}
188
189static inline void
190usb_phy_shutdown(struct usb_phy *x)
191{
Felipe Balbi410aee72013-06-30 15:27:26 +0300192 if (x && x->shutdown)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530193 x->shutdown(x);
194}
195
Felipe Balbib7742122013-03-08 13:22:58 +0200196static inline int
197usb_phy_vbus_on(struct usb_phy *x)
198{
Felipe Balbi410aee72013-06-30 15:27:26 +0300199 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200200 return 0;
201
202 return x->set_vbus(x, true);
203}
204
205static inline int
206usb_phy_vbus_off(struct usb_phy *x)
207{
Felipe Balbi410aee72013-06-30 15:27:26 +0300208 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200209 return 0;
210
211 return x->set_vbus(x, false);
212}
213
Mayank Ranaa99689a2016-08-10 17:39:47 -0700214static inline int
215usb_phy_reset(struct usb_phy *x)
216{
217 if (x && x->reset)
218 return x->reset(x);
219
220 return 0;
221}
222
Venu Byravarasude4217d2012-09-04 14:25:58 +0530223/* for usb host and peripheral controller drivers */
Felipe Balbiedc7cb22013-03-07 11:13:43 +0200224#if IS_ENABLED(CONFIG_USB_PHY)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530225extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
226extern struct usb_phy *devm_usb_get_phy(struct device *dev,
227 enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530228extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
229extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530230extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
231 const char *phandle, u8 index);
NeilBrowne842b842015-03-23 09:52:48 +1100232extern struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
233 struct device_node *node, struct notifier_block *nb);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530234extern void usb_put_phy(struct usb_phy *);
235extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530236extern int usb_bind_phy(const char *dev_name, u8 index,
237 const char *phy_dev_name);
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530238extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530239#else
240static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
241{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200242 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530243}
244
245static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
246 enum usb_phy_type type)
247{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200248 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530249}
250
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530251static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
252{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200253 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530254}
255
256static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
257{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200258 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530259}
260
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530261static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
262 const char *phandle, u8 index)
263{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200264 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530265}
266
Arnd Bergmann307c8582015-05-28 16:08:02 +0200267static inline struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
268 struct device_node *node, struct notifier_block *nb)
269{
270 return ERR_PTR(-ENXIO);
271}
272
Venu Byravarasude4217d2012-09-04 14:25:58 +0530273static inline void usb_put_phy(struct usb_phy *x)
274{
275}
276
277static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
278{
279}
280
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530281static inline int usb_bind_phy(const char *dev_name, u8 index,
282 const char *phy_dev_name)
283{
284 return -EOPNOTSUPP;
285}
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530286
287static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
288{
289}
Venu Byravarasude4217d2012-09-04 14:25:58 +0530290#endif
291
292static inline int
293usb_phy_set_power(struct usb_phy *x, unsigned mA)
294{
295 if (x && x->set_power)
296 return x->set_power(x, mA);
297 return 0;
298}
299
300/* Context: can sleep */
301static inline int
302usb_phy_set_suspend(struct usb_phy *x, int suspend)
303{
Felipe Balbi410aee72013-06-30 15:27:26 +0300304 if (x && x->set_suspend != NULL)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530305 return x->set_suspend(x, suspend);
306 else
307 return 0;
308}
309
310static inline int
Peter Chen57bf9b02014-02-24 10:21:01 +0800311usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
312{
313 if (x && x->set_wakeup)
314 return x->set_wakeup(x, enabled);
315 else
316 return 0;
317}
318
319static inline int
Peter Chenac965112012-11-09 09:44:44 +0800320usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530321{
Felipe Balbi410aee72013-06-30 15:27:26 +0300322 if (x && x->notify_connect)
Peter Chenac965112012-11-09 09:44:44 +0800323 return x->notify_connect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530324 else
325 return 0;
326}
327
328static inline int
Peter Chenac965112012-11-09 09:44:44 +0800329usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530330{
Felipe Balbi410aee72013-06-30 15:27:26 +0300331 if (x && x->notify_disconnect)
Peter Chenac965112012-11-09 09:44:44 +0800332 return x->notify_disconnect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530333 else
334 return 0;
335}
336
337/* notifiers */
338static inline int
339usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
340{
341 return atomic_notifier_chain_register(&x->notifier, nb);
342}
343
344static inline void
345usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
346{
347 atomic_notifier_chain_unregister(&x->notifier, nb);
348}
349
350static inline const char *usb_phy_type_string(enum usb_phy_type type)
351{
352 switch (type) {
353 case USB_PHY_TYPE_USB2:
354 return "USB2 PHY";
355 case USB_PHY_TYPE_USB3:
356 return "USB3 PHY";
357 default:
358 return "UNKNOWN PHY TYPE";
359 }
360}
361#endif /* __LINUX_USB_PHY_H */