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.
9 #ifndef __LINUX_USB_PHY_H
10 #define __LINUX_USB_PHY_H
12 #include <linux/extcon.h>
13 #include <linux/notifier.h>
14 #include <linux/usb.h>
15 #include <uapi/linux/usb/charger.h>
17 enum usb_phy_interface {
18 USBPHY_INTERFACE_MODE_UNKNOWN,
19 USBPHY_INTERFACE_MODE_UTMI,
20 USBPHY_INTERFACE_MODE_UTMIW,
21 USBPHY_INTERFACE_MODE_ULPI,
22 USBPHY_INTERFACE_MODE_SERIAL,
23 USBPHY_INTERFACE_MODE_HSIC,
27 USB_EVENT_NONE, /* no events or cable disconnected */
28 USB_EVENT_VBUS, /* vbus valid event */
29 USB_EVENT_ID, /* id was grounded */
30 USB_EVENT_CHARGER, /* usb dedicated charger */
31 USB_EVENT_ENUMERATED, /* gadget driver enumerated */
34 /* associate a type with PHY */
36 USB_PHY_TYPE_UNDEFINED,
41 /* OTG defines lots of enumeration states before device reset */
43 OTG_STATE_UNDEFINED = 0,
45 /* single-role peripheral, and dual-role default-b */
48 OTG_STATE_B_PERIPHERAL,
50 /* extra dual-role default-b states */
51 OTG_STATE_B_WAIT_ACON,
54 /* dual-role default-a */
56 OTG_STATE_A_WAIT_VRISE,
57 OTG_STATE_A_WAIT_BCON,
60 OTG_STATE_A_PERIPHERAL,
61 OTG_STATE_A_WAIT_VFALL,
68 /* for phys connected thru an ULPI interface, the user must
71 struct usb_phy_io_ops {
72 int (*read)(struct usb_phy *x, u32 reg);
73 int (*write)(struct usb_phy *x, u32 val, u32 reg);
76 struct usb_charger_current {
92 enum usb_phy_type type;
93 enum usb_phy_events last_event;
97 struct device *io_dev;
98 struct usb_phy_io_ops *io_ops;
99 void __iomem *io_priv;
101 /* to support extcon device */
102 struct extcon_dev *edev;
103 struct extcon_dev *id_edev;
104 struct notifier_block vbus_nb;
105 struct notifier_block id_nb;
106 struct notifier_block type_nb;
108 /* Support USB charger */
109 enum usb_charger_type chg_type;
110 enum usb_charger_state chg_state;
111 struct usb_charger_current chg_cur;
112 struct work_struct chg_work;
114 /* for notification of usb_phy_events */
115 struct atomic_notifier_head notifier;
117 /* to pass extra port status to the root hub */
121 /* to support controllers that have multiple phys */
122 struct list_head head;
124 /* initialize/shutdown the phy */
125 int (*init)(struct usb_phy *x);
126 void (*shutdown)(struct usb_phy *x);
128 /* enable/disable VBUS */
129 int (*set_vbus)(struct usb_phy *x, int on);
131 /* effective for B devices, ignored for A-peripheral */
132 int (*set_power)(struct usb_phy *x,
135 /* Set phy into suspend mode */
136 int (*set_suspend)(struct usb_phy *x,
140 * Set wakeup enable for PHY, in that case, the PHY can be
141 * woken up from suspend status due to external events,
142 * like vbus change, dp/dm change and id.
144 int (*set_wakeup)(struct usb_phy *x, bool enabled);
146 /* notify phy connect status change */
147 int (*notify_connect)(struct usb_phy *x,
148 enum usb_device_speed speed);
149 int (*notify_disconnect)(struct usb_phy *x,
150 enum usb_device_speed speed);
153 * Charger detection method can be implemented if you need to
154 * manually detect the charger type.
156 enum usb_charger_type (*charger_detect)(struct usb_phy *x);
160 * struct usb_phy_bind - represent the binding for the phy
161 * @dev_name: the device name of the device that will bind to the phy
162 * @phy_dev_name: the device name of the phy
163 * @index: used if a single controller uses multiple phys
164 * @phy: reference to the phy
165 * @list: to maintain a linked list of the binding information
167 struct usb_phy_bind {
168 const char *dev_name;
169 const char *phy_dev_name;
172 struct list_head list;
175 /* for board-specific init logic */
176 extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
177 extern int usb_add_phy_dev(struct usb_phy *);
178 extern void usb_remove_phy(struct usb_phy *);
180 /* helpers for direct access thru low-level io interface */
181 static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
183 if (x && x->io_ops && x->io_ops->read)
184 return x->io_ops->read(x, reg);
189 static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
191 if (x && x->io_ops && x->io_ops->write)
192 return x->io_ops->write(x, val, reg);
198 usb_phy_init(struct usb_phy *x)
207 usb_phy_shutdown(struct usb_phy *x)
209 if (x && x->shutdown)
214 usb_phy_vbus_on(struct usb_phy *x)
216 if (!x || !x->set_vbus)
219 return x->set_vbus(x, true);
223 usb_phy_vbus_off(struct usb_phy *x)
225 if (!x || !x->set_vbus)
228 return x->set_vbus(x, false);
231 /* for usb host and peripheral controller drivers */
232 #if IS_ENABLED(CONFIG_USB_PHY)
233 extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
234 extern struct usb_phy *devm_usb_get_phy(struct device *dev,
235 enum usb_phy_type type);
236 extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
237 extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
238 extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
239 const char *phandle, u8 index);
240 extern struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
241 struct device_node *node, struct notifier_block *nb);
242 extern void usb_put_phy(struct usb_phy *);
243 extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
244 extern int usb_bind_phy(const char *dev_name, u8 index,
245 const char *phy_dev_name);
246 extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
247 extern void usb_phy_set_charger_current(struct usb_phy *usb_phy,
249 extern void usb_phy_get_charger_current(struct usb_phy *usb_phy,
250 unsigned int *min, unsigned int *max);
251 extern void usb_phy_set_charger_state(struct usb_phy *usb_phy,
252 enum usb_charger_state state);
254 static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
256 return ERR_PTR(-ENXIO);
259 static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
260 enum usb_phy_type type)
262 return ERR_PTR(-ENXIO);
265 static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
267 return ERR_PTR(-ENXIO);
270 static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
272 return ERR_PTR(-ENXIO);
275 static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
276 const char *phandle, u8 index)
278 return ERR_PTR(-ENXIO);
281 static inline struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
282 struct device_node *node, struct notifier_block *nb)
284 return ERR_PTR(-ENXIO);
287 static inline void usb_put_phy(struct usb_phy *x)
291 static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
295 static inline int usb_bind_phy(const char *dev_name, u8 index,
296 const char *phy_dev_name)
301 static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
305 static inline void usb_phy_set_charger_current(struct usb_phy *usb_phy,
310 static inline void usb_phy_get_charger_current(struct usb_phy *usb_phy,
316 static inline void usb_phy_set_charger_state(struct usb_phy *usb_phy,
317 enum usb_charger_state state)
323 usb_phy_set_power(struct usb_phy *x, unsigned mA)
328 usb_phy_set_charger_current(x, mA);
331 return x->set_power(x, mA);
335 /* Context: can sleep */
337 usb_phy_set_suspend(struct usb_phy *x, int suspend)
339 if (x && x->set_suspend != NULL)
340 return x->set_suspend(x, suspend);
346 usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
348 if (x && x->set_wakeup)
349 return x->set_wakeup(x, enabled);
355 usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
357 if (x && x->notify_connect)
358 return x->notify_connect(x, speed);
364 usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
366 if (x && x->notify_disconnect)
367 return x->notify_disconnect(x, speed);
374 usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
376 return atomic_notifier_chain_register(&x->notifier, nb);
380 usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
382 atomic_notifier_chain_unregister(&x->notifier, nb);
385 static inline const char *usb_phy_type_string(enum usb_phy_type type)
388 case USB_PHY_TYPE_USB2:
390 case USB_PHY_TYPE_USB3:
393 return "UNKNOWN PHY TYPE";
396 #endif /* __LINUX_USB_PHY_H */