1 /* SPDX-License-Identifier: GPL-2.0 */
5 * These APIs may be used between USB controllers. USB device drivers
6 * (for either host or peripheral roles) don't use these calls; they
7 * continue to use just usb_device and usb_gadget.
10 #ifndef __LINUX_USB_PHY_H
11 #define __LINUX_USB_PHY_H
13 #include <linux/extcon.h>
14 #include <linux/notifier.h>
15 #include <linux/usb.h>
16 #include <uapi/linux/usb/charger.h>
18 enum usb_phy_interface
{
19 USBPHY_INTERFACE_MODE_UNKNOWN
,
20 USBPHY_INTERFACE_MODE_UTMI
,
21 USBPHY_INTERFACE_MODE_UTMIW
,
22 USBPHY_INTERFACE_MODE_ULPI
,
23 USBPHY_INTERFACE_MODE_SERIAL
,
24 USBPHY_INTERFACE_MODE_HSIC
,
28 USB_EVENT_NONE
, /* no events or cable disconnected */
29 USB_EVENT_VBUS
, /* vbus valid event */
30 USB_EVENT_ID
, /* id was grounded */
31 USB_EVENT_CHARGER
, /* usb dedicated charger */
32 USB_EVENT_ENUMERATED
, /* gadget driver enumerated */
35 /* associate a type with PHY */
37 USB_PHY_TYPE_UNDEFINED
,
42 /* OTG defines lots of enumeration states before device reset */
44 OTG_STATE_UNDEFINED
= 0,
46 /* single-role peripheral, and dual-role default-b */
49 OTG_STATE_B_PERIPHERAL
,
51 /* extra dual-role default-b states */
52 OTG_STATE_B_WAIT_ACON
,
55 /* dual-role default-a */
57 OTG_STATE_A_WAIT_VRISE
,
58 OTG_STATE_A_WAIT_BCON
,
61 OTG_STATE_A_PERIPHERAL
,
62 OTG_STATE_A_WAIT_VFALL
,
69 /* for phys connected thru an ULPI interface, the user must
72 struct usb_phy_io_ops
{
73 int (*read
)(struct usb_phy
*x
, u32 reg
);
74 int (*write
)(struct usb_phy
*x
, u32 val
, u32 reg
);
77 struct usb_charger_current
{
93 enum usb_phy_type type
;
94 enum usb_phy_events last_event
;
98 struct device
*io_dev
;
99 struct usb_phy_io_ops
*io_ops
;
100 void __iomem
*io_priv
;
102 /* to support extcon device */
103 struct extcon_dev
*edev
;
104 struct extcon_dev
*id_edev
;
105 struct notifier_block vbus_nb
;
106 struct notifier_block id_nb
;
107 struct notifier_block type_nb
;
109 /* Support USB charger */
110 enum usb_charger_type chg_type
;
111 enum usb_charger_state chg_state
;
112 struct usb_charger_current chg_cur
;
113 struct work_struct chg_work
;
115 /* for notification of usb_phy_events */
116 struct atomic_notifier_head notifier
;
118 /* to pass extra port status to the root hub */
122 /* to support controllers that have multiple phys */
123 struct list_head head
;
125 /* initialize/shutdown the phy */
126 int (*init
)(struct usb_phy
*x
);
127 void (*shutdown
)(struct usb_phy
*x
);
129 /* enable/disable VBUS */
130 int (*set_vbus
)(struct usb_phy
*x
, int on
);
132 /* effective for B devices, ignored for A-peripheral */
133 int (*set_power
)(struct usb_phy
*x
,
136 /* Set phy into suspend mode */
137 int (*set_suspend
)(struct usb_phy
*x
,
141 * Set wakeup enable for PHY, in that case, the PHY can be
142 * woken up from suspend status due to external events,
143 * like vbus change, dp/dm change and id.
145 int (*set_wakeup
)(struct usb_phy
*x
, bool enabled
);
147 /* notify phy connect status change */
148 int (*notify_connect
)(struct usb_phy
*x
,
149 enum usb_device_speed speed
);
150 int (*notify_disconnect
)(struct usb_phy
*x
,
151 enum usb_device_speed speed
);
154 * Charger detection method can be implemented if you need to
155 * manually detect the charger type.
157 enum usb_charger_type (*charger_detect
)(struct usb_phy
*x
);
161 * struct usb_phy_bind - represent the binding for the phy
162 * @dev_name: the device name of the device that will bind to the phy
163 * @phy_dev_name: the device name of the phy
164 * @index: used if a single controller uses multiple phys
165 * @phy: reference to the phy
166 * @list: to maintain a linked list of the binding information
168 struct usb_phy_bind
{
169 const char *dev_name
;
170 const char *phy_dev_name
;
173 struct list_head list
;
176 /* for board-specific init logic */
177 extern int usb_add_phy(struct usb_phy
*, enum usb_phy_type type
);
178 extern int usb_add_phy_dev(struct usb_phy
*);
179 extern void usb_remove_phy(struct usb_phy
*);
181 /* helpers for direct access thru low-level io interface */
182 static inline int usb_phy_io_read(struct usb_phy
*x
, u32 reg
)
184 if (x
&& x
->io_ops
&& x
->io_ops
->read
)
185 return x
->io_ops
->read(x
, reg
);
190 static inline int usb_phy_io_write(struct usb_phy
*x
, u32 val
, u32 reg
)
192 if (x
&& x
->io_ops
&& x
->io_ops
->write
)
193 return x
->io_ops
->write(x
, val
, reg
);
199 usb_phy_init(struct usb_phy
*x
)
208 usb_phy_shutdown(struct usb_phy
*x
)
210 if (x
&& x
->shutdown
)
215 usb_phy_vbus_on(struct usb_phy
*x
)
217 if (!x
|| !x
->set_vbus
)
220 return x
->set_vbus(x
, true);
224 usb_phy_vbus_off(struct usb_phy
*x
)
226 if (!x
|| !x
->set_vbus
)
229 return x
->set_vbus(x
, false);
232 /* for usb host and peripheral controller drivers */
233 #if IS_ENABLED(CONFIG_USB_PHY)
234 extern struct usb_phy
*usb_get_phy(enum usb_phy_type type
);
235 extern struct usb_phy
*devm_usb_get_phy(struct device
*dev
,
236 enum usb_phy_type type
);
237 extern struct usb_phy
*usb_get_phy_dev(struct device
*dev
, u8 index
);
238 extern struct usb_phy
*devm_usb_get_phy_dev(struct device
*dev
, u8 index
);
239 extern struct usb_phy
*devm_usb_get_phy_by_phandle(struct device
*dev
,
240 const char *phandle
, u8 index
);
241 extern struct usb_phy
*devm_usb_get_phy_by_node(struct device
*dev
,
242 struct device_node
*node
, struct notifier_block
*nb
);
243 extern void usb_put_phy(struct usb_phy
*);
244 extern void devm_usb_put_phy(struct device
*dev
, struct usb_phy
*x
);
245 extern int usb_bind_phy(const char *dev_name
, u8 index
,
246 const char *phy_dev_name
);
247 extern void usb_phy_set_event(struct usb_phy
*x
, unsigned long event
);
248 extern void usb_phy_set_charger_current(struct usb_phy
*usb_phy
,
250 extern void usb_phy_get_charger_current(struct usb_phy
*usb_phy
,
251 unsigned int *min
, unsigned int *max
);
252 extern void usb_phy_set_charger_state(struct usb_phy
*usb_phy
,
253 enum usb_charger_state state
);
255 static inline struct usb_phy
*usb_get_phy(enum usb_phy_type type
)
257 return ERR_PTR(-ENXIO
);
260 static inline struct usb_phy
*devm_usb_get_phy(struct device
*dev
,
261 enum usb_phy_type type
)
263 return ERR_PTR(-ENXIO
);
266 static inline struct usb_phy
*usb_get_phy_dev(struct device
*dev
, u8 index
)
268 return ERR_PTR(-ENXIO
);
271 static inline struct usb_phy
*devm_usb_get_phy_dev(struct device
*dev
, u8 index
)
273 return ERR_PTR(-ENXIO
);
276 static inline struct usb_phy
*devm_usb_get_phy_by_phandle(struct device
*dev
,
277 const char *phandle
, u8 index
)
279 return ERR_PTR(-ENXIO
);
282 static inline struct usb_phy
*devm_usb_get_phy_by_node(struct device
*dev
,
283 struct device_node
*node
, struct notifier_block
*nb
)
285 return ERR_PTR(-ENXIO
);
288 static inline void usb_put_phy(struct usb_phy
*x
)
292 static inline void devm_usb_put_phy(struct device
*dev
, struct usb_phy
*x
)
296 static inline int usb_bind_phy(const char *dev_name
, u8 index
,
297 const char *phy_dev_name
)
302 static inline void usb_phy_set_event(struct usb_phy
*x
, unsigned long event
)
306 static inline void usb_phy_set_charger_current(struct usb_phy
*usb_phy
,
311 static inline void usb_phy_get_charger_current(struct usb_phy
*usb_phy
,
317 static inline void usb_phy_set_charger_state(struct usb_phy
*usb_phy
,
318 enum usb_charger_state state
)
324 usb_phy_set_power(struct usb_phy
*x
, unsigned mA
)
329 usb_phy_set_charger_current(x
, mA
);
332 return x
->set_power(x
, mA
);
336 /* Context: can sleep */
338 usb_phy_set_suspend(struct usb_phy
*x
, int suspend
)
340 if (x
&& x
->set_suspend
!= NULL
)
341 return x
->set_suspend(x
, suspend
);
347 usb_phy_set_wakeup(struct usb_phy
*x
, bool enabled
)
349 if (x
&& x
->set_wakeup
)
350 return x
->set_wakeup(x
, enabled
);
356 usb_phy_notify_connect(struct usb_phy
*x
, enum usb_device_speed speed
)
358 if (x
&& x
->notify_connect
)
359 return x
->notify_connect(x
, speed
);
365 usb_phy_notify_disconnect(struct usb_phy
*x
, enum usb_device_speed speed
)
367 if (x
&& x
->notify_disconnect
)
368 return x
->notify_disconnect(x
, speed
);
375 usb_register_notifier(struct usb_phy
*x
, struct notifier_block
*nb
)
377 return atomic_notifier_chain_register(&x
->notifier
, nb
);
381 usb_unregister_notifier(struct usb_phy
*x
, struct notifier_block
*nb
)
383 atomic_notifier_chain_unregister(&x
->notifier
, nb
);
386 static inline const char *usb_phy_type_string(enum usb_phy_type type
)
389 case USB_PHY_TYPE_USB2
:
391 case USB_PHY_TYPE_USB3
:
394 return "UNKNOWN PHY TYPE";
397 #endif /* __LINUX_USB_PHY_H */