blob: 64aa52e0969180e51a8d4564aa6c32806ebb845d [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,
Hemant Kumarbce97a22017-12-07 16:17:38 -080048 USB_PHY_TYPE_USB3_OR_DP,
49 USB_PHY_TYPE_USB3_AND_DP,
Venu Byravarasude4217d2012-09-04 14:25:58 +053050};
51
Venu Byravarasua4c3dde2012-09-06 10:42:15 +053052/* OTG defines lots of enumeration states before device reset */
53enum usb_otg_state {
54 OTG_STATE_UNDEFINED = 0,
55
56 /* single-role peripheral, and dual-role default-b */
57 OTG_STATE_B_IDLE,
58 OTG_STATE_B_SRP_INIT,
59 OTG_STATE_B_PERIPHERAL,
Mayank Ranaa99689a2016-08-10 17:39:47 -070060 OTG_STATE_B_SUSPEND,
Venu Byravarasua4c3dde2012-09-06 10:42:15 +053061
62 /* extra dual-role default-b states */
63 OTG_STATE_B_WAIT_ACON,
64 OTG_STATE_B_HOST,
65
66 /* dual-role default-a */
67 OTG_STATE_A_IDLE,
68 OTG_STATE_A_WAIT_VRISE,
69 OTG_STATE_A_WAIT_BCON,
70 OTG_STATE_A_HOST,
71 OTG_STATE_A_SUSPEND,
72 OTG_STATE_A_PERIPHERAL,
73 OTG_STATE_A_WAIT_VFALL,
74 OTG_STATE_A_VBUS_ERR,
75};
76
Venu Byravarasude4217d2012-09-04 14:25:58 +053077struct usb_phy;
78struct usb_otg;
79
Peter Chen58efd4b02015-09-22 15:31:34 +080080/* for phys connected thru an ULPI interface, the user must
Venu Byravarasude4217d2012-09-04 14:25:58 +053081 * provide access ops
82 */
83struct usb_phy_io_ops {
84 int (*read)(struct usb_phy *x, u32 reg);
85 int (*write)(struct usb_phy *x, u32 val, u32 reg);
86};
87
88struct usb_phy {
89 struct device *dev;
90 const char *label;
91 unsigned int flags;
92
93 enum usb_phy_type type;
Venu Byravarasude4217d2012-09-04 14:25:58 +053094 enum usb_phy_events last_event;
95
96 struct usb_otg *otg;
97
98 struct device *io_dev;
99 struct usb_phy_io_ops *io_ops;
100 void __iomem *io_priv;
101
102 /* for notification of usb_phy_events */
103 struct atomic_notifier_head notifier;
104
105 /* to pass extra port status to the root hub */
106 u16 port_status;
107 u16 port_change;
108
Peter Chen58efd4b02015-09-22 15:31:34 +0800109 /* to support controllers that have multiple phys */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530110 struct list_head head;
111
Peter Chen58efd4b02015-09-22 15:31:34 +0800112 /* initialize/shutdown the phy */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530113 int (*init)(struct usb_phy *x);
114 void (*shutdown)(struct usb_phy *x);
115
Felipe Balbib7742122013-03-08 13:22:58 +0200116 /* enable/disable VBUS */
117 int (*set_vbus)(struct usb_phy *x, int on);
Hemant Kumar2bb3bdf2017-11-22 13:53:08 -0800118 /* callback to indicate port is being reset or reset the port */
119 void (*start_port_reset)(struct usb_phy *x);
Felipe Balbib7742122013-03-08 13:22:58 +0200120
Venu Byravarasude4217d2012-09-04 14:25:58 +0530121 /* effective for B devices, ignored for A-peripheral */
122 int (*set_power)(struct usb_phy *x,
123 unsigned mA);
124
Peter Chen58efd4b02015-09-22 15:31:34 +0800125 /* Set phy into suspend mode */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530126 int (*set_suspend)(struct usb_phy *x,
127 int suspend);
128
Peter Chen57bf9b02014-02-24 10:21:01 +0800129 /*
130 * Set wakeup enable for PHY, in that case, the PHY can be
131 * woken up from suspend status due to external events,
132 * like vbus change, dp/dm change and id.
133 */
134 int (*set_wakeup)(struct usb_phy *x, bool enabled);
135
Venu Byravarasude4217d2012-09-04 14:25:58 +0530136 /* notify phy connect status change */
Peter Chenac965112012-11-09 09:44:44 +0800137 int (*notify_connect)(struct usb_phy *x,
138 enum usb_device_speed speed);
139 int (*notify_disconnect)(struct usb_phy *x,
140 enum usb_device_speed speed);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700141
142 /* reset the PHY clocks */
143 int (*reset)(struct usb_phy *x);
Hemant Kumar91f5e542017-11-20 16:25:46 -0800144 int (*disable_chirp)(struct usb_phy *x, bool disable);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530145};
146
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530147/**
148 * struct usb_phy_bind - represent the binding for the phy
149 * @dev_name: the device name of the device that will bind to the phy
150 * @phy_dev_name: the device name of the phy
151 * @index: used if a single controller uses multiple phys
152 * @phy: reference to the phy
153 * @list: to maintain a linked list of the binding information
154 */
155struct usb_phy_bind {
156 const char *dev_name;
157 const char *phy_dev_name;
158 u8 index;
159 struct usb_phy *phy;
160 struct list_head list;
161};
Venu Byravarasude4217d2012-09-04 14:25:58 +0530162
163/* for board-specific init logic */
164extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530165extern int usb_add_phy_dev(struct usb_phy *);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530166extern void usb_remove_phy(struct usb_phy *);
167
168/* helpers for direct access thru low-level io interface */
169static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
170{
Felipe Balbi410aee72013-06-30 15:27:26 +0300171 if (x && x->io_ops && x->io_ops->read)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530172 return x->io_ops->read(x, reg);
173
174 return -EINVAL;
175}
176
177static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
178{
Felipe Balbi410aee72013-06-30 15:27:26 +0300179 if (x && x->io_ops && x->io_ops->write)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530180 return x->io_ops->write(x, val, reg);
181
182 return -EINVAL;
183}
184
185static inline int
186usb_phy_init(struct usb_phy *x)
187{
Felipe Balbi410aee72013-06-30 15:27:26 +0300188 if (x && x->init)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530189 return x->init(x);
190
191 return 0;
192}
193
194static inline void
195usb_phy_shutdown(struct usb_phy *x)
196{
Felipe Balbi410aee72013-06-30 15:27:26 +0300197 if (x && x->shutdown)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530198 x->shutdown(x);
199}
200
Felipe Balbib7742122013-03-08 13:22:58 +0200201static inline int
202usb_phy_vbus_on(struct usb_phy *x)
203{
Felipe Balbi410aee72013-06-30 15:27:26 +0300204 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200205 return 0;
206
207 return x->set_vbus(x, true);
208}
209
210static inline int
211usb_phy_vbus_off(struct usb_phy *x)
212{
Felipe Balbi410aee72013-06-30 15:27:26 +0300213 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200214 return 0;
215
216 return x->set_vbus(x, false);
217}
218
Hemant Kumar2bb3bdf2017-11-22 13:53:08 -0800219static inline void
220usb_phy_start_port_reset(struct usb_phy *x)
221{
222 if (!x || !x->start_port_reset)
223 return;
224
225 x->start_port_reset(x);
226}
227
Mayank Ranaa99689a2016-08-10 17:39:47 -0700228static inline int
229usb_phy_reset(struct usb_phy *x)
230{
231 if (x && x->reset)
232 return x->reset(x);
233
234 return 0;
235}
236
Venu Byravarasude4217d2012-09-04 14:25:58 +0530237/* for usb host and peripheral controller drivers */
Felipe Balbiedc7cb22013-03-07 11:13:43 +0200238#if IS_ENABLED(CONFIG_USB_PHY)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530239extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
240extern struct usb_phy *devm_usb_get_phy(struct device *dev,
241 enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530242extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
243extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530244extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
245 const char *phandle, u8 index);
NeilBrowne842b842015-03-23 09:52:48 +1100246extern struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
247 struct device_node *node, struct notifier_block *nb);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530248extern void usb_put_phy(struct usb_phy *);
249extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530250extern int usb_bind_phy(const char *dev_name, u8 index,
251 const char *phy_dev_name);
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530252extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530253#else
254static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
255{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200256 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530257}
258
259static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
260 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
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530265static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
266{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200267 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530268}
269
270static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
271{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200272 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530273}
274
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530275static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
276 const char *phandle, u8 index)
277{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200278 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530279}
280
Arnd Bergmann307c8582015-05-28 16:08:02 +0200281static inline struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
282 struct device_node *node, struct notifier_block *nb)
283{
284 return ERR_PTR(-ENXIO);
285}
286
Venu Byravarasude4217d2012-09-04 14:25:58 +0530287static inline void usb_put_phy(struct usb_phy *x)
288{
289}
290
291static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
292{
293}
294
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530295static inline int usb_bind_phy(const char *dev_name, u8 index,
296 const char *phy_dev_name)
297{
298 return -EOPNOTSUPP;
299}
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530300
301static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
302{
303}
Venu Byravarasude4217d2012-09-04 14:25:58 +0530304#endif
305
306static inline int
307usb_phy_set_power(struct usb_phy *x, unsigned mA)
308{
309 if (x && x->set_power)
310 return x->set_power(x, mA);
311 return 0;
312}
313
314/* Context: can sleep */
315static inline int
316usb_phy_set_suspend(struct usb_phy *x, int suspend)
317{
Felipe Balbi410aee72013-06-30 15:27:26 +0300318 if (x && x->set_suspend != NULL)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530319 return x->set_suspend(x, suspend);
320 else
321 return 0;
322}
323
324static inline int
Peter Chen57bf9b02014-02-24 10:21:01 +0800325usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
326{
327 if (x && x->set_wakeup)
328 return x->set_wakeup(x, enabled);
329 else
330 return 0;
331}
332
333static inline int
Peter Chenac965112012-11-09 09:44:44 +0800334usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530335{
Felipe Balbi410aee72013-06-30 15:27:26 +0300336 if (x && x->notify_connect)
Peter Chenac965112012-11-09 09:44:44 +0800337 return x->notify_connect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530338 else
339 return 0;
340}
341
342static inline int
Peter Chenac965112012-11-09 09:44:44 +0800343usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530344{
Felipe Balbi410aee72013-06-30 15:27:26 +0300345 if (x && x->notify_disconnect)
Peter Chenac965112012-11-09 09:44:44 +0800346 return x->notify_disconnect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530347 else
348 return 0;
349}
350
351/* notifiers */
352static inline int
353usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
354{
355 return atomic_notifier_chain_register(&x->notifier, nb);
356}
357
358static inline void
359usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
360{
361 atomic_notifier_chain_unregister(&x->notifier, nb);
362}
363
364static inline const char *usb_phy_type_string(enum usb_phy_type type)
365{
366 switch (type) {
367 case USB_PHY_TYPE_USB2:
368 return "USB2 PHY";
369 case USB_PHY_TYPE_USB3:
370 return "USB3 PHY";
371 default:
372 return "UNKNOWN PHY TYPE";
373 }
374}
375#endif /* __LINUX_USB_PHY_H */