blob: 742ef8d6215c28b9a37ebdb3da1ee9f7f203db7d [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);
Hemant Kumar2bb3bdf2017-11-22 13:53:08 -0800117 /* callback to indicate port is being reset or reset the port */
118 void (*start_port_reset)(struct usb_phy *x);
Felipe Balbib7742122013-03-08 13:22:58 +0200119
Venu Byravarasude4217d2012-09-04 14:25:58 +0530120 /* effective for B devices, ignored for A-peripheral */
121 int (*set_power)(struct usb_phy *x,
122 unsigned mA);
123
Peter Chen58efd4b02015-09-22 15:31:34 +0800124 /* Set phy into suspend mode */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530125 int (*set_suspend)(struct usb_phy *x,
126 int suspend);
127
Peter Chen57bf9b02014-02-24 10:21:01 +0800128 /*
129 * Set wakeup enable for PHY, in that case, the PHY can be
130 * woken up from suspend status due to external events,
131 * like vbus change, dp/dm change and id.
132 */
133 int (*set_wakeup)(struct usb_phy *x, bool enabled);
134
Venu Byravarasude4217d2012-09-04 14:25:58 +0530135 /* notify phy connect status change */
Peter Chenac965112012-11-09 09:44:44 +0800136 int (*notify_connect)(struct usb_phy *x,
137 enum usb_device_speed speed);
138 int (*notify_disconnect)(struct usb_phy *x,
139 enum usb_device_speed speed);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700140
141 /* reset the PHY clocks */
142 int (*reset)(struct usb_phy *x);
Hemant Kumar91f5e542017-11-20 16:25:46 -0800143 int (*disable_chirp)(struct usb_phy *x, bool disable);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530144};
145
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530146/**
147 * struct usb_phy_bind - represent the binding for the phy
148 * @dev_name: the device name of the device that will bind to the phy
149 * @phy_dev_name: the device name of the phy
150 * @index: used if a single controller uses multiple phys
151 * @phy: reference to the phy
152 * @list: to maintain a linked list of the binding information
153 */
154struct usb_phy_bind {
155 const char *dev_name;
156 const char *phy_dev_name;
157 u8 index;
158 struct usb_phy *phy;
159 struct list_head list;
160};
Venu Byravarasude4217d2012-09-04 14:25:58 +0530161
162/* for board-specific init logic */
163extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530164extern int usb_add_phy_dev(struct usb_phy *);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530165extern void usb_remove_phy(struct usb_phy *);
166
167/* helpers for direct access thru low-level io interface */
168static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
169{
Felipe Balbi410aee72013-06-30 15:27:26 +0300170 if (x && x->io_ops && x->io_ops->read)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530171 return x->io_ops->read(x, reg);
172
173 return -EINVAL;
174}
175
176static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
177{
Felipe Balbi410aee72013-06-30 15:27:26 +0300178 if (x && x->io_ops && x->io_ops->write)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530179 return x->io_ops->write(x, val, reg);
180
181 return -EINVAL;
182}
183
184static inline int
185usb_phy_init(struct usb_phy *x)
186{
Felipe Balbi410aee72013-06-30 15:27:26 +0300187 if (x && x->init)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530188 return x->init(x);
189
190 return 0;
191}
192
193static inline void
194usb_phy_shutdown(struct usb_phy *x)
195{
Felipe Balbi410aee72013-06-30 15:27:26 +0300196 if (x && x->shutdown)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530197 x->shutdown(x);
198}
199
Felipe Balbib7742122013-03-08 13:22:58 +0200200static inline int
201usb_phy_vbus_on(struct usb_phy *x)
202{
Felipe Balbi410aee72013-06-30 15:27:26 +0300203 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200204 return 0;
205
206 return x->set_vbus(x, true);
207}
208
209static inline int
210usb_phy_vbus_off(struct usb_phy *x)
211{
Felipe Balbi410aee72013-06-30 15:27:26 +0300212 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200213 return 0;
214
215 return x->set_vbus(x, false);
216}
217
Hemant Kumar2bb3bdf2017-11-22 13:53:08 -0800218static inline void
219usb_phy_start_port_reset(struct usb_phy *x)
220{
221 if (!x || !x->start_port_reset)
222 return;
223
224 x->start_port_reset(x);
225}
226
Mayank Ranaa99689a2016-08-10 17:39:47 -0700227static inline int
228usb_phy_reset(struct usb_phy *x)
229{
230 if (x && x->reset)
231 return x->reset(x);
232
233 return 0;
234}
235
Venu Byravarasude4217d2012-09-04 14:25:58 +0530236/* for usb host and peripheral controller drivers */
Felipe Balbiedc7cb22013-03-07 11:13:43 +0200237#if IS_ENABLED(CONFIG_USB_PHY)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530238extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
239extern struct usb_phy *devm_usb_get_phy(struct device *dev,
240 enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530241extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
242extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530243extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
244 const char *phandle, u8 index);
NeilBrowne842b842015-03-23 09:52:48 +1100245extern struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
246 struct device_node *node, struct notifier_block *nb);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530247extern void usb_put_phy(struct usb_phy *);
248extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530249extern int usb_bind_phy(const char *dev_name, u8 index,
250 const char *phy_dev_name);
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530251extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530252#else
253static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
254{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200255 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530256}
257
258static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
259 enum usb_phy_type type)
260{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200261 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530262}
263
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530264static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
265{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200266 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530267}
268
269static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
270{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200271 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530272}
273
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530274static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
275 const char *phandle, u8 index)
276{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200277 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530278}
279
Arnd Bergmann307c8582015-05-28 16:08:02 +0200280static inline struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
281 struct device_node *node, struct notifier_block *nb)
282{
283 return ERR_PTR(-ENXIO);
284}
285
Venu Byravarasude4217d2012-09-04 14:25:58 +0530286static inline void usb_put_phy(struct usb_phy *x)
287{
288}
289
290static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
291{
292}
293
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530294static inline int usb_bind_phy(const char *dev_name, u8 index,
295 const char *phy_dev_name)
296{
297 return -EOPNOTSUPP;
298}
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530299
300static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
301{
302}
Venu Byravarasude4217d2012-09-04 14:25:58 +0530303#endif
304
305static inline int
306usb_phy_set_power(struct usb_phy *x, unsigned mA)
307{
308 if (x && x->set_power)
309 return x->set_power(x, mA);
310 return 0;
311}
312
313/* Context: can sleep */
314static inline int
315usb_phy_set_suspend(struct usb_phy *x, int suspend)
316{
Felipe Balbi410aee72013-06-30 15:27:26 +0300317 if (x && x->set_suspend != NULL)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530318 return x->set_suspend(x, suspend);
319 else
320 return 0;
321}
322
323static inline int
Peter Chen57bf9b02014-02-24 10:21:01 +0800324usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
325{
326 if (x && x->set_wakeup)
327 return x->set_wakeup(x, enabled);
328 else
329 return 0;
330}
331
332static inline int
Peter Chenac965112012-11-09 09:44:44 +0800333usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530334{
Felipe Balbi410aee72013-06-30 15:27:26 +0300335 if (x && x->notify_connect)
Peter Chenac965112012-11-09 09:44:44 +0800336 return x->notify_connect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530337 else
338 return 0;
339}
340
341static inline int
Peter Chenac965112012-11-09 09:44:44 +0800342usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530343{
Felipe Balbi410aee72013-06-30 15:27:26 +0300344 if (x && x->notify_disconnect)
Peter Chenac965112012-11-09 09:44:44 +0800345 return x->notify_disconnect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530346 else
347 return 0;
348}
349
350/* notifiers */
351static inline int
352usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
353{
354 return atomic_notifier_chain_register(&x->notifier, nb);
355}
356
357static inline void
358usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
359{
360 atomic_notifier_chain_unregister(&x->notifier, nb);
361}
362
363static inline const char *usb_phy_type_string(enum usb_phy_type type)
364{
365 switch (type) {
366 case USB_PHY_TYPE_USB2:
367 return "USB2 PHY";
368 case USB_PHY_TYPE_USB3:
369 return "USB3 PHY";
370 default:
371 return "UNKNOWN PHY TYPE";
372 }
373}
374#endif /* __LINUX_USB_PHY_H */