blob: ffb63937044e872746473253e3d9e0f4057604f4 [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);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530141};
142
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530143/**
144 * struct usb_phy_bind - represent the binding for the phy
145 * @dev_name: the device name of the device that will bind to the phy
146 * @phy_dev_name: the device name of the phy
147 * @index: used if a single controller uses multiple phys
148 * @phy: reference to the phy
149 * @list: to maintain a linked list of the binding information
150 */
151struct usb_phy_bind {
152 const char *dev_name;
153 const char *phy_dev_name;
154 u8 index;
155 struct usb_phy *phy;
156 struct list_head list;
157};
Venu Byravarasude4217d2012-09-04 14:25:58 +0530158
159/* for board-specific init logic */
160extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530161extern int usb_add_phy_dev(struct usb_phy *);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530162extern void usb_remove_phy(struct usb_phy *);
163
164/* helpers for direct access thru low-level io interface */
165static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
166{
Felipe Balbi410aee72013-06-30 15:27:26 +0300167 if (x && x->io_ops && x->io_ops->read)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530168 return x->io_ops->read(x, reg);
169
170 return -EINVAL;
171}
172
173static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
174{
Felipe Balbi410aee72013-06-30 15:27:26 +0300175 if (x && x->io_ops && x->io_ops->write)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530176 return x->io_ops->write(x, val, reg);
177
178 return -EINVAL;
179}
180
181static inline int
182usb_phy_init(struct usb_phy *x)
183{
Felipe Balbi410aee72013-06-30 15:27:26 +0300184 if (x && x->init)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530185 return x->init(x);
186
187 return 0;
188}
189
190static inline void
191usb_phy_shutdown(struct usb_phy *x)
192{
Felipe Balbi410aee72013-06-30 15:27:26 +0300193 if (x && x->shutdown)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530194 x->shutdown(x);
195}
196
Felipe Balbib7742122013-03-08 13:22:58 +0200197static inline int
198usb_phy_vbus_on(struct usb_phy *x)
199{
Felipe Balbi410aee72013-06-30 15:27:26 +0300200 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200201 return 0;
202
203 return x->set_vbus(x, true);
204}
205
206static inline int
207usb_phy_vbus_off(struct usb_phy *x)
208{
Felipe Balbi410aee72013-06-30 15:27:26 +0300209 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200210 return 0;
211
212 return x->set_vbus(x, false);
213}
214
Mayank Ranaa99689a2016-08-10 17:39:47 -0700215static inline int
216usb_phy_reset(struct usb_phy *x)
217{
218 if (x && x->reset)
219 return x->reset(x);
220
221 return 0;
222}
223
Venu Byravarasude4217d2012-09-04 14:25:58 +0530224/* for usb host and peripheral controller drivers */
Felipe Balbiedc7cb22013-03-07 11:13:43 +0200225#if IS_ENABLED(CONFIG_USB_PHY)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530226extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
227extern struct usb_phy *devm_usb_get_phy(struct device *dev,
228 enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530229extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
230extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530231extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
232 const char *phandle, u8 index);
NeilBrowne842b842015-03-23 09:52:48 +1100233extern struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
234 struct device_node *node, struct notifier_block *nb);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530235extern void usb_put_phy(struct usb_phy *);
236extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530237extern int usb_bind_phy(const char *dev_name, u8 index,
238 const char *phy_dev_name);
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530239extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530240#else
241static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
242{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200243 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530244}
245
246static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
247 enum usb_phy_type type)
248{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200249 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530250}
251
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530252static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
253{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200254 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530255}
256
257static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
258{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200259 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530260}
261
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530262static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
263 const char *phandle, u8 index)
264{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200265 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530266}
267
Arnd Bergmann307c8582015-05-28 16:08:02 +0200268static inline struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
269 struct device_node *node, struct notifier_block *nb)
270{
271 return ERR_PTR(-ENXIO);
272}
273
Venu Byravarasude4217d2012-09-04 14:25:58 +0530274static inline void usb_put_phy(struct usb_phy *x)
275{
276}
277
278static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
279{
280}
281
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530282static inline int usb_bind_phy(const char *dev_name, u8 index,
283 const char *phy_dev_name)
284{
285 return -EOPNOTSUPP;
286}
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530287
288static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
289{
290}
Venu Byravarasude4217d2012-09-04 14:25:58 +0530291#endif
292
293static inline int
294usb_phy_set_power(struct usb_phy *x, unsigned mA)
295{
296 if (x && x->set_power)
297 return x->set_power(x, mA);
298 return 0;
299}
300
301/* Context: can sleep */
302static inline int
303usb_phy_set_suspend(struct usb_phy *x, int suspend)
304{
Felipe Balbi410aee72013-06-30 15:27:26 +0300305 if (x && x->set_suspend != NULL)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530306 return x->set_suspend(x, suspend);
307 else
308 return 0;
309}
310
311static inline int
Peter Chen57bf9b02014-02-24 10:21:01 +0800312usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
313{
314 if (x && x->set_wakeup)
315 return x->set_wakeup(x, enabled);
316 else
317 return 0;
318}
319
320static inline int
Peter Chenac965112012-11-09 09:44:44 +0800321usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530322{
Felipe Balbi410aee72013-06-30 15:27:26 +0300323 if (x && x->notify_connect)
Peter Chenac965112012-11-09 09:44:44 +0800324 return x->notify_connect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530325 else
326 return 0;
327}
328
329static inline int
Peter Chenac965112012-11-09 09:44:44 +0800330usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530331{
Felipe Balbi410aee72013-06-30 15:27:26 +0300332 if (x && x->notify_disconnect)
Peter Chenac965112012-11-09 09:44:44 +0800333 return x->notify_disconnect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530334 else
335 return 0;
336}
337
338/* notifiers */
339static inline int
340usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
341{
342 return atomic_notifier_chain_register(&x->notifier, nb);
343}
344
345static inline void
346usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
347{
348 atomic_notifier_chain_unregister(&x->notifier, nb);
349}
350
351static inline const char *usb_phy_type_string(enum usb_phy_type type)
352{
353 switch (type) {
354 case USB_PHY_TYPE_USB2:
355 return "USB2 PHY";
356 case USB_PHY_TYPE_USB3:
357 return "USB3 PHY";
358 default:
359 return "UNKNOWN PHY TYPE";
360 }
361}
362#endif /* __LINUX_USB_PHY_H */