blob: 4caac13c2d97992686a6165e90b6c11e7abb04af [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)
Pratham Pratapc69aa332018-09-12 13:35:37 +053025#define PHY_USB_DP_CONCURRENT_MODE BIT(10)
Mayank Rana83ad5822016-08-09 14:17:22 -070026
Michael Grzeschik1c9af652013-06-13 17:59:55 +030027enum usb_phy_interface {
28 USBPHY_INTERFACE_MODE_UNKNOWN,
29 USBPHY_INTERFACE_MODE_UTMI,
30 USBPHY_INTERFACE_MODE_UTMIW,
31 USBPHY_INTERFACE_MODE_ULPI,
32 USBPHY_INTERFACE_MODE_SERIAL,
33 USBPHY_INTERFACE_MODE_HSIC,
34};
35
Venu Byravarasude4217d2012-09-04 14:25:58 +053036enum usb_phy_events {
37 USB_EVENT_NONE, /* no events or cable disconnected */
38 USB_EVENT_VBUS, /* vbus valid event */
39 USB_EVENT_ID, /* id was grounded */
40 USB_EVENT_CHARGER, /* usb dedicated charger */
41 USB_EVENT_ENUMERATED, /* gadget driver enumerated */
42};
43
44/* associate a type with PHY */
45enum usb_phy_type {
46 USB_PHY_TYPE_UNDEFINED,
47 USB_PHY_TYPE_USB2,
48 USB_PHY_TYPE_USB3,
Hemant Kumarbce97a22017-12-07 16:17:38 -080049 USB_PHY_TYPE_USB3_OR_DP,
50 USB_PHY_TYPE_USB3_AND_DP,
Venu Byravarasude4217d2012-09-04 14:25:58 +053051};
52
Venu Byravarasua4c3dde2012-09-06 10:42:15 +053053/* OTG defines lots of enumeration states before device reset */
54enum usb_otg_state {
55 OTG_STATE_UNDEFINED = 0,
56
57 /* single-role peripheral, and dual-role default-b */
58 OTG_STATE_B_IDLE,
59 OTG_STATE_B_SRP_INIT,
60 OTG_STATE_B_PERIPHERAL,
Mayank Ranaa99689a2016-08-10 17:39:47 -070061 OTG_STATE_B_SUSPEND,
Ajay Agarwal1ef64692018-01-23 19:46:25 +053062 OTG_STATE_B_CHARGER,
Venu Byravarasua4c3dde2012-09-06 10:42:15 +053063
64 /* extra dual-role default-b states */
65 OTG_STATE_B_WAIT_ACON,
66 OTG_STATE_B_HOST,
67
68 /* dual-role default-a */
69 OTG_STATE_A_IDLE,
70 OTG_STATE_A_WAIT_VRISE,
71 OTG_STATE_A_WAIT_BCON,
72 OTG_STATE_A_HOST,
73 OTG_STATE_A_SUSPEND,
74 OTG_STATE_A_PERIPHERAL,
75 OTG_STATE_A_WAIT_VFALL,
76 OTG_STATE_A_VBUS_ERR,
77};
78
Venu Byravarasude4217d2012-09-04 14:25:58 +053079struct usb_phy;
80struct usb_otg;
81
Peter Chen58efd4b02015-09-22 15:31:34 +080082/* for phys connected thru an ULPI interface, the user must
Venu Byravarasude4217d2012-09-04 14:25:58 +053083 * provide access ops
84 */
85struct usb_phy_io_ops {
86 int (*read)(struct usb_phy *x, u32 reg);
87 int (*write)(struct usb_phy *x, u32 val, u32 reg);
88};
89
90struct usb_phy {
91 struct device *dev;
92 const char *label;
93 unsigned int flags;
94
95 enum usb_phy_type type;
Venu Byravarasude4217d2012-09-04 14:25:58 +053096 enum usb_phy_events last_event;
97
98 struct usb_otg *otg;
99
100 struct device *io_dev;
101 struct usb_phy_io_ops *io_ops;
102 void __iomem *io_priv;
103
104 /* for notification of usb_phy_events */
105 struct atomic_notifier_head notifier;
106
107 /* to pass extra port status to the root hub */
108 u16 port_status;
109 u16 port_change;
110
Peter Chen58efd4b02015-09-22 15:31:34 +0800111 /* to support controllers that have multiple phys */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530112 struct list_head head;
113
Peter Chen58efd4b02015-09-22 15:31:34 +0800114 /* initialize/shutdown the phy */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530115 int (*init)(struct usb_phy *x);
116 void (*shutdown)(struct usb_phy *x);
117
Felipe Balbib7742122013-03-08 13:22:58 +0200118 /* enable/disable VBUS */
119 int (*set_vbus)(struct usb_phy *x, int on);
Hemant Kumar2bb3bdf2017-11-22 13:53:08 -0800120 /* callback to indicate port is being reset or reset the port */
121 void (*start_port_reset)(struct usb_phy *x);
Felipe Balbib7742122013-03-08 13:22:58 +0200122
Venu Byravarasude4217d2012-09-04 14:25:58 +0530123 /* effective for B devices, ignored for A-peripheral */
124 int (*set_power)(struct usb_phy *x,
125 unsigned mA);
126
Peter Chen58efd4b02015-09-22 15:31:34 +0800127 /* Set phy into suspend mode */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530128 int (*set_suspend)(struct usb_phy *x,
129 int suspend);
130
Peter Chen57bf9b02014-02-24 10:21:01 +0800131 /*
132 * Set wakeup enable for PHY, in that case, the PHY can be
133 * woken up from suspend status due to external events,
134 * like vbus change, dp/dm change and id.
135 */
136 int (*set_wakeup)(struct usb_phy *x, bool enabled);
137
Venu Byravarasude4217d2012-09-04 14:25:58 +0530138 /* notify phy connect status change */
Peter Chenac965112012-11-09 09:44:44 +0800139 int (*notify_connect)(struct usb_phy *x,
140 enum usb_device_speed speed);
141 int (*notify_disconnect)(struct usb_phy *x,
142 enum usb_device_speed speed);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700143
144 /* reset the PHY clocks */
145 int (*reset)(struct usb_phy *x);
Ajay Agarwal1ef64692018-01-23 19:46:25 +0530146
147 /* for notification of usb_phy_dbg_events */
148 void (*dbg_event)(struct usb_phy *x,
149 char *event, int msg1, int msg2);
Hemant Kumar91f5e542017-11-20 16:25:46 -0800150 int (*disable_chirp)(struct usb_phy *x, bool disable);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530151};
152
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530153/**
154 * struct usb_phy_bind - represent the binding for the phy
155 * @dev_name: the device name of the device that will bind to the phy
156 * @phy_dev_name: the device name of the phy
157 * @index: used if a single controller uses multiple phys
158 * @phy: reference to the phy
159 * @list: to maintain a linked list of the binding information
160 */
161struct usb_phy_bind {
162 const char *dev_name;
163 const char *phy_dev_name;
164 u8 index;
165 struct usb_phy *phy;
166 struct list_head list;
167};
Venu Byravarasude4217d2012-09-04 14:25:58 +0530168
169/* for board-specific init logic */
170extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530171extern int usb_add_phy_dev(struct usb_phy *);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530172extern void usb_remove_phy(struct usb_phy *);
173
174/* helpers for direct access thru low-level io interface */
175static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
176{
Felipe Balbi410aee72013-06-30 15:27:26 +0300177 if (x && x->io_ops && x->io_ops->read)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530178 return x->io_ops->read(x, reg);
179
180 return -EINVAL;
181}
182
183static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
184{
Felipe Balbi410aee72013-06-30 15:27:26 +0300185 if (x && x->io_ops && x->io_ops->write)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530186 return x->io_ops->write(x, val, reg);
187
188 return -EINVAL;
189}
190
191static inline int
192usb_phy_init(struct usb_phy *x)
193{
Felipe Balbi410aee72013-06-30 15:27:26 +0300194 if (x && x->init)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530195 return x->init(x);
196
197 return 0;
198}
199
200static inline void
201usb_phy_shutdown(struct usb_phy *x)
202{
Felipe Balbi410aee72013-06-30 15:27:26 +0300203 if (x && x->shutdown)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530204 x->shutdown(x);
205}
206
Felipe Balbib7742122013-03-08 13:22:58 +0200207static inline int
208usb_phy_vbus_on(struct usb_phy *x)
209{
Felipe Balbi410aee72013-06-30 15:27:26 +0300210 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200211 return 0;
212
213 return x->set_vbus(x, true);
214}
215
216static inline int
217usb_phy_vbus_off(struct usb_phy *x)
218{
Felipe Balbi410aee72013-06-30 15:27:26 +0300219 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200220 return 0;
221
222 return x->set_vbus(x, false);
223}
224
Hemant Kumar2bb3bdf2017-11-22 13:53:08 -0800225static inline void
226usb_phy_start_port_reset(struct usb_phy *x)
227{
228 if (!x || !x->start_port_reset)
229 return;
230
231 x->start_port_reset(x);
232}
233
Mayank Ranaa99689a2016-08-10 17:39:47 -0700234static inline int
235usb_phy_reset(struct usb_phy *x)
236{
237 if (x && x->reset)
238 return x->reset(x);
239
240 return 0;
241}
242
Venu Byravarasude4217d2012-09-04 14:25:58 +0530243/* for usb host and peripheral controller drivers */
Felipe Balbiedc7cb22013-03-07 11:13:43 +0200244#if IS_ENABLED(CONFIG_USB_PHY)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530245extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
246extern struct usb_phy *devm_usb_get_phy(struct device *dev,
247 enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530248extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
249extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530250extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
251 const char *phandle, u8 index);
NeilBrowne842b842015-03-23 09:52:48 +1100252extern struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
253 struct device_node *node, struct notifier_block *nb);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530254extern void usb_put_phy(struct usb_phy *);
255extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530256extern int usb_bind_phy(const char *dev_name, u8 index,
257 const char *phy_dev_name);
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530258extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530259#else
260static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
261{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200262 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530263}
264
265static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
266 enum usb_phy_type type)
267{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200268 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530269}
270
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530271static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
272{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200273 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530274}
275
276static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
277{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200278 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530279}
280
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530281static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
282 const char *phandle, u8 index)
283{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200284 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530285}
286
Arnd Bergmann307c8582015-05-28 16:08:02 +0200287static inline struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
288 struct device_node *node, struct notifier_block *nb)
289{
290 return ERR_PTR(-ENXIO);
291}
292
Venu Byravarasude4217d2012-09-04 14:25:58 +0530293static inline void usb_put_phy(struct usb_phy *x)
294{
295}
296
297static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
298{
299}
300
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530301static inline int usb_bind_phy(const char *dev_name, u8 index,
302 const char *phy_dev_name)
303{
304 return -EOPNOTSUPP;
305}
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530306
307static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
308{
309}
Venu Byravarasude4217d2012-09-04 14:25:58 +0530310#endif
311
312static inline int
313usb_phy_set_power(struct usb_phy *x, unsigned mA)
314{
315 if (x && x->set_power)
316 return x->set_power(x, mA);
317 return 0;
318}
319
320/* Context: can sleep */
321static inline int
322usb_phy_set_suspend(struct usb_phy *x, int suspend)
323{
Felipe Balbi410aee72013-06-30 15:27:26 +0300324 if (x && x->set_suspend != NULL)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530325 return x->set_suspend(x, suspend);
326 else
327 return 0;
328}
329
330static inline int
Peter Chen57bf9b02014-02-24 10:21:01 +0800331usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
332{
333 if (x && x->set_wakeup)
334 return x->set_wakeup(x, enabled);
335 else
336 return 0;
337}
338
339static inline int
Peter Chenac965112012-11-09 09:44:44 +0800340usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530341{
Felipe Balbi410aee72013-06-30 15:27:26 +0300342 if (x && x->notify_connect)
Peter Chenac965112012-11-09 09:44:44 +0800343 return x->notify_connect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530344 else
345 return 0;
346}
347
348static inline int
Peter Chenac965112012-11-09 09:44:44 +0800349usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530350{
Felipe Balbi410aee72013-06-30 15:27:26 +0300351 if (x && x->notify_disconnect)
Peter Chenac965112012-11-09 09:44:44 +0800352 return x->notify_disconnect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530353 else
354 return 0;
355}
356
357/* notifiers */
358static inline int
359usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
360{
361 return atomic_notifier_chain_register(&x->notifier, nb);
362}
363
364static inline void
365usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
366{
367 atomic_notifier_chain_unregister(&x->notifier, nb);
368}
369
370static inline const char *usb_phy_type_string(enum usb_phy_type type)
371{
372 switch (type) {
373 case USB_PHY_TYPE_USB2:
374 return "USB2 PHY";
375 case USB_PHY_TYPE_USB3:
376 return "USB3 PHY";
377 default:
378 return "UNKNOWN PHY TYPE";
379 }
380}
381#endif /* __LINUX_USB_PHY_H */