blob: 299245105610f271aa68399508a0486aca595db7 [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
Baolin Wang7d21114d2017-05-05 14:12:24 +080012#include <linux/extcon.h>
Venu Byravarasude4217d2012-09-04 14:25:58 +053013#include <linux/notifier.h>
Peter Chenac965112012-11-09 09:44:44 +080014#include <linux/usb.h>
Venu Byravarasude4217d2012-09-04 14:25:58 +053015
Michael Grzeschik1c9af652013-06-13 17:59:55 +030016enum usb_phy_interface {
17 USBPHY_INTERFACE_MODE_UNKNOWN,
18 USBPHY_INTERFACE_MODE_UTMI,
19 USBPHY_INTERFACE_MODE_UTMIW,
20 USBPHY_INTERFACE_MODE_ULPI,
21 USBPHY_INTERFACE_MODE_SERIAL,
22 USBPHY_INTERFACE_MODE_HSIC,
23};
24
Venu Byravarasude4217d2012-09-04 14:25:58 +053025enum usb_phy_events {
26 USB_EVENT_NONE, /* no events or cable disconnected */
27 USB_EVENT_VBUS, /* vbus valid event */
28 USB_EVENT_ID, /* id was grounded */
29 USB_EVENT_CHARGER, /* usb dedicated charger */
30 USB_EVENT_ENUMERATED, /* gadget driver enumerated */
31};
32
33/* associate a type with PHY */
34enum usb_phy_type {
35 USB_PHY_TYPE_UNDEFINED,
36 USB_PHY_TYPE_USB2,
37 USB_PHY_TYPE_USB3,
38};
39
Venu Byravarasua4c3dde2012-09-06 10:42:15 +053040/* OTG defines lots of enumeration states before device reset */
41enum usb_otg_state {
42 OTG_STATE_UNDEFINED = 0,
43
44 /* single-role peripheral, and dual-role default-b */
45 OTG_STATE_B_IDLE,
46 OTG_STATE_B_SRP_INIT,
47 OTG_STATE_B_PERIPHERAL,
48
49 /* extra dual-role default-b states */
50 OTG_STATE_B_WAIT_ACON,
51 OTG_STATE_B_HOST,
52
53 /* dual-role default-a */
54 OTG_STATE_A_IDLE,
55 OTG_STATE_A_WAIT_VRISE,
56 OTG_STATE_A_WAIT_BCON,
57 OTG_STATE_A_HOST,
58 OTG_STATE_A_SUSPEND,
59 OTG_STATE_A_PERIPHERAL,
60 OTG_STATE_A_WAIT_VFALL,
61 OTG_STATE_A_VBUS_ERR,
62};
63
Venu Byravarasude4217d2012-09-04 14:25:58 +053064struct usb_phy;
65struct usb_otg;
66
Peter Chen58efd4b02015-09-22 15:31:34 +080067/* for phys connected thru an ULPI interface, the user must
Venu Byravarasude4217d2012-09-04 14:25:58 +053068 * provide access ops
69 */
70struct usb_phy_io_ops {
71 int (*read)(struct usb_phy *x, u32 reg);
72 int (*write)(struct usb_phy *x, u32 val, u32 reg);
73};
74
75struct usb_phy {
76 struct device *dev;
77 const char *label;
78 unsigned int flags;
79
80 enum usb_phy_type type;
Venu Byravarasude4217d2012-09-04 14:25:58 +053081 enum usb_phy_events last_event;
82
83 struct usb_otg *otg;
84
85 struct device *io_dev;
86 struct usb_phy_io_ops *io_ops;
87 void __iomem *io_priv;
88
Baolin Wang7d21114d2017-05-05 14:12:24 +080089 /* to support extcon device */
90 struct extcon_dev *edev;
91 struct extcon_dev *id_edev;
92 struct notifier_block vbus_nb;
93 struct notifier_block id_nb;
94
Venu Byravarasude4217d2012-09-04 14:25:58 +053095 /* for notification of usb_phy_events */
96 struct atomic_notifier_head notifier;
97
98 /* to pass extra port status to the root hub */
99 u16 port_status;
100 u16 port_change;
101
Peter Chen58efd4b02015-09-22 15:31:34 +0800102 /* to support controllers that have multiple phys */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530103 struct list_head head;
104
Peter Chen58efd4b02015-09-22 15:31:34 +0800105 /* initialize/shutdown the phy */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530106 int (*init)(struct usb_phy *x);
107 void (*shutdown)(struct usb_phy *x);
108
Felipe Balbib7742122013-03-08 13:22:58 +0200109 /* enable/disable VBUS */
110 int (*set_vbus)(struct usb_phy *x, int on);
111
Venu Byravarasude4217d2012-09-04 14:25:58 +0530112 /* effective for B devices, ignored for A-peripheral */
113 int (*set_power)(struct usb_phy *x,
114 unsigned mA);
115
Peter Chen58efd4b02015-09-22 15:31:34 +0800116 /* Set phy into suspend mode */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530117 int (*set_suspend)(struct usb_phy *x,
118 int suspend);
119
Peter Chen57bf9b02014-02-24 10:21:01 +0800120 /*
121 * Set wakeup enable for PHY, in that case, the PHY can be
122 * woken up from suspend status due to external events,
123 * like vbus change, dp/dm change and id.
124 */
125 int (*set_wakeup)(struct usb_phy *x, bool enabled);
126
Venu Byravarasude4217d2012-09-04 14:25:58 +0530127 /* notify phy connect status change */
Peter Chenac965112012-11-09 09:44:44 +0800128 int (*notify_connect)(struct usb_phy *x,
129 enum usb_device_speed speed);
130 int (*notify_disconnect)(struct usb_phy *x,
131 enum usb_device_speed speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530132};
133
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530134/**
135 * struct usb_phy_bind - represent the binding for the phy
136 * @dev_name: the device name of the device that will bind to the phy
137 * @phy_dev_name: the device name of the phy
138 * @index: used if a single controller uses multiple phys
139 * @phy: reference to the phy
140 * @list: to maintain a linked list of the binding information
141 */
142struct usb_phy_bind {
143 const char *dev_name;
144 const char *phy_dev_name;
145 u8 index;
146 struct usb_phy *phy;
147 struct list_head list;
148};
Venu Byravarasude4217d2012-09-04 14:25:58 +0530149
150/* for board-specific init logic */
151extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530152extern int usb_add_phy_dev(struct usb_phy *);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530153extern void usb_remove_phy(struct usb_phy *);
154
155/* helpers for direct access thru low-level io interface */
156static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
157{
Felipe Balbi410aee72013-06-30 15:27:26 +0300158 if (x && x->io_ops && x->io_ops->read)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530159 return x->io_ops->read(x, reg);
160
161 return -EINVAL;
162}
163
164static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
165{
Felipe Balbi410aee72013-06-30 15:27:26 +0300166 if (x && x->io_ops && x->io_ops->write)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530167 return x->io_ops->write(x, val, reg);
168
169 return -EINVAL;
170}
171
172static inline int
173usb_phy_init(struct usb_phy *x)
174{
Felipe Balbi410aee72013-06-30 15:27:26 +0300175 if (x && x->init)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530176 return x->init(x);
177
178 return 0;
179}
180
181static inline void
182usb_phy_shutdown(struct usb_phy *x)
183{
Felipe Balbi410aee72013-06-30 15:27:26 +0300184 if (x && x->shutdown)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530185 x->shutdown(x);
186}
187
Felipe Balbib7742122013-03-08 13:22:58 +0200188static inline int
189usb_phy_vbus_on(struct usb_phy *x)
190{
Felipe Balbi410aee72013-06-30 15:27:26 +0300191 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200192 return 0;
193
194 return x->set_vbus(x, true);
195}
196
197static inline int
198usb_phy_vbus_off(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, false);
204}
205
Venu Byravarasude4217d2012-09-04 14:25:58 +0530206/* for usb host and peripheral controller drivers */
Felipe Balbiedc7cb22013-03-07 11:13:43 +0200207#if IS_ENABLED(CONFIG_USB_PHY)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530208extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
209extern struct usb_phy *devm_usb_get_phy(struct device *dev,
210 enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530211extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
212extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530213extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
214 const char *phandle, u8 index);
NeilBrowne842b842015-03-23 09:52:48 +1100215extern struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
216 struct device_node *node, struct notifier_block *nb);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530217extern void usb_put_phy(struct usb_phy *);
218extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530219extern int usb_bind_phy(const char *dev_name, u8 index,
220 const char *phy_dev_name);
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530221extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530222#else
223static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
224{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200225 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530226}
227
228static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
229 enum usb_phy_type type)
230{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200231 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530232}
233
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530234static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
235{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200236 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530237}
238
239static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
240{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200241 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530242}
243
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530244static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
245 const char *phandle, u8 index)
246{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200247 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530248}
249
Arnd Bergmann307c8582015-05-28 16:08:02 +0200250static inline struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
251 struct device_node *node, struct notifier_block *nb)
252{
253 return ERR_PTR(-ENXIO);
254}
255
Venu Byravarasude4217d2012-09-04 14:25:58 +0530256static inline void usb_put_phy(struct usb_phy *x)
257{
258}
259
260static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
261{
262}
263
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530264static inline int usb_bind_phy(const char *dev_name, u8 index,
265 const char *phy_dev_name)
266{
267 return -EOPNOTSUPP;
268}
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530269
270static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
271{
272}
Venu Byravarasude4217d2012-09-04 14:25:58 +0530273#endif
274
275static inline int
276usb_phy_set_power(struct usb_phy *x, unsigned mA)
277{
278 if (x && x->set_power)
279 return x->set_power(x, mA);
280 return 0;
281}
282
283/* Context: can sleep */
284static inline int
285usb_phy_set_suspend(struct usb_phy *x, int suspend)
286{
Felipe Balbi410aee72013-06-30 15:27:26 +0300287 if (x && x->set_suspend != NULL)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530288 return x->set_suspend(x, suspend);
289 else
290 return 0;
291}
292
293static inline int
Peter Chen57bf9b02014-02-24 10:21:01 +0800294usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
295{
296 if (x && x->set_wakeup)
297 return x->set_wakeup(x, enabled);
298 else
299 return 0;
300}
301
302static inline int
Peter Chenac965112012-11-09 09:44:44 +0800303usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530304{
Felipe Balbi410aee72013-06-30 15:27:26 +0300305 if (x && x->notify_connect)
Peter Chenac965112012-11-09 09:44:44 +0800306 return x->notify_connect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530307 else
308 return 0;
309}
310
311static inline int
Peter Chenac965112012-11-09 09:44:44 +0800312usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530313{
Felipe Balbi410aee72013-06-30 15:27:26 +0300314 if (x && x->notify_disconnect)
Peter Chenac965112012-11-09 09:44:44 +0800315 return x->notify_disconnect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530316 else
317 return 0;
318}
319
320/* notifiers */
321static inline int
322usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
323{
324 return atomic_notifier_chain_register(&x->notifier, nb);
325}
326
327static inline void
328usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
329{
330 atomic_notifier_chain_unregister(&x->notifier, nb);
331}
332
333static inline const char *usb_phy_type_string(enum usb_phy_type type)
334{
335 switch (type) {
336 case USB_PHY_TYPE_USB2:
337 return "USB2 PHY";
338 case USB_PHY_TYPE_USB3:
339 return "USB3 PHY";
340 default:
341 return "UNKNOWN PHY TYPE";
342 }
343}
344#endif /* __LINUX_USB_PHY_H */