blob: 092c32e8d91066e2f1bd34632b2cdb33a5bc5b02 [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,
Mayank Rana4c40cd92017-03-21 14:40:55 -070048 USB_PHY_TYPE_USB3_DP,
Venu Byravarasude4217d2012-09-04 14:25:58 +053049};
50
Venu Byravarasua4c3dde2012-09-06 10:42:15 +053051/* OTG defines lots of enumeration states before device reset */
52enum usb_otg_state {
53 OTG_STATE_UNDEFINED = 0,
54
55 /* single-role peripheral, and dual-role default-b */
56 OTG_STATE_B_IDLE,
57 OTG_STATE_B_SRP_INIT,
58 OTG_STATE_B_PERIPHERAL,
Mayank Ranaa99689a2016-08-10 17:39:47 -070059 OTG_STATE_B_SUSPEND,
Venu Byravarasua4c3dde2012-09-06 10:42:15 +053060
61 /* extra dual-role default-b states */
62 OTG_STATE_B_WAIT_ACON,
63 OTG_STATE_B_HOST,
64
65 /* dual-role default-a */
66 OTG_STATE_A_IDLE,
67 OTG_STATE_A_WAIT_VRISE,
68 OTG_STATE_A_WAIT_BCON,
69 OTG_STATE_A_HOST,
70 OTG_STATE_A_SUSPEND,
71 OTG_STATE_A_PERIPHERAL,
72 OTG_STATE_A_WAIT_VFALL,
73 OTG_STATE_A_VBUS_ERR,
74};
75
Venu Byravarasude4217d2012-09-04 14:25:58 +053076struct usb_phy;
77struct usb_otg;
78
Peter Chen58efd4b02015-09-22 15:31:34 +080079/* for phys connected thru an ULPI interface, the user must
Venu Byravarasude4217d2012-09-04 14:25:58 +053080 * provide access ops
81 */
82struct usb_phy_io_ops {
83 int (*read)(struct usb_phy *x, u32 reg);
84 int (*write)(struct usb_phy *x, u32 val, u32 reg);
85};
86
87struct usb_phy {
88 struct device *dev;
89 const char *label;
90 unsigned int flags;
91
92 enum usb_phy_type type;
Venu Byravarasude4217d2012-09-04 14:25:58 +053093 enum usb_phy_events last_event;
94
95 struct usb_otg *otg;
96
97 struct device *io_dev;
98 struct usb_phy_io_ops *io_ops;
99 void __iomem *io_priv;
100
101 /* for notification of usb_phy_events */
102 struct atomic_notifier_head notifier;
103
104 /* to pass extra port status to the root hub */
105 u16 port_status;
106 u16 port_change;
107
Peter Chen58efd4b02015-09-22 15:31:34 +0800108 /* to support controllers that have multiple phys */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530109 struct list_head head;
110
Peter Chen58efd4b02015-09-22 15:31:34 +0800111 /* initialize/shutdown the phy */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530112 int (*init)(struct usb_phy *x);
113 void (*shutdown)(struct usb_phy *x);
114
Felipe Balbib7742122013-03-08 13:22:58 +0200115 /* enable/disable VBUS */
116 int (*set_vbus)(struct usb_phy *x, int on);
117
Venu Byravarasude4217d2012-09-04 14:25:58 +0530118 /* effective for B devices, ignored for A-peripheral */
119 int (*set_power)(struct usb_phy *x,
120 unsigned mA);
121
Peter Chen58efd4b02015-09-22 15:31:34 +0800122 /* Set phy into suspend mode */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530123 int (*set_suspend)(struct usb_phy *x,
124 int suspend);
125
Peter Chen57bf9b02014-02-24 10:21:01 +0800126 /*
127 * Set wakeup enable for PHY, in that case, the PHY can be
128 * woken up from suspend status due to external events,
129 * like vbus change, dp/dm change and id.
130 */
131 int (*set_wakeup)(struct usb_phy *x, bool enabled);
132
Venu Byravarasude4217d2012-09-04 14:25:58 +0530133 /* notify phy connect status change */
Peter Chenac965112012-11-09 09:44:44 +0800134 int (*notify_connect)(struct usb_phy *x,
135 enum usb_device_speed speed);
136 int (*notify_disconnect)(struct usb_phy *x,
137 enum usb_device_speed speed);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700138
139 /* reset the PHY clocks */
140 int (*reset)(struct usb_phy *x);
Hemant Kumar91f5e542017-11-20 16:25:46 -0800141 int (*disable_chirp)(struct usb_phy *x, bool disable);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530142};
143
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530144/**
145 * struct usb_phy_bind - represent the binding for the phy
146 * @dev_name: the device name of the device that will bind to the phy
147 * @phy_dev_name: the device name of the phy
148 * @index: used if a single controller uses multiple phys
149 * @phy: reference to the phy
150 * @list: to maintain a linked list of the binding information
151 */
152struct usb_phy_bind {
153 const char *dev_name;
154 const char *phy_dev_name;
155 u8 index;
156 struct usb_phy *phy;
157 struct list_head list;
158};
Venu Byravarasude4217d2012-09-04 14:25:58 +0530159
160/* for board-specific init logic */
161extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530162extern int usb_add_phy_dev(struct usb_phy *);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530163extern void usb_remove_phy(struct usb_phy *);
164
165/* helpers for direct access thru low-level io interface */
166static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
167{
Felipe Balbi410aee72013-06-30 15:27:26 +0300168 if (x && x->io_ops && x->io_ops->read)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530169 return x->io_ops->read(x, reg);
170
171 return -EINVAL;
172}
173
174static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
175{
Felipe Balbi410aee72013-06-30 15:27:26 +0300176 if (x && x->io_ops && x->io_ops->write)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530177 return x->io_ops->write(x, val, reg);
178
179 return -EINVAL;
180}
181
182static inline int
183usb_phy_init(struct usb_phy *x)
184{
Felipe Balbi410aee72013-06-30 15:27:26 +0300185 if (x && x->init)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530186 return x->init(x);
187
188 return 0;
189}
190
191static inline void
192usb_phy_shutdown(struct usb_phy *x)
193{
Felipe Balbi410aee72013-06-30 15:27:26 +0300194 if (x && x->shutdown)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530195 x->shutdown(x);
196}
197
Felipe Balbib7742122013-03-08 13:22:58 +0200198static inline int
199usb_phy_vbus_on(struct usb_phy *x)
200{
Felipe Balbi410aee72013-06-30 15:27:26 +0300201 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200202 return 0;
203
204 return x->set_vbus(x, true);
205}
206
207static inline int
208usb_phy_vbus_off(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, false);
214}
215
Mayank Ranaa99689a2016-08-10 17:39:47 -0700216static inline int
217usb_phy_reset(struct usb_phy *x)
218{
219 if (x && x->reset)
220 return x->reset(x);
221
222 return 0;
223}
224
Venu Byravarasude4217d2012-09-04 14:25:58 +0530225/* for usb host and peripheral controller drivers */
Felipe Balbiedc7cb22013-03-07 11:13:43 +0200226#if IS_ENABLED(CONFIG_USB_PHY)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530227extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
228extern struct usb_phy *devm_usb_get_phy(struct device *dev,
229 enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530230extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
231extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530232extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
233 const char *phandle, u8 index);
NeilBrowne842b842015-03-23 09:52:48 +1100234extern struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
235 struct device_node *node, struct notifier_block *nb);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530236extern void usb_put_phy(struct usb_phy *);
237extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530238extern int usb_bind_phy(const char *dev_name, u8 index,
239 const char *phy_dev_name);
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530240extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530241#else
242static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
243{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200244 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530245}
246
247static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
248 enum usb_phy_type type)
249{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200250 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530251}
252
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530253static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
254{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200255 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530256}
257
258static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
259{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200260 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530261}
262
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530263static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
264 const char *phandle, u8 index)
265{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200266 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530267}
268
Arnd Bergmann307c8582015-05-28 16:08:02 +0200269static inline struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
270 struct device_node *node, struct notifier_block *nb)
271{
272 return ERR_PTR(-ENXIO);
273}
274
Venu Byravarasude4217d2012-09-04 14:25:58 +0530275static inline void usb_put_phy(struct usb_phy *x)
276{
277}
278
279static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
280{
281}
282
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530283static inline int usb_bind_phy(const char *dev_name, u8 index,
284 const char *phy_dev_name)
285{
286 return -EOPNOTSUPP;
287}
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530288
289static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
290{
291}
Venu Byravarasude4217d2012-09-04 14:25:58 +0530292#endif
293
294static inline int
295usb_phy_set_power(struct usb_phy *x, unsigned mA)
296{
297 if (x && x->set_power)
298 return x->set_power(x, mA);
299 return 0;
300}
301
302/* Context: can sleep */
303static inline int
304usb_phy_set_suspend(struct usb_phy *x, int suspend)
305{
Felipe Balbi410aee72013-06-30 15:27:26 +0300306 if (x && x->set_suspend != NULL)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530307 return x->set_suspend(x, suspend);
308 else
309 return 0;
310}
311
312static inline int
Peter Chen57bf9b02014-02-24 10:21:01 +0800313usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
314{
315 if (x && x->set_wakeup)
316 return x->set_wakeup(x, enabled);
317 else
318 return 0;
319}
320
321static inline int
Peter Chenac965112012-11-09 09:44:44 +0800322usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530323{
Felipe Balbi410aee72013-06-30 15:27:26 +0300324 if (x && x->notify_connect)
Peter Chenac965112012-11-09 09:44:44 +0800325 return x->notify_connect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530326 else
327 return 0;
328}
329
330static inline int
Peter Chenac965112012-11-09 09:44:44 +0800331usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530332{
Felipe Balbi410aee72013-06-30 15:27:26 +0300333 if (x && x->notify_disconnect)
Peter Chenac965112012-11-09 09:44:44 +0800334 return x->notify_disconnect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530335 else
336 return 0;
337}
338
339/* notifiers */
340static inline int
341usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
342{
343 return atomic_notifier_chain_register(&x->notifier, nb);
344}
345
346static inline void
347usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
348{
349 atomic_notifier_chain_unregister(&x->notifier, nb);
350}
351
352static inline const char *usb_phy_type_string(enum usb_phy_type type)
353{
354 switch (type) {
355 case USB_PHY_TYPE_USB2:
356 return "USB2 PHY";
357 case USB_PHY_TYPE_USB3:
358 return "USB3 PHY";
359 default:
360 return "UNKNOWN PHY TYPE";
361 }
362}
363#endif /* __LINUX_USB_PHY_H */