blob: e4f3f21001aa52054aa6cf8b92a7848dddc96865 [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)
Vijayavardhan Vennapusab6cf5ac2019-11-12 11:13:56 +053026#define PHY_SOFT_CONNECT BIT(11)
Mayank Rana83ad5822016-08-09 14:17:22 -070027
Michael Grzeschik1c9af652013-06-13 17:59:55 +030028enum usb_phy_interface {
29 USBPHY_INTERFACE_MODE_UNKNOWN,
30 USBPHY_INTERFACE_MODE_UTMI,
31 USBPHY_INTERFACE_MODE_UTMIW,
32 USBPHY_INTERFACE_MODE_ULPI,
33 USBPHY_INTERFACE_MODE_SERIAL,
34 USBPHY_INTERFACE_MODE_HSIC,
35};
36
Venu Byravarasude4217d2012-09-04 14:25:58 +053037enum usb_phy_events {
38 USB_EVENT_NONE, /* no events or cable disconnected */
39 USB_EVENT_VBUS, /* vbus valid event */
40 USB_EVENT_ID, /* id was grounded */
41 USB_EVENT_CHARGER, /* usb dedicated charger */
42 USB_EVENT_ENUMERATED, /* gadget driver enumerated */
43};
44
45/* associate a type with PHY */
46enum usb_phy_type {
47 USB_PHY_TYPE_UNDEFINED,
48 USB_PHY_TYPE_USB2,
49 USB_PHY_TYPE_USB3,
Hemant Kumarbce97a22017-12-07 16:17:38 -080050 USB_PHY_TYPE_USB3_OR_DP,
51 USB_PHY_TYPE_USB3_AND_DP,
Venu Byravarasude4217d2012-09-04 14:25:58 +053052};
53
Venu Byravarasua4c3dde2012-09-06 10:42:15 +053054/* OTG defines lots of enumeration states before device reset */
55enum usb_otg_state {
56 OTG_STATE_UNDEFINED = 0,
57
58 /* single-role peripheral, and dual-role default-b */
59 OTG_STATE_B_IDLE,
60 OTG_STATE_B_SRP_INIT,
61 OTG_STATE_B_PERIPHERAL,
Mayank Ranaa99689a2016-08-10 17:39:47 -070062 OTG_STATE_B_SUSPEND,
Ajay Agarwal1ef64692018-01-23 19:46:25 +053063 OTG_STATE_B_CHARGER,
Venu Byravarasua4c3dde2012-09-06 10:42:15 +053064
65 /* extra dual-role default-b states */
66 OTG_STATE_B_WAIT_ACON,
67 OTG_STATE_B_HOST,
68
69 /* dual-role default-a */
70 OTG_STATE_A_IDLE,
71 OTG_STATE_A_WAIT_VRISE,
72 OTG_STATE_A_WAIT_BCON,
73 OTG_STATE_A_HOST,
74 OTG_STATE_A_SUSPEND,
75 OTG_STATE_A_PERIPHERAL,
76 OTG_STATE_A_WAIT_VFALL,
77 OTG_STATE_A_VBUS_ERR,
78};
79
Venu Byravarasude4217d2012-09-04 14:25:58 +053080struct usb_phy;
81struct usb_otg;
82
Peter Chen58efd4b02015-09-22 15:31:34 +080083/* for phys connected thru an ULPI interface, the user must
Venu Byravarasude4217d2012-09-04 14:25:58 +053084 * provide access ops
85 */
86struct usb_phy_io_ops {
87 int (*read)(struct usb_phy *x, u32 reg);
88 int (*write)(struct usb_phy *x, u32 val, u32 reg);
89};
90
91struct usb_phy {
92 struct device *dev;
93 const char *label;
94 unsigned int flags;
95
96 enum usb_phy_type type;
Venu Byravarasude4217d2012-09-04 14:25:58 +053097 enum usb_phy_events last_event;
98
99 struct usb_otg *otg;
100
101 struct device *io_dev;
102 struct usb_phy_io_ops *io_ops;
103 void __iomem *io_priv;
104
105 /* for notification of usb_phy_events */
106 struct atomic_notifier_head notifier;
107
108 /* to pass extra port status to the root hub */
109 u16 port_status;
110 u16 port_change;
111
Peter Chen58efd4b02015-09-22 15:31:34 +0800112 /* to support controllers that have multiple phys */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530113 struct list_head head;
114
Peter Chen58efd4b02015-09-22 15:31:34 +0800115 /* initialize/shutdown the phy */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530116 int (*init)(struct usb_phy *x);
117 void (*shutdown)(struct usb_phy *x);
118
Felipe Balbib7742122013-03-08 13:22:58 +0200119 /* enable/disable VBUS */
120 int (*set_vbus)(struct usb_phy *x, int on);
Hemant Kumar2bb3bdf2017-11-22 13:53:08 -0800121 /* callback to indicate port is being reset or reset the port */
122 void (*start_port_reset)(struct usb_phy *x);
Felipe Balbib7742122013-03-08 13:22:58 +0200123
Venu Byravarasude4217d2012-09-04 14:25:58 +0530124 /* effective for B devices, ignored for A-peripheral */
125 int (*set_power)(struct usb_phy *x,
126 unsigned mA);
127
Peter Chen58efd4b02015-09-22 15:31:34 +0800128 /* Set phy into suspend mode */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530129 int (*set_suspend)(struct usb_phy *x,
130 int suspend);
131
Peter Chen57bf9b02014-02-24 10:21:01 +0800132 /*
133 * Set wakeup enable for PHY, in that case, the PHY can be
134 * woken up from suspend status due to external events,
135 * like vbus change, dp/dm change and id.
136 */
137 int (*set_wakeup)(struct usb_phy *x, bool enabled);
138
Venu Byravarasude4217d2012-09-04 14:25:58 +0530139 /* notify phy connect status change */
Peter Chenac965112012-11-09 09:44:44 +0800140 int (*notify_connect)(struct usb_phy *x,
141 enum usb_device_speed speed);
142 int (*notify_disconnect)(struct usb_phy *x,
143 enum usb_device_speed speed);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700144
145 /* reset the PHY clocks */
146 int (*reset)(struct usb_phy *x);
Udipto Goswami52760062020-01-17 12:42:09 +0530147 int (*drive_dp_pulse)(struct usb_phy *x, unsigned int pulse_width);
Ajay Agarwal1ef64692018-01-23 19:46:25 +0530148
149 /* for notification of usb_phy_dbg_events */
150 void (*dbg_event)(struct usb_phy *x,
151 char *event, int msg1, int msg2);
Hemant Kumar91f5e542017-11-20 16:25:46 -0800152 int (*disable_chirp)(struct usb_phy *x, bool disable);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530153};
154
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530155/**
156 * struct usb_phy_bind - represent the binding for the phy
157 * @dev_name: the device name of the device that will bind to the phy
158 * @phy_dev_name: the device name of the phy
159 * @index: used if a single controller uses multiple phys
160 * @phy: reference to the phy
161 * @list: to maintain a linked list of the binding information
162 */
163struct usb_phy_bind {
164 const char *dev_name;
165 const char *phy_dev_name;
166 u8 index;
167 struct usb_phy *phy;
168 struct list_head list;
169};
Venu Byravarasude4217d2012-09-04 14:25:58 +0530170
171/* for board-specific init logic */
172extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530173extern int usb_add_phy_dev(struct usb_phy *);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530174extern void usb_remove_phy(struct usb_phy *);
175
176/* helpers for direct access thru low-level io interface */
177static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
178{
Felipe Balbi410aee72013-06-30 15:27:26 +0300179 if (x && x->io_ops && x->io_ops->read)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530180 return x->io_ops->read(x, reg);
181
182 return -EINVAL;
183}
184
185static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
186{
Felipe Balbi410aee72013-06-30 15:27:26 +0300187 if (x && x->io_ops && x->io_ops->write)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530188 return x->io_ops->write(x, val, reg);
189
190 return -EINVAL;
191}
192
193static inline int
194usb_phy_init(struct usb_phy *x)
195{
Felipe Balbi410aee72013-06-30 15:27:26 +0300196 if (x && x->init)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530197 return x->init(x);
198
199 return 0;
200}
201
202static inline void
203usb_phy_shutdown(struct usb_phy *x)
204{
Felipe Balbi410aee72013-06-30 15:27:26 +0300205 if (x && x->shutdown)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530206 x->shutdown(x);
207}
208
Felipe Balbib7742122013-03-08 13:22:58 +0200209static inline int
210usb_phy_vbus_on(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, true);
216}
217
218static inline int
219usb_phy_vbus_off(struct usb_phy *x)
220{
Felipe Balbi410aee72013-06-30 15:27:26 +0300221 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200222 return 0;
223
224 return x->set_vbus(x, false);
225}
226
Hemant Kumar2bb3bdf2017-11-22 13:53:08 -0800227static inline void
228usb_phy_start_port_reset(struct usb_phy *x)
229{
230 if (!x || !x->start_port_reset)
231 return;
232
233 x->start_port_reset(x);
234}
235
Mayank Ranaa99689a2016-08-10 17:39:47 -0700236static inline int
237usb_phy_reset(struct usb_phy *x)
238{
239 if (x && x->reset)
240 return x->reset(x);
241
242 return 0;
243}
244
Udipto Goswami52760062020-01-17 12:42:09 +0530245static inline int
246usb_phy_drive_dp_pulse(struct usb_phy *x, unsigned int pulse_width)
247{
248 if (x && x->drive_dp_pulse)
249 return x->drive_dp_pulse(x, pulse_width);
250
251 return 0;
252}
253
Venu Byravarasude4217d2012-09-04 14:25:58 +0530254/* for usb host and peripheral controller drivers */
Felipe Balbiedc7cb22013-03-07 11:13:43 +0200255#if IS_ENABLED(CONFIG_USB_PHY)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530256extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
257extern struct usb_phy *devm_usb_get_phy(struct device *dev,
258 enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530259extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
260extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530261extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
262 const char *phandle, u8 index);
NeilBrowne842b842015-03-23 09:52:48 +1100263extern struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
264 struct device_node *node, struct notifier_block *nb);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530265extern void usb_put_phy(struct usb_phy *);
266extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530267extern int usb_bind_phy(const char *dev_name, u8 index,
268 const char *phy_dev_name);
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530269extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530270#else
271static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
272{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200273 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530274}
275
276static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
277 enum usb_phy_type type)
278{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200279 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530280}
281
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530282static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
283{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200284 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530285}
286
287static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
288{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200289 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530290}
291
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530292static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
293 const char *phandle, u8 index)
294{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200295 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530296}
297
Arnd Bergmann307c8582015-05-28 16:08:02 +0200298static inline struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
299 struct device_node *node, struct notifier_block *nb)
300{
301 return ERR_PTR(-ENXIO);
302}
303
Venu Byravarasude4217d2012-09-04 14:25:58 +0530304static inline void usb_put_phy(struct usb_phy *x)
305{
306}
307
308static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
309{
310}
311
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530312static inline int usb_bind_phy(const char *dev_name, u8 index,
313 const char *phy_dev_name)
314{
315 return -EOPNOTSUPP;
316}
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530317
318static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
319{
320}
Venu Byravarasude4217d2012-09-04 14:25:58 +0530321#endif
322
323static inline int
324usb_phy_set_power(struct usb_phy *x, unsigned mA)
325{
326 if (x && x->set_power)
327 return x->set_power(x, mA);
328 return 0;
329}
330
331/* Context: can sleep */
332static inline int
333usb_phy_set_suspend(struct usb_phy *x, int suspend)
334{
Felipe Balbi410aee72013-06-30 15:27:26 +0300335 if (x && x->set_suspend != NULL)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530336 return x->set_suspend(x, suspend);
337 else
338 return 0;
339}
340
341static inline int
Peter Chen57bf9b02014-02-24 10:21:01 +0800342usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
343{
344 if (x && x->set_wakeup)
345 return x->set_wakeup(x, enabled);
346 else
347 return 0;
348}
349
350static inline int
Peter Chenac965112012-11-09 09:44:44 +0800351usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530352{
Felipe Balbi410aee72013-06-30 15:27:26 +0300353 if (x && x->notify_connect)
Peter Chenac965112012-11-09 09:44:44 +0800354 return x->notify_connect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530355 else
356 return 0;
357}
358
359static inline int
Peter Chenac965112012-11-09 09:44:44 +0800360usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530361{
Felipe Balbi410aee72013-06-30 15:27:26 +0300362 if (x && x->notify_disconnect)
Peter Chenac965112012-11-09 09:44:44 +0800363 return x->notify_disconnect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530364 else
365 return 0;
366}
367
368/* notifiers */
369static inline int
370usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
371{
372 return atomic_notifier_chain_register(&x->notifier, nb);
373}
374
375static inline void
376usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
377{
378 atomic_notifier_chain_unregister(&x->notifier, nb);
379}
380
381static inline const char *usb_phy_type_string(enum usb_phy_type type)
382{
383 switch (type) {
384 case USB_PHY_TYPE_USB2:
385 return "USB2 PHY";
386 case USB_PHY_TYPE_USB3:
387 return "USB3 PHY";
388 default:
389 return "UNKNOWN PHY TYPE";
390 }
391}
392#endif /* __LINUX_USB_PHY_H */