blob: c724747dd77a3c42ff94f41d1a25d11ebe311bc6 [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);
Ajay Agarwal1ef64692018-01-23 19:46:25 +0530147
148 /* for notification of usb_phy_dbg_events */
149 void (*dbg_event)(struct usb_phy *x,
150 char *event, int msg1, int msg2);
Hemant Kumar91f5e542017-11-20 16:25:46 -0800151 int (*disable_chirp)(struct usb_phy *x, bool disable);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530152};
153
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530154/**
155 * struct usb_phy_bind - represent the binding for the phy
156 * @dev_name: the device name of the device that will bind to the phy
157 * @phy_dev_name: the device name of the phy
158 * @index: used if a single controller uses multiple phys
159 * @phy: reference to the phy
160 * @list: to maintain a linked list of the binding information
161 */
162struct usb_phy_bind {
163 const char *dev_name;
164 const char *phy_dev_name;
165 u8 index;
166 struct usb_phy *phy;
167 struct list_head list;
168};
Venu Byravarasude4217d2012-09-04 14:25:58 +0530169
170/* for board-specific init logic */
171extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530172extern int usb_add_phy_dev(struct usb_phy *);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530173extern void usb_remove_phy(struct usb_phy *);
174
175/* helpers for direct access thru low-level io interface */
176static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
177{
Felipe Balbi410aee72013-06-30 15:27:26 +0300178 if (x && x->io_ops && x->io_ops->read)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530179 return x->io_ops->read(x, reg);
180
181 return -EINVAL;
182}
183
184static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
185{
Felipe Balbi410aee72013-06-30 15:27:26 +0300186 if (x && x->io_ops && x->io_ops->write)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530187 return x->io_ops->write(x, val, reg);
188
189 return -EINVAL;
190}
191
192static inline int
193usb_phy_init(struct usb_phy *x)
194{
Felipe Balbi410aee72013-06-30 15:27:26 +0300195 if (x && x->init)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530196 return x->init(x);
197
198 return 0;
199}
200
201static inline void
202usb_phy_shutdown(struct usb_phy *x)
203{
Felipe Balbi410aee72013-06-30 15:27:26 +0300204 if (x && x->shutdown)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530205 x->shutdown(x);
206}
207
Felipe Balbib7742122013-03-08 13:22:58 +0200208static inline int
209usb_phy_vbus_on(struct usb_phy *x)
210{
Felipe Balbi410aee72013-06-30 15:27:26 +0300211 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200212 return 0;
213
214 return x->set_vbus(x, true);
215}
216
217static inline int
218usb_phy_vbus_off(struct usb_phy *x)
219{
Felipe Balbi410aee72013-06-30 15:27:26 +0300220 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200221 return 0;
222
223 return x->set_vbus(x, false);
224}
225
Hemant Kumar2bb3bdf2017-11-22 13:53:08 -0800226static inline void
227usb_phy_start_port_reset(struct usb_phy *x)
228{
229 if (!x || !x->start_port_reset)
230 return;
231
232 x->start_port_reset(x);
233}
234
Mayank Ranaa99689a2016-08-10 17:39:47 -0700235static inline int
236usb_phy_reset(struct usb_phy *x)
237{
238 if (x && x->reset)
239 return x->reset(x);
240
241 return 0;
242}
243
Venu Byravarasude4217d2012-09-04 14:25:58 +0530244/* for usb host and peripheral controller drivers */
Felipe Balbiedc7cb22013-03-07 11:13:43 +0200245#if IS_ENABLED(CONFIG_USB_PHY)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530246extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
247extern struct usb_phy *devm_usb_get_phy(struct device *dev,
248 enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530249extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
250extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530251extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
252 const char *phandle, u8 index);
NeilBrowne842b842015-03-23 09:52:48 +1100253extern struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
254 struct device_node *node, struct notifier_block *nb);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530255extern void usb_put_phy(struct usb_phy *);
256extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530257extern int usb_bind_phy(const char *dev_name, u8 index,
258 const char *phy_dev_name);
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530259extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530260#else
261static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
262{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200263 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530264}
265
266static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
267 enum usb_phy_type type)
268{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200269 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530270}
271
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530272static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
273{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200274 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530275}
276
277static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
278{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200279 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530280}
281
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530282static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
283 const char *phandle, u8 index)
284{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200285 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530286}
287
Arnd Bergmann307c8582015-05-28 16:08:02 +0200288static inline struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
289 struct device_node *node, struct notifier_block *nb)
290{
291 return ERR_PTR(-ENXIO);
292}
293
Venu Byravarasude4217d2012-09-04 14:25:58 +0530294static inline void usb_put_phy(struct usb_phy *x)
295{
296}
297
298static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
299{
300}
301
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530302static inline int usb_bind_phy(const char *dev_name, u8 index,
303 const char *phy_dev_name)
304{
305 return -EOPNOTSUPP;
306}
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530307
308static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
309{
310}
Venu Byravarasude4217d2012-09-04 14:25:58 +0530311#endif
312
313static inline int
314usb_phy_set_power(struct usb_phy *x, unsigned mA)
315{
316 if (x && x->set_power)
317 return x->set_power(x, mA);
318 return 0;
319}
320
321/* Context: can sleep */
322static inline int
323usb_phy_set_suspend(struct usb_phy *x, int suspend)
324{
Felipe Balbi410aee72013-06-30 15:27:26 +0300325 if (x && x->set_suspend != NULL)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530326 return x->set_suspend(x, suspend);
327 else
328 return 0;
329}
330
331static inline int
Peter Chen57bf9b02014-02-24 10:21:01 +0800332usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
333{
334 if (x && x->set_wakeup)
335 return x->set_wakeup(x, enabled);
336 else
337 return 0;
338}
339
340static inline int
Peter Chenac965112012-11-09 09:44:44 +0800341usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530342{
Felipe Balbi410aee72013-06-30 15:27:26 +0300343 if (x && x->notify_connect)
Peter Chenac965112012-11-09 09:44:44 +0800344 return x->notify_connect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530345 else
346 return 0;
347}
348
349static inline int
Peter Chenac965112012-11-09 09:44:44 +0800350usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530351{
Felipe Balbi410aee72013-06-30 15:27:26 +0300352 if (x && x->notify_disconnect)
Peter Chenac965112012-11-09 09:44:44 +0800353 return x->notify_disconnect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530354 else
355 return 0;
356}
357
358/* notifiers */
359static inline int
360usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
361{
362 return atomic_notifier_chain_register(&x->notifier, nb);
363}
364
365static inline void
366usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
367{
368 atomic_notifier_chain_unregister(&x->notifier, nb);
369}
370
371static inline const char *usb_phy_type_string(enum usb_phy_type type)
372{
373 switch (type) {
374 case USB_PHY_TYPE_USB2:
375 return "USB2 PHY";
376 case USB_PHY_TYPE_USB3:
377 return "USB3 PHY";
378 default:
379 return "UNKNOWN PHY TYPE";
380 }
381}
382#endif /* __LINUX_USB_PHY_H */